【问题标题】:How to put ggplot2 legend in two columns for an area plot如何将ggplot2图例放在区域图的两列中
【发布时间】:2014-12-31 15:56:41
【问题描述】:

我想把一个很长的图例分成两列,但我没有成功。这是我与在其他地方找到的解决方案一起使用的代码,它不适用于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!

请注意,creating columns within a legend list while using ggplot in R code 中的解决方案已包含在上面,不幸的是它不起作用!

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    您指的是错误的指南。

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

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 2020-02-28
      • 2018-02-07
      • 1970-01-01
      • 2022-09-29
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多