guide_legend和ggplot2,格式为nrow

时间:2014-09-02 11:06:20

标签: r ggplot2

我正在尝试在ggplot上格式化一个超长的图例,以便最大值没有。的行。我已经阅读了所有可以找到的文档,尤其是:http://docs.ggplot2.org/0.9.3.1/guide_legend.html但由于某些原因,图例不会格式化。

我在下面使用quakes数据集给出了一个可重现的样本,并将列站转换为字符,以便它们单独绘制(否则,它们似乎绘制为组)。

plotquakes <- function(magreq) {
    library(ggplot2)
    magdata <- subset(quakes, mag > magreq)
    magdata$stations <- as.character(magdata$stations)
    g <- ggplot(magdata, aes (x = lat, y = long))
    g + geom_point(aes(alpha = stations), fill = "black", pch=21, size = 6) + 
    labs(x = "Latitude", y = "Longitude") + 
    geom_vline(xintercept = 0, col = "red") + 
    geom_hline(yintercept = 0, col = "red") +
    guides(col = guide_legend(nrow = 16))
}

plotquakes(5)

我得到的是这个:

enter image description here

虽然我希望图例中每列最多包含16个数据字段。

1 个答案:

答案 0 :(得分:3)

您正在更改错误的指南。

plotquakes <- function(magreq) {
  library(ggplot2)
  magdata <- subset(quakes, mag > magreq)
  magdata$stations <- as.character(magdata$stations)
  g <- ggplot(magdata, aes (x = lat, y = long))
  g + geom_point(aes(alpha = stations), fill = "black", pch=21, size = 6) + 
    labs(x = "Latitude", y = "Longitude") + 
    geom_vline(xintercept = 0, col = "red") + 
    geom_hline(yintercept = 0, col = "red") +
    guides(alpha = guide_legend(nrow = 16)) #note it's alpha not col
}

plotquakes(5)

resulting plot