删除空数据空间并在分组条形图中保留统一的条宽。在 ggplot2 中使用小平面网格
Removing the empty data spaces and retaining the uniform bar width in grouped bar chart. Using facet grid in ggplot2
以下是我用来绘制分组条形图的代码:
如果我使用
,我能够保持统一的条宽但无法删除丢失数据的空白区域
geom_bar(stat="identity",position=position_dodge(preserve = "single"),na.rm = TRUE,width = 0.9).
如果我不使用上述命令,尽管我可以删除空格,但我无法保留条形宽度。
library(ggplot2)
library(reshape2)
library(readxl)
df <- read_excel("Documents/Analysis.xlsx")
ggplot(data = df, aes(x = Cell_type, y = Percentage_of_cell, fill = Sample_id, na.rm = TRUE),
position = position_dodge(preserve = "single", padding = 0)) +
geom_bar(stat = "identity", position = position_dodge(preserve = "single"), na.rm = TRUE, width = 0.9)+
theme_bw() +
scale_fill_manual(values=c("darkgoldenrod3","darkolivegreen"))+
theme(axis.text.x = element_blank(),
axis.text.y = element_text(color="black", angle = 90, hjust = 1, size = 12, face = "bold"),
axis.title.y = element_text( size = 12, angle = 90, hjust = .5, vjust = .5, face = "bold"),
legend.text = element_text(size = 12, face = "bold"),
axis.ticks.x = element_blank())+
labs(fill="") +
xlab("") +
ylab("% of Cell_type\n") +
facet_grid(~Cell_type),
scales = "free_x", space = "free_x")+
theme(plot.margin = unit(c(1,1,1.5,1.2),"cm"))+
theme(strip.text = element_text(colour = 'black', face="bold", size=11))+
theme(legend.position = "bottom")
这是 df:
Sample_id Cell_type Percentage_of_cell
Sample1 Cell1 4.701222662
Sample2 Cell1 0.829875519
Sample2 Cell2 5.526216522
Sample1 Cell3 1.980368521
Sample2 Cell3 20.50169747
Sample1 Cell4 22.75701739
Sample2 Cell4 21.3504338
Sample1 Cell5 15.00774927
Sample2 Cell5 9.430403621
Sample1 Cell6 7.465128293
Sample1 Cell7 0.602720854
Sample2 Cell7 1.772915881
Sample1 Cell8 44.07611503
Sample2 Cell8 40.58845719
Sample1 Unassigned 3.409677975
我是R的新手,请帮忙指出我哪里出错了,或者是否有其他解决这个问题的方法。
这是您要找的吗?
请注意:
x
在 aes
现在是 Sampl_id
position_dodge
已删除
如果你需要 stat=identity
,geom_col
比 geom_bar
更简洁
theme
和lab
现在都在一起了
scales
和 facet_grid
中的 space
现在可以正常工作了
ggplot(data = df, aes(x = Sample_id, y = Percentage_of_cell, fill = Sample_id)) +
geom_col(width = 0.9) +
facet_grid(cols = vars(Cell_type),
labeller = label_wrap_gen(width = 16,multi_line = TRUE),
scales="free", space = "free") +
labs(x = "", y = "% of Cell_type\n", fill = "") +
scale_fill_manual(values = c("darkgoldenrod3", "darkolivegreen")) +
theme_bw() +
theme(axis.text.x = element_blank(),
axis.text.y = element_text(color = "black", angle = 90, hjust = 1, size = 12, face = "bold"),
axis.title.y = element_text(size = 12, angle = 90, hjust = 0.5, vjust = 0.5, face = "bold"),
legend.text = element_text(size = 12, face = "bold"),
axis.ticks.x = element_blank(),
plot.margin = unit(c(1,1,1.5,1.2),"cm"),
strip.text = element_text(colour = 'black', face = "bold", size = 11),
legend.position = "bottom")
以下是我用来绘制分组条形图的代码: 如果我使用
,我能够保持统一的条宽但无法删除丢失数据的空白区域geom_bar(stat="identity",position=position_dodge(preserve = "single"),na.rm = TRUE,width = 0.9).
如果我不使用上述命令,尽管我可以删除空格,但我无法保留条形宽度。
library(ggplot2)
library(reshape2)
library(readxl)
df <- read_excel("Documents/Analysis.xlsx")
ggplot(data = df, aes(x = Cell_type, y = Percentage_of_cell, fill = Sample_id, na.rm = TRUE),
position = position_dodge(preserve = "single", padding = 0)) +
geom_bar(stat = "identity", position = position_dodge(preserve = "single"), na.rm = TRUE, width = 0.9)+
theme_bw() +
scale_fill_manual(values=c("darkgoldenrod3","darkolivegreen"))+
theme(axis.text.x = element_blank(),
axis.text.y = element_text(color="black", angle = 90, hjust = 1, size = 12, face = "bold"),
axis.title.y = element_text( size = 12, angle = 90, hjust = .5, vjust = .5, face = "bold"),
legend.text = element_text(size = 12, face = "bold"),
axis.ticks.x = element_blank())+
labs(fill="") +
xlab("") +
ylab("% of Cell_type\n") +
facet_grid(~Cell_type),
scales = "free_x", space = "free_x")+
theme(plot.margin = unit(c(1,1,1.5,1.2),"cm"))+
theme(strip.text = element_text(colour = 'black', face="bold", size=11))+
theme(legend.position = "bottom")
这是 df:
Sample_id Cell_type Percentage_of_cell
Sample1 Cell1 4.701222662
Sample2 Cell1 0.829875519
Sample2 Cell2 5.526216522
Sample1 Cell3 1.980368521
Sample2 Cell3 20.50169747
Sample1 Cell4 22.75701739
Sample2 Cell4 21.3504338
Sample1 Cell5 15.00774927
Sample2 Cell5 9.430403621
Sample1 Cell6 7.465128293
Sample1 Cell7 0.602720854
Sample2 Cell7 1.772915881
Sample1 Cell8 44.07611503
Sample2 Cell8 40.58845719
Sample1 Unassigned 3.409677975
我是R的新手,请帮忙指出我哪里出错了,或者是否有其他解决这个问题的方法。
这是您要找的吗?
请注意:
x
在aes
现在是Sampl_id
position_dodge
已删除
如果你需要 geom_col
比geom_bar
更简洁theme
和lab
现在都在一起了scales
和facet_grid
中的space
现在可以正常工作了
stat=identity
,ggplot(data = df, aes(x = Sample_id, y = Percentage_of_cell, fill = Sample_id)) +
geom_col(width = 0.9) +
facet_grid(cols = vars(Cell_type),
labeller = label_wrap_gen(width = 16,multi_line = TRUE),
scales="free", space = "free") +
labs(x = "", y = "% of Cell_type\n", fill = "") +
scale_fill_manual(values = c("darkgoldenrod3", "darkolivegreen")) +
theme_bw() +
theme(axis.text.x = element_blank(),
axis.text.y = element_text(color = "black", angle = 90, hjust = 1, size = 12, face = "bold"),
axis.title.y = element_text(size = 12, angle = 90, hjust = 0.5, vjust = 0.5, face = "bold"),
legend.text = element_text(size = 12, face = "bold"),
axis.ticks.x = element_blank(),
plot.margin = unit(c(1,1,1.5,1.2),"cm"),
strip.text = element_text(colour = 'black', face = "bold", size = 11),
legend.position = "bottom")