0

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

数据集:

天=代表(1:6,次=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", "Pear"), times = c(18,18,12,18,18,6))

数据 <-data.frame(Day, Fruit, Colour, Values) %>%
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 标签和误差线)。

帮助表示赞赏!

4

1 回答 1

2

指出@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')

在此处输入图像描述

于 2020-07-31T17:06:05.367 回答