问题描述
以下是我用来绘制分组条形图的代码: 我能够保持一致的条形宽度,但是如果使用的话,无法删除丢失数据的空白区域
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"),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,hjust = .5,vjust = .5,legend.text = element_text(size = 12,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的新手,请帮助指出我要去哪里或对这个问题是否有其他解决方案。