R 删除空数据空间并在分组条形图中保留统一的条形宽度。在ggplot2中使用镶嵌面栅格

R 删除空数据空间并在分组条形图中保留统一的条形宽度。在ggplot2中使用镶嵌面栅格,r,ggplot2,facet-grid,R,Ggplot2,Facet Grid,以下是我用来绘制分组条形图的代码: 我可以保留统一的条形宽度,但如果使用,则无法删除缺失数据的空白 geom_bar(stat="identity",position=position_dodge(preserve = "single"),na.rm = TRUE,width = 0.9). 如果我没有使用上面的命令,我将无法保留条形图的宽度,尽管我可以删除空白 library(ggplot2) library(reshape2) library(re

以下是我用来绘制分组条形图的代码: 我可以保留统一的条形宽度,但如果使用,则无法删除缺失数据的空白

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")

我是R的新手,请帮我指出哪里出了问题,或者是否有其他解决方案。

这就是您想要的吗

请注意:

  • aes中的
    x
    现在是
    Sampl\u id
  • position\u dodge
    已删除
  • 如果您需要
    stat=identity
  • theme
    s和
    lab
    s现在都在一起了
  • 小平面网格中的
    缩放
    空间
    现在按预期工作

这就是你要找的吗

请注意:

  • aes中的
    x
    现在是
    Sampl\u id
  • position\u dodge
    已删除
  • 如果您需要
    stat=identity
  • theme
    s和
    lab
    s现在都在一起了
  • 小平面网格中的
    缩放
    空间
    现在按预期工作

如果您以这种方式编辑df
:完成(df、样本id、tidyr::嵌套(单元格类型)、填充=列表(单元格百分比=0))
,您是否得到了预期结果?[问题出在2号牢房,对吧?]我也试过了。但是它对我不起作用,问题是Cell2、Cell6和Unassigned,我只有一个样本的数据。那么我不理解您的预期结果。当柱单独存在时,您希望它位于中心吗?是的,我希望在Cell2、Cell6面中心设置条形图,而不是在Sample1和Sample2空间中。如果您以这种方式编辑
df
tidyr::complete(df,Sample_id,tidyr::nesting(Cell_type),fill=list(Cell的百分比=0))
,您是否得到了预期的结果?[问题出在2号牢房,对吧?]我也试过了。但是它对我不起作用,问题是Cell2、Cell6和Unassigned,我只有一个样本的数据。那么我不理解您的预期结果。当柱单独存在时,你想让它在中心吗?是的,我想在2号、6号单元的中心,而不是在1号和2号样本的空间。
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  
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")