【问题标题】:Rstudio: Adjusting bar width of grouped bar plot in facetsRstudio:调整构面中分组条形图的条形宽度
【发布时间】:2020-11-21 13:04:20
【问题描述】:

我有一个数据集,我试图将其绘制为构面中的分组列,列的大小相同。

数据集:

Day=rep(1:6, times=15)

颜色=c("黄色","黄色","黄色","黄色","黄色","黄色", “红色”、“红色”、“红色”、“红色”、“红色”、“红色”、 "绿色","绿色","绿色","绿色","绿色","绿色", "黄色","黄色","黄色","黄色","黄色","黄色", “红色”、“红色”、“红色”、“红色”、“红色”、“红色”、 "绿色","绿色","绿色","绿色","绿色","绿色", "蓝色","蓝色","蓝色","蓝色","蓝色","蓝色", "紫","紫","紫","紫","紫","紫", "黄色","黄色","黄色","黄色","黄色","黄色", “红色”、“红色”、“红色”、“红色”、“红色”、“红色”、 "绿色","绿色","绿色","绿色","绿色","绿色", "黄色","黄色","黄色","黄色","黄色","黄色", “红色”、“红色”、“红色”、“红色”、“红色”、“红色”、 "绿色","绿色","绿色","绿色","绿色","绿色", "黄","黄","黄","黄","黄","黄")

值=rep(c(9,8,7,6,5,8,7,6,5,4,7,6,5,4,3), times=6)

Fruit=rep(c("CApple", "Banana", "ABlueberry","Mango", "Melon", "梨"), 次 = c(18,18,12,18,18,6))

数据 %
mutate(unten=Values-0.2, oben=Values+0.2)

我的代码是:

ForPlot <-  ggplot(Data, aes(Colour, Values), fill=Colour)

design <- theme(strip.background = element_blank(),
                 panel.background = element_blank(),  
                 panel.grid.major = element_blank(),
                 strip.placement = "outside",
                 panel.border=element_blank(),
                 #     axis.line = element_line(colour = "black"),
                 axis.line.x = element_line(colour = "white"),
                 axis.ticks.x=element_blank(),
                 legend.title = element_blank(),
                 # axis.text=element_text(size=8),
                 axis.text.x=element_blank(),
                 axis.title=element_text(size=10),
                 strip.text = element_text(size = 10)) 

  ForPlot+
  geom_hline(yintercept = c(0, 2.5, 5, 7.5, 10), colour= "lightgrey" )+
  geom_col(aes(fill=Colour), 
           position=position_dodge2(preserve="single"))+
  ggtitle("Fruity Colours")+
  ylab("Values") + xlab("Day") +
  facet_wrap(~Fruit,  scales= "free", ncol=3)+
  scale_y_continuous()+
  scale_fill_discrete()+
  geom_text(aes(label = Day), position=position_dodge2(width= 0.9), 
            y=-0.2, size=2.4) +
  geom_errorbar(aes(ymin=pmax(0,unten), ymax=oben), 
                position=position_dodge2())+
  design+
  geom_segment(aes(x=0.4,xend=Inf, y=0, yend=0), color="black")+
  geom_segment(aes(x=0.4,xend=0.4, y=0, yend=Inf), colour="black")

目前,不同切面中的条都具有不同的宽度。我尝试使用“width=”来调整问题,但没有成功(条形图仅相对于它们当前的大小缩小,或者我失去了我的 Day 标签和误差线)。

感谢您的帮助!

【问题讨论】:

  • space 参数(在 facet_grid() 中可用,但在 facet_wrap() 中不可用)可能会有所帮助:facet_grid(~Fruit, scales= "free", space = "free")。这就是你想要的吗?条的宽度相同,但当然,刻面本身的大小也不同。

标签: r facet column-width geom-col


【解决方案1】:

指出@aosmith 的有用评论会有所帮助:

ForPlot+
  geom_hline(yintercept = c(0, 2.5, 5, 7.5, 10), colour= "lightgrey" )+
  geom_col(aes(fill=Colour), 
           position=position_dodge2(preserve="single"))+
  ggtitle("Fruity Colours")+
  ylab("Values") + xlab("Day") +
  facet_grid(.~Fruit,  scales= "free", space = 'free')+
  scale_y_continuous()+
  scale_fill_discrete()+
  geom_text(aes(label = Day), position=position_dodge2(width= 0.9), 
            y=-0.2, size=2.4) +
  geom_errorbar(aes(ymin=pmax(0,unten), ymax=oben), 
                position=position_dodge2())+
  design+
  geom_segment(aes(x=0.4,xend=Inf, y=0, yend=0), color="black")+
  geom_segment(aes(x=0.4,xend=0.4, y=0, yend=Inf), colour="black") 

更新:您可以使用theme(legend.position = 'top')在顶部放置图例获得更多空间@我使用了您的新数据:

更新 2:有了新数据,我更接近于你想要的东西(请注意 scale_fill_manual() 中的颜色),试试这个代码:

library(tidyverse)
library(patchwork)
#Create a split
Fruit <- unique(Data$Fruit)
Key <- sort(rep(c(1,2),length(Fruit)/2))
DKeys <- data.frame(Fruit,Key)

#Join data for splits
Data2 <- Data %>% left_join(DKeys)

#Now split
List <- split(Data2,Data2$Key)

#Plot function
myplot <- function(x)
{
  ForPlot <-  ggplot(x, aes(Colour, Values), fill=Colour)
  
  design <- theme(strip.background = element_blank(),
                  panel.background = element_blank(),  
                  panel.grid.major = element_blank(),
                  strip.placement = "outside",
                  panel.border=element_blank(),
                  #     axis.line = element_line(colour = "black"),
                  axis.line.x = element_line(colour = "white"),
                  axis.ticks.x=element_blank(),
                  legend.title = element_blank(),
                  # axis.text=element_text(size=8),
                  axis.text.x=element_blank(),
                  axis.title=element_text(size=10),
                  strip.text = element_text(size = 10),
                  legend.position = 'top') 
  
  
  ForPlot <- ForPlot+
    geom_hline(yintercept = c(0, 2.5, 5, 7.5, 10), colour= "lightgrey" )+
    geom_col(aes(fill=Colour), 
             position=position_dodge2(preserve="single"))+
    ylab("Values") + xlab("Day") +
    facet_grid(.~Fruit,  scales= "free", space = 'free')+
    scale_y_continuous()+
    # scale_fill_discrete()+
    geom_text(aes(label = Day), position=position_dodge2(width= 0.9), 
              y=-0.2, size=2.4) +
    geom_errorbar(aes(ymin=pmax(0,unten), ymax=oben), 
                  position=position_dodge2())+
    design+
    geom_segment(aes(x=0.4,xend=Inf, y=0, yend=0), color="black")+
    geom_segment(aes(x=0.4,xend=0.4, y=0, yend=Inf), colour="black")+
    scale_fill_manual(values=c(Blue='Blue',Green='Green',Purple='Purple',Red='Red',Yellow='Yellow'))
  return(ForPlot)
}

#Apply to plots
List2 <- lapply(List,myplot)
#Format 
List2[2:length(List2)] <- lapply(List2[2:length(List2)], function(x) {x<-x+theme(legend.position = 'none')})
names(List2) <- paste0('G',1:length(List))
list2env(List2,envir = .GlobalEnv)
#Arrange plots
G1/G2+ plot_annotation(title = 'Fruity Colours')

【讨论】:

  • 您好,感谢您的快速回复。抱歉,我的数据集不够完善(现在已调整)。我有足够的数据,它必须显示在两行中,同时只针对一个变量。有没有办法使用分面网格来完成这项工作?
  • @BeccaLi 你可以在末尾添加+theme(legend.position = 'top')。它会将图例放在顶部,我检查了情节,你有更多的空间。这对你想要的有效吗?
  • 很遗憾,这只是一个示例数据集。实际的 Data 仍然有太多的 bar 需要并排放置(15 个组,每个组有 6 个 bar,这些组分布到 6 个方面:2,3,3,3,3,1)
  • @BeccaLi 明白了。我可以建议你尝试分成两个不同的地块,然后你可以拼凑起来。如果您可以将您提到的所有组添加到示例数据中,我可以为您做。
  • 非常感谢您的帮助!我再次调整了样本集。