[英]How to put ggplot2 legend in two columns for an area plot
我想把一個長長的傳說分成兩列,但我沒有取得任何成功。 這是我在其他地方找到的解決方案所使用的代碼,它不適用於geom='area'
,盡管它適用於我的其他情節。 我從下面的代碼得到的情節如下:
那么如何用兩列中的圖例繪制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!
請注意, 在R代碼中使用ggplot時在圖例列表中創建列的解決方案已在上面合並,但不幸的是它不起作用!
你指的是錯誤的指南。
Q1 + guides(fill=guide_legend(ncol=2))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.