有没有一种简单的方法可以用 R 制作水平单层堆叠条形图?
Is there an easy way to make an horizontal single stacked Barplot with R?
如果有人能帮我用 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-axis 变量。
也许你想要这样的东西:
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")
输出:
更新:
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 条形图:作为替代方法:
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, "%"))
如果有人能帮我用 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-axis 变量。
也许你想要这样的东西:
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")
输出:
更新: 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 条形图:作为替代方法:
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, "%"))