本文介绍了如何在双y轴ggplot上显示图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图使用ggplot组成一个双y轴图表。首先让我说,我不是在寻求讨论是否是好做法的优点。我发现它们在查看基于时间的数据以确定2个离散变量的趋势时特别有用。对此的进一步讨论更适合在我看来交叉验证。 Kohske 提供了一个很好的例子,如何做到这一点,我已经使用到目前为止效果很大。然而,我在我的极限,包括一个传说为两个y轴。我也看到过类似的问题此处和这里,但似乎没有解决包括图例的问题。 我有一个可重现的例子使用ggplot的钻石数据集。 数据 库(ggplot2 ) library(gtable) library(grid) library(data.table) library(scales) grid.newpage $ b dt.diamonds< - as.data.table(diamonds) d1 图表 p1 geom_bar(stat =identity)+ labs scale_fill_identity(name =,guide =legend,labels = c(Revenue))+ scale_y_continuous(labels = dollar,expand = c(0,0))+ theme(axis.text.x = element_text(angle = 90,hjust = 1), axis.text.y = element_text(color =#4B92DB), legend.position =bottom ) p2< - ggplot(d1,aes(x = clarity,y = stones,color =red))+ geom_point(size = 6)+ labs(x =,y =number of stones)+ expand_limits(y = 0)+ scale_y_continuous(labels = comma,expand = c(0,0))+ scale_colour_manual name ='',values = c(red,green),labels = c(Number of Stones)+ theme(axis.text.y = element_text(color =red) )+ theme(panel.background = element_rect(fill = NA), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_rect(fill = NA,color =grey50), legend.position =bottom) #extract gtable g1< - ggplot_gtable (ggplot_build(p1)) g2 pp g pp $ l,pp $ b,pp $ l)#轴调整 ia< - which(g2 $ layout $ name ==axis-l) ga& $ grobs [[ia]] ax< - ga $ children [[2]] ax $ wides< - rev(ax $ wides) ax $ grobs< - rev (ax $ grobs) ax $ grobs [[1]] $ x g< - gtable_add_cols(g,g2 $ wides [g2 $ layout [ia,] $ l],length(g $ width) - 1)g< - gtable_add_grob(g,ax,pp $ t,length(g $ width) - 1,pp $ b)#draw it grid.draw(g) 问题:有没有人知道如何获得图例的第二部分? 以下是按照p1,p2,p1和p2组合生成的图表,您会注意到p2的图例未显示在组合图表中。 p1 p2 组合p1& p2 解决方案类似于上面使用的技术,您可以提取图例,绑定它们,然后使用它们覆盖图例。 从开始 #extract legend leg1 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) I am trying to compose a dual y-axis chart using ggplot. Firstly let me say that I am not looking for a discussion on the merits of whether or not it is good practice to do so. I find them to be particularly useful when looking at time based data to identify trends in 2 discrete variables. A further discussion of this is better suited to crossvalidated in my opinion.Kohske provides a very good example of how to do it, which I have used to great effect so far. I am however at my limits to include a legend for both y-axes. I have also seen similar questions here and here but none seem to address the issue of including a legend.I've got a reproduceable example using the diamonds dataset from ggplot. Datalibrary(ggplot2)library(gtable)library(grid)library(data.table)library(scales)grid.newpage()dt.diamonds <- as.data.table(diamonds)d1 <- dt.diamonds[,list(revenue = sum(price), stones = length(price)), by=clarity]setkey(d1, clarity)Chartsp1 <- ggplot(d1, aes(x=clarity,y=revenue, fill="#4B92DB")) + geom_bar(stat="identity") + labs(x="clarity", y="revenue") + scale_fill_identity(name="", guide="legend", labels=c("Revenue")) + scale_y_continuous(labels=dollar, expand=c(0,0)) + theme(axis.text.x = element_text(angle = 90, hjust = 1), axis.text.y = element_text(colour="#4B92DB"), legend.position="bottom")p2 <- ggplot(d1, aes(x=clarity, y=stones, colour="red")) + geom_point(size=6) + labs(x="", y="number of stones") + expand_limits(y=0) + scale_y_continuous(labels=comma, expand=c(0,0)) + scale_colour_manual(name = '',values =c("red","green"), labels = c("Number of Stones"))+ theme(axis.text.y = element_text(colour = "red")) + theme(panel.background = element_rect(fill = NA), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_rect(fill=NA,colour="grey50"), legend.position="bottom")# extract gtableg1 <- ggplot_gtable(ggplot_build(p1))g2 <- ggplot_gtable(ggplot_build(p2))pp <- c(subset(g1$layout, name == "panel", se = t:r))g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, pp$l, pp$b, pp$l)# axis tweaksia <- which(g2$layout$name == "axis-l")ga <- g2$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, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)# draw itgrid.draw(g)QUESTION: Does anyone have some tips on how to get the 2nd part of the legend to show?The following are the charts produced in order p1, p2, combined p1&p2, you'll notice that the legend for p2 doesn't show in the combined chart.p1p2combined p1 & p2 解决方案 Similar to the technique you use above you can extract the legends, bind them and then overwrite the plot legend with them.So starting from # draw it in your code# extract legendleg1 <- 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) 这篇关于如何在双y轴ggplot上显示图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 15:37