简体   繁体   English

如何将ggplot2图例放在区域图的两列中

[英]How to put ggplot2 legend in two columns for an area plot

I would like to put a long legend into two columns and I am not having any success. 我想把一个长长的传说分成两列,但我没有取得任何成功。 Here's the code that I'm using with the solution found elsewhere which does not work for geom='area' , though it works for my other plots. 这是我在其他地方找到的解决方案所使用的代码,它不适用于geom='area' ,尽管它适用于我的其他情节。 The plot that I do get from the code below looks like: 我从下面的代码得到的情节如下: 单列图例

So how do I plot Q1 with the legend in two columns please? 那么如何用两列中的图例绘制Q1

NVER <- 10
NGRID <- 20
MAT <- matrix(NA, nrow=NVER, ncol=NGRID)
gsd <- 0.1  # standard deviation of the Gaussians

verlocs <- seq(from=0, to=1, length.out=NVER)
thegrid <- seq(from=0, to=1, length.out=NGRID)
# create a mixture of Gaussians with modes spaced evenly on 0 to 1
# i.e. the first mode is at 0 and the last mode is at 1
for (i in 1:NVER) {
  # add the shape of gaussian i
  MAT[i,] <- dnorm(thegrid, verlocs[[i]], sd=gsd)
}
M2 <- MAT/rowSums(MAT)
colnames(M2) <- as.character(thegrid)
# rownames(M2) <- as.character(verlocs)
library(reshape2)
D2 <- melt(M2)
# head(D2)
# str(D2)
D2$Var1 <- ordered(D2$Var1)
library(ggplot2)
Q1 <- qplot(Var2, value, data=D2, order=Var1, fill=Var1, geom='area')
Q1
# ggsave('sillyrainbow.png')
# now try the stackoverflow guide() solution
Q1 + guides(col=guide_legend(ncol=2))  # try but fail to put the legend in two columns!

Note that the solution in creating columns within a legend list while using ggplot in R code is incorporated above and it does not work unfortunately! 请注意, 在R代码中使用ggplot时在图例列表创建列的解决方案已在上面合并,但不幸的是它不起作用!

You are referring to the wrong guide. 你指的是错误的指南。

Q1 + guides(fill=guide_legend(ncol=2))

结果情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM