更改 ggplot 的分面标题格式 facet_wrap
Change facet title format for ggplot facet_wrap
使用以下代码绘制由以下数据生成的图:
library(dplyr)
dd<-data.frame(matrix(0,36,1))
dd$drug<-c("x","x","x","x","x","x","x","x","x","y","y","y","y","y","y","y","y","y","z","z","z","z","z","z","z","z","z","w","w","w","w","w","w","w","w","w")
dd$prev<-c(10,11,12,11,12,13,12,13,14,10,11,12,11,12,13,12,13,14,10,11,12,11,12,13,12,13,14,10,11,12,11,12,13,12,13,14)
dd$country<-c("a","a","a","b","b","b","c","c","c","a","a","a","b","b","b","c","c","c","a","a","a","b","b","b","c","c","c","a","a","a","b","b","b","c","c","c")
dd$year<-c(15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17)
dd<-dd[,-1]
dd <- dd %>%
group_by(drug) %>%
arrange(country) %>%
ungroup()
ggplot(dd,aes(x=factor(year),y=prev,fill = factor(country,levels=c("a","b","c")),pos="dodge"))+
geom_col(col = "black",size=0.25,pos=position_dodge(0.9)) +
facet_wrap(~factor(drug, levels(factor(dd$drug))[c(2,1,3,4)]),strip.position="bottom",as.table=T,scales="free_x")+
scale_fill_manual(values = c("a" = "white",
"b" = "lightskyblue2",
"c" = "lightpink"),
guide = F)+
scale_x_discrete(name="Year",labels=c(2015,2016,2017))+
scale_y_continuous(name="Prevalence (%)")+
theme_classic()
我想移动,但找不到方法:
1- 将年份移动到每个方面的 x-axis 上面的方框与方面标题
2-删除构面标题周围的框
3- 增加 2 个顶面和 2 个底面之间的 space
结果应该大致如下所示:
非常感谢!!!
马可
检查 ?theme
并阅读 strip.*
的所有选项。相关的是位置和背景。对于间距,有 panel.spacing
.
ggplot(mtcars, aes(factor(am))) +
geom_bar() +
facet_wrap(~cyl, strip.position = "bottom", nrow = 2, scales = "free") +
theme_classic() +
theme(strip.placement = "outside",
strip.background = element_blank(),
panel.spacing.y = unit(2, "lines"))
使用以下代码绘制由以下数据生成的图:
library(dplyr)
dd<-data.frame(matrix(0,36,1))
dd$drug<-c("x","x","x","x","x","x","x","x","x","y","y","y","y","y","y","y","y","y","z","z","z","z","z","z","z","z","z","w","w","w","w","w","w","w","w","w")
dd$prev<-c(10,11,12,11,12,13,12,13,14,10,11,12,11,12,13,12,13,14,10,11,12,11,12,13,12,13,14,10,11,12,11,12,13,12,13,14)
dd$country<-c("a","a","a","b","b","b","c","c","c","a","a","a","b","b","b","c","c","c","a","a","a","b","b","b","c","c","c","a","a","a","b","b","b","c","c","c")
dd$year<-c(15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17,15,16,17)
dd<-dd[,-1]
dd <- dd %>%
group_by(drug) %>%
arrange(country) %>%
ungroup()
ggplot(dd,aes(x=factor(year),y=prev,fill = factor(country,levels=c("a","b","c")),pos="dodge"))+
geom_col(col = "black",size=0.25,pos=position_dodge(0.9)) +
facet_wrap(~factor(drug, levels(factor(dd$drug))[c(2,1,3,4)]),strip.position="bottom",as.table=T,scales="free_x")+
scale_fill_manual(values = c("a" = "white",
"b" = "lightskyblue2",
"c" = "lightpink"),
guide = F)+
scale_x_discrete(name="Year",labels=c(2015,2016,2017))+
scale_y_continuous(name="Prevalence (%)")+
theme_classic()
我想移动,但找不到方法:
1- 将年份移动到每个方面的 x-axis 上面的方框与方面标题 2-删除构面标题周围的框 3- 增加 2 个顶面和 2 个底面之间的 space
结果应该大致如下所示:
非常感谢!!! 马可
检查 ?theme
并阅读 strip.*
的所有选项。相关的是位置和背景。对于间距,有 panel.spacing
.
ggplot(mtcars, aes(factor(am))) +
geom_bar() +
facet_wrap(~cyl, strip.position = "bottom", nrow = 2, scales = "free") +
theme_classic() +
theme(strip.placement = "outside",
strip.background = element_blank(),
panel.spacing.y = unit(2, "lines"))