R 使用gtable定位图例

R 使用gtable定位图例,r,ggplot2,legend,gtable,R,Ggplot2,Legend,Gtable,我做了下面的情节,由多个情节组成。我对它很满意,但是传说开始于底线的中间,我想把它放在左边。 然而,我不知道该怎么做。这就是我在图表下放置图例的方式 # extract legend leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]] leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]] g$grobs[[which(g

我做了下面的情节,由多个情节组成。我对它很满意,但是传说开始于底线的中间,我想把它放在左边。

然而,我不知道该怎么做。这就是我在图表下放置图例的方式

# extract legend
leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]

g$grobs[[which(g$layout$name == "guide-box")]] <- 
  gtable:::cbind_gtable(leg1, leg2, "first")
grid.draw(g)
#提取图例

leg1你很接近。我认为你的问题在于你在第二个情节中定位了这个传奇,而这个定位一直延续到合并的传奇,因此把它搞糟了。此外,图例标题应位于顶部,然后合并的图例需要更多的空间。你的第一个情节很好。从第二个图中提取:

# Making adjustments to legend: adjusting position and title position
l2 <- ggplot(foo.long, aes(out121.perc,value,fill=variable))+
  geom_bar(position="stack",stat="identity")+
  scale_fill_manual(values=cbbPalette,name = "% of irrigation",
                    labels = c("0-10% irrigation", "10-20% irrigation", "20-30% irrigation", 
                               "30-40% irrigation", "40-50% irrigation", "50-60% irrigation",
                               "60-70% irrigation", "70-80% irrigation", "80-90% irrigation",
                               "90-100% irrigation"))+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
                 labels=c("0", "25", "50","75","100")) +
  scale_y_continuous(name="number of farms",limits = c(0, 10000), breaks=c(0, 1000,2000,3000, 4000))+
  theme(legend.position="bottom", panel.background = element_rect(fill = NA),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+
  guides(fill=guide_legend(title="Number of irrigated farms",ncol=3, title.position = "top"))

l2

# ggplotGrob

g1 <- ggplotGrob(l2)
g2 <- ggplotGrob(l66)


# Add plots together
pp <- c(subset(g2$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g2, g1$grobs[[which(g1$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

# Add second axis for accuracy
ia <- which(g1$layout$name == "axis-l")
ga <- g1$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


# Add second y-axis title 
ia <- which(g1$layout$name == "ylab")
ax <- g1$grobs[[ia]]
# str(ax) # you can change features (size, colour etc for these - 
# change rotation below 
ax$rot <- 270
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


# extract legend
leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]

legc = gtable:::cbind_gtable(leg1, leg2, "first")


g$grobs[[which(g$layout$name == "guide-box")]] <- 
  gtable:::cbind_gtable(leg1, leg2, "first")
grid.draw(g)  # Note: Legend does not fit

g$heights[[6]] = unit(5, "cm")   # Add more space for the legend - can adjust this to suit
grid.draw(g)
#调整图例:调整位置和标题位置

l2您应该提供某种类型的。很难猜测您在这里引用的变量中可能包含什么。我们不需要您的实际数据或复杂的格式代码,只是一些简单的,我们可以测试。没问题,我会添加它现在!我添加了一个数据样本+我用来制作图形的完整代码,非常感谢!我想我自己不会找到的。我真的很感激你的回答!
library(ggplot2)
library(gtable)
library(reshape2)

# line plot
out121$perc1<-c(1:999)
l64<-ggplot(out121,aes(perc1))
l65<-l64+geom_line(aes(y=MEt_R,colour="Rainfed"),size=1.3)+
  geom_line(aes(y=MEt_R+se_MEt_Rainfed,colour="Rainfed range"),size=0.7)+
  geom_line(aes(y=MEt_R-se_MEt_Rainfed,colour="Rainfed range"),size=0.7)+
  geom_line(aes(y=MEt_Irr,colour="Irrigation"),size=1.3)+
  geom_line(aes(y=MEt_Irr+se_MEt_Irrigation,colour="Irrigation range"),size=0.7)+
  geom_line(aes(y=MEt_Irr-se_MEt_Irrigation,colour="Irrigation range"),size=0.7)+
  scale_colour_manual(values=c("blue3","mediumslateblue","green3","green"), name="")+
  scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
                   labels=c("0", "25", "50","75","100")) +
  scale_y_continuous(name="MEt",limits = c(-0.7, 0.5),breaks=c(-0.3,-0.1,0,0.1,0.3,0.5))

l66<-l65+ theme_bw()+ggtitle("subsidies 1 large") + 
  theme(plot.title = element_text(lineheight=.8, face="bold"),legend.position="bottom")+
  guides(col=guide_legend(ncol=2))+
  theme(panel.background = element_rect(fill = NA),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())
l66


# bar plot

test  <- data.frame(out121$perc,out121$Irrigation10,out121$Irrigation20,out121$Irrigation30,out121$Irrigation40,
                    out121$Irrigation50,out121$Irrigation60,out121$Irrigation70,out121$Irrigation80,
                    out121$Irrigation90,out121$Irrigation100)

# barplot(as.matrix(test))
library(reshape2)
foo.long<-melt(test)

foo.long$out121.perc<- as.character(foo.long$out121.perc)
foo.long$out121.perc <- factor(foo.long$out121.perc, levels=unique(foo.long$out121.perc))

cbbPalette <- c("yellow","greenyellow","#00FF00", "#00C639","#00AA55", "#00718E", "#0055AA", "#001CE3","blue4","midnightblue")

l2<-ggplot(foo.long, aes(out121.perc,value,fill=variable))+
  geom_bar(position="stack",stat="identity")+
  scale_fill_manual(values=cbbPalette,name = "% of irrigation",
                    labels = c("0-10% irrigation", "10-20% irrigation", "20-30% irrigation", 
                               "30-40% irrigation", "40-50% irrigation", "50-60% irrigation",
                               "60-70% irrigation", "70-80% irrigation", "80-90% irrigation",
                               "90-100% irrigation"))+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
                 labels=c("0", "25", "50","75","100")) +
  scale_y_continuous(name="number of farms",limits = c(0, 10000), breaks=c(0, 1000,2000,3000, 4000))+
  theme(legend.position=c(0.7,0.4),panel.background = element_rect(fill = NA),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+
  guides(fill=guide_legend(title="Number of irrigated farms",ncol=3))

l2

# ggplotGrob

g1 <- ggplotGrob(l2)
g2 <- ggplotGrob(l66)


# Add plots together
pp <- c(subset(g2$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g2, g1$grobs[[which(g1$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

# Add second axis for accuracy
ia <- which(g1$layout$name == "axis-l")
ga <- g1$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


# Add second y-axis title 
ia <- which(g1$layout$name == "ylab")
ax <- g1$grobs[[ia]]
# str(ax) # you can change features (size, colour etc for these - 
# change rotation below 
ax$rot <- 270
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


# extract legend
leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]

g$grobs[[which(g$layout$name == "guide-box")]] <- 
  gtable:::cbind_gtable(leg1, leg2, "first")
grid.draw(g)
# Making adjustments to legend: adjusting position and title position
l2 <- ggplot(foo.long, aes(out121.perc,value,fill=variable))+
  geom_bar(position="stack",stat="identity")+
  scale_fill_manual(values=cbbPalette,name = "% of irrigation",
                    labels = c("0-10% irrigation", "10-20% irrigation", "20-30% irrigation", 
                               "30-40% irrigation", "40-50% irrigation", "50-60% irrigation",
                               "60-70% irrigation", "70-80% irrigation", "80-90% irrigation",
                               "90-100% irrigation"))+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
                 labels=c("0", "25", "50","75","100")) +
  scale_y_continuous(name="number of farms",limits = c(0, 10000), breaks=c(0, 1000,2000,3000, 4000))+
  theme(legend.position="bottom", panel.background = element_rect(fill = NA),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+
  guides(fill=guide_legend(title="Number of irrigated farms",ncol=3, title.position = "top"))

l2

# ggplotGrob

g1 <- ggplotGrob(l2)
g2 <- ggplotGrob(l66)


# Add plots together
pp <- c(subset(g2$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g2, g1$grobs[[which(g1$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

# Add second axis for accuracy
ia <- which(g1$layout$name == "axis-l")
ga <- g1$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


# Add second y-axis title 
ia <- which(g1$layout$name == "ylab")
ax <- g1$grobs[[ia]]
# str(ax) # you can change features (size, colour etc for these - 
# change rotation below 
ax$rot <- 270
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


# extract legend
leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]

legc = gtable:::cbind_gtable(leg1, leg2, "first")


g$grobs[[which(g$layout$name == "guide-box")]] <- 
  gtable:::cbind_gtable(leg1, leg2, "first")
grid.draw(g)  # Note: Legend does not fit

g$heights[[6]] = unit(5, "cm")   # Add more space for the legend - can adjust this to suit
grid.draw(g)