微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

删除空的数据空间并在组合的条形图中保留统一的条形宽度在ggplot2中使用构面网格

如何解决删除空的数据空间并在组合的条形图中保留统一的条形宽度在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"),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的新手,请帮助指出我要去哪里或对这个问题是否有其他解决方案。

解决方法

这是您要寻找的吗?

注意:

    x中的
  • aes现在为Sampl_id
  • position_dodge已被删除
  • 如果您需要geom_col
  • geom_barstat=identity现在在一起
  • theme中的
  • labscales现在可以正常工作了
space

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。