是否有一种简单的方法可以用R制作水平的单堆堆积的Barplot?

发布于 2025-01-30 18:52:57 字数 338 浏览 5 评论 0原文

如果有人可以帮助我与R一起制作单个堆叠的Barplot,我将不胜感激。这是我使用的数据的一个小例子:

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

非常感谢您的帮助。

I'd appreciate it if anyone could help me with making a single stacked Barplot with R. Here is a small example of data to plot:

cell_type   Percentage
CD20 B cells    15.00
CD4 T cells 25.00
Other cells 60.00

This is what I used, but cannot modify it too much.

p1 <-  ggplot(Data, aes(x = "", y = percentage, fill = cell_type))

p1

p2 <-  p1 + geom_col()
p2

Many thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2025-02-06 18:52:57

使用coord_flip使其水平调整栏厚度指定width参数,并且可以使用scape_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轴变量。

Use coord_flip to make it horizontal, to adjust bar thickness specify the width argument, and you can set fill colors manually using 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() 

If you have multiple bars then you will need to set an x-axis variable.

enter image description here

安静 2025-02-06 18:52:57

也许您想要这样的东西:

library(tidyverse)
df %>%
  mutate(dummy = "v") %>%
  ggplot(aes(x = dummy, y = Percentage, fill = cell_type)) +
  geom_col() +
  geom_text(aes(label = paste0(Percentage, "%")),
            position = position_stack(vjust = 0.5)) +
  theme_minimal() +
  labs(x = "", y = "Percentage")

输出:

”在此处输入图像描述”

Maybe you want something like this:

library(tidyverse)
df %>%
  mutate(dummy = "v") %>%
  ggplot(aes(x = dummy, y = Percentage, fill = cell_type)) +
  geom_col() +
  geom_text(aes(label = paste0(Percentage, "%")),
            position = position_stack(vjust = 0.5)) +
  theme_minimal() +
  labs(x = "", y = "Percentage")

Output:

enter image description here

月亮坠入山谷 2025-02-06 18:52:57

更新:
X-axis:


library(dplyr)
library(tibble)

my_matrix <- df %>% 
  column_to_rownames("cell_type") %>% 
  rename(Cells = Percentage) %>% 
  as.matrix()

my_Matrix <- apply(my_matrix, 2, cumsum)
my_Matrix <- my_Matrix - my_matrix / 2


par(mfrow = c(1, 3))
x <- barplot(my_matrix,
             ylab = "Percentage",
             col=c("red2", "green3", "slateblue4"),
             border="white",
             legend.text = rownames(my_matrix),
             args.legend=list(cex=1,x = "topright"))


text(rep(x, each = nrow(my_Matrix)), my_Matrix, cex = 1.5, col = "white", labels = paste0(Percentage, "%"))

“在此处输入图像描述”

第一个答案:

这是 base r barplot :作为替代方法:

library(dplyr)
library(tibble)

my_matrix <- df %>% 
  column_to_rownames("cell_type") %>% 
  as.matrix()

my_Matrix <- apply(my_matrix, 2, cumsum)
my_Matrix <- my_Matrix - my_matrix / 2


par(mfrow = c(1, 3))
x <- barplot(my_matrix,
        col=c("red2", "green3", "slateblue4"),
        border="white",
       # col = 1 + 1:nrow(my_matrix),
        width = 0.5,
        legend.text = rownames(my_matrix),
        args.legend=list(cex=1,x = "topright"))

text(rep(x, each = nrow(my_Matrix)), my_Matrix, cex = 1.5, col = "white", labels = paste0(Percentage, "%"))


Update:
x-axis:


library(dplyr)
library(tibble)

my_matrix <- df %>% 
  column_to_rownames("cell_type") %>% 
  rename(Cells = Percentage) %>% 
  as.matrix()

my_Matrix <- apply(my_matrix, 2, cumsum)
my_Matrix <- my_Matrix - my_matrix / 2


par(mfrow = c(1, 3))
x <- barplot(my_matrix,
             ylab = "Percentage",
             col=c("red2", "green3", "slateblue4"),
             border="white",
             legend.text = rownames(my_matrix),
             args.legend=list(cex=1,x = "topright"))


text(rep(x, each = nrow(my_Matrix)), my_Matrix, cex = 1.5, col = "white", labels = paste0(Percentage, "%"))

enter image description here

First answer:

Here is a base R barplot: as an alternative approach:

library(dplyr)
library(tibble)

my_matrix <- df %>% 
  column_to_rownames("cell_type") %>% 
  as.matrix()

my_Matrix <- apply(my_matrix, 2, cumsum)
my_Matrix <- my_Matrix - my_matrix / 2


par(mfrow = c(1, 3))
x <- barplot(my_matrix,
        col=c("red2", "green3", "slateblue4"),
        border="white",
       # col = 1 + 1:nrow(my_matrix),
        width = 0.5,
        legend.text = rownames(my_matrix),
        args.legend=list(cex=1,x = "topright"))

text(rep(x, each = nrow(my_Matrix)), my_Matrix, cex = 1.5, col = "white", labels = paste0(Percentage, "%"))


enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文