Rstudio:在构面中调整分组条形图的条宽
Rstudio: Adjusting bar width of grouped bar plot in facets
我有一个数据集,我试图将其绘制为构面中的分组列,其中列的大小相等。
数据集:
Day=rep(1:6, times=15)
Colour=c("Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Blue","Blue","Blue","Blue","Blue","Blue",
"Purple","Purple","Purple","Purple","Purple","Purple",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow")
Values=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 <-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 标签和错误条)。
感谢帮助!
指向@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")
更新: 你可以获得更多 space placing legend on top with 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')
我有一个数据集,我试图将其绘制为构面中的分组列,其中列的大小相等。
数据集:
Day=rep(1:6, times=15)
Colour=c("Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow", "Red", "Red","Red","Red","Red","Red", "Green","Green","Green","Green","Green","Green", "Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow", "Red", "Red","Red","Red","Red","Red", "Green","Green","Green","Green","Green","Green", "Blue","Blue","Blue","Blue","Blue","Blue", "Purple","Purple","Purple","Purple","Purple","Purple", "Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow", "Red", "Red","Red","Red","Red","Red", "Green","Green","Green","Green","Green","Green", "Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow", "Red", "Red","Red","Red","Red","Red", "Green","Green","Green","Green","Green","Green", "Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow")
Values=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 <-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 标签和错误条)。
感谢帮助!
指向@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")
更新: 你可以获得更多 space placing legend on top with 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')