如果有人可以帮助我使用 R 制作单个堆叠条形图,我将不胜感激。这是要绘制的数据的一个小示例:
cell_type Percentage
CD20 B cells 15.00
CD4 T cells 25.00
Other cells 60.00
这是我用的,但不能修改太多。
p1 <- ggplot(Data, aes(x = "", y = percentage, fill = cell_type))
p1
p2 <- p1 + geom_col()
p2
非常感谢您的帮助。
最佳答案
使用 coord_flip
使其水平,调整条的粗细指定 width
参数,您可以使用 scale_fill_manual
手动设置填充颜色:
library(ggplot2)
df %>%
ggplot(aes(x = "", y = Percentage, fill = cell_type)) +
geom_col(width = .25) +
scale_fill_manual(values = c("black", "#039dfc", "yellow")) +
coord_flip()
如果您有多个条,则需要设置一个 x 轴变量。
https://stackoverflow.com/questions/72322185/