用共享和不共享的轴比例混合对齐多个图

时间:2019-09-16 17:49:05

标签: r ggplot2 gridextra facet-wrap grob

我想要类似下图的内容,但是,我不知道如何使第四行和第五行中的图与前三行中的图垂直对齐。第4行中的图比上面的图略窄,导致左右边框未对齐。第五行中的第一个图相对于上面的图非常狭窄,我希望第五行中的后三个图与第3、4和5列中的图对齐,但它们向右移一点。我使用了grid.arrange和facet_wrap,但是不知道这是否是解决方案。 Graphic

library(RColorBrewer)
library(ggplot2)
library(tidyr)
library(dplyr)
library(gridExtra)

set.seed(1)
wide.df <- data.frame(
  group = rep(letters[1:4],c(18,4,1,3)),
  label.name = c("Coniferous","Oak forest","Decid./conif.","Pine barren","Ruderal forest","Ruderal grass/shrub","Agriculture","Arid vacant land","Residential","Urban","Lake","Pond","Emergent wl","Estuar. marine deep","Estuar. marine wl","Forest/shrub wl","Salt marsh","Beach","Core forest","Perforated forest","Patch forest","Edge forest","Elevation", "Eastness","Northness","Slope"),
  points = c(runif(18,min=0,max=0.3),runif(4,0,.4),runif(1,40,60),runif(1,0,0.04),runif(1,-0.04,0),runif(1,0,0.04)),
  state = c(runif(18,min=0,max=0.3),runif(4,0,.4),runif(1,40,60),runif(1,0,0.04),runif(1,-0.04,0),runif(1,0,0.04))
)

df_long <- wide.df %>% gather(group,proportion,3:4)

brewer.pal(6,"Set1")
adds.browns <- brewer.pal(10,"BrBG")[1:4]
adds.purples <- rev(brewer.pal(9,"BuPu")[6:9])
adds.greens <- rev(brewer.pal(6,"Greens"))
adds.greens[5:6] <- c("yellowgreen","yellow4")
adds.wetlands <- c("deepskyblue","royalblue4", "darkturquoise","midnightblue","darkslateblue","darkseagreen4","darkcyan")#,"aquamarine4" ,"darkslategray4""mediumspringgreen","darkseagreen4","darkseagreen2")
adds.ag.urban <- c("darkorange1","grey","firebrick","red")
colors.v <- c( adds.greens,adds.ag.urban,adds.wetlands,"cornsilk3",adds.purples,adds.browns)

#landcover
colors.lc <- c(adds.greens,adds.ag.urban,adds.wetlands,"cornsilk3")
df_long.lc <- df_long[-which(df_long$label.name %in% c("Core forest","Perforated forest","Patch forest","Edge forest","Elevation","Eastness","Northness","Slope")),]
df_long.lc$label.name <- droplevels(df_long.lc$label.name)
lc.plot <- ggplot(df_long.lc,aes(fill=label.name,y=proportion,x=group))+
  geom_bar(position="dodge",stat="identity")+
  scale_fill_manual(values = colors.lc)+
  facet_wrap(~label.name,ncol = 6)+
  theme_bw()+
  theme(legend.position = "none",strip.text.x=element_text(size=7),
        axis.text=element_text(size=7))+
  ylab(NULL)+
  xlab(NULL)

#frag
colors.frag <- adds.purples
df_long.frag <- df_long[which(df_long$label.name %in% c("Core forest","Perforated forest","Patch forest","Edge forest")),]
df_long.frag$label.name <- droplevels(df_long.frag$label.name)
frag.plot<-ggplot(df_long.frag,aes(fill=label.name,y=proportion,x=group))+
  geom_bar(position="dodge",stat="identity")+
  scale_fill_manual(values = colors.frag)+
  facet_wrap(~label.name,ncol = 6)+
  theme_bw()+
  theme(legend.position = "none",strip.text.x=element_text(size=7),
        axis.text=element_text(size=7))+
  ylab(NULL)+
  xlab(NULL)

#topo
colors.topo <- adds.browns[-1]
df_long.topo <- df_long[which(df_long$label.name %in% c("Eastness","Northness","Slope")),]
df_long.topo$label.name <- droplevels(df_long.topo$label.name)
str(df_long.topo)
topo.plot<-ggplot(df_long.topo,aes(fill=label.name,y=proportion,x=group))+
  geom_bar(position="dodge",stat="identity")+
  scale_fill_manual(values = colors.topo)+
  facet_wrap(~label.name,ncol = 6)+
  theme_bw()+
  theme(legend.position = "none",strip.text.x=element_text(size=7),
        axis.text=element_text(size=7))+
  ylab(NULL)+
  xlab(NULL)

#elev
color.elev <- adds.browns[1]
df_long.elev <- df_long[which(df_long$label.name %in% c("Elevation")),]
df_long.elev$label.name <- droplevels(df_long.elev$label.name)
str(df_long.elev)
elev.plot<-ggplot(df_long.elev,aes(fill=label.name,y=proportion,x=group))+
  geom_bar(position="dodge",stat="identity")+
  scale_fill_manual(values = color.elev)+
  facet_wrap(~label.name,ncol = 6)+
  theme_bw()+
  theme(legend.position = "none",strip.text.x=element_text(size=7),
        axis.text=element_text(size=7))+
  ylab(NULL)+
  xlab(NULL)

twoplots <- grid.arrange(lc.plot,frag.plot,topo.plot,elev.plot, heights=c(3,1,1),nrow = 3,
                         layout_matrix=rbind(c(1,1,1,1,1,1),c(2,2,2,2,NA,NA),c(4,NA,3,3,3,NA)))

0 个答案:

没有答案