guide_legend和ggplot2,格式为行 [英] guide_legend and ggplot2, format nrow
问题描述
我正在尝试在ggplot上格式化一个超长的图例,以使最大编号不存在.行.我已经阅读了所有可以找到的文档,尤其是: http://docs.ggplot2.org/0.9.3.1/guide_legend.html ,但由于某些原因,图例将无法格式化.
I am trying to format an over-long legend on a ggplot so that there is a maximum no. of rows. I've read all the documentation that I could find, especially this: http://docs.ggplot2.org/0.9.3.1/guide_legend.html but for some reason, the legend will not format.
我在下面使用地震数据集提供了可重现的样本,并将列测站转换为字符,以便它们分别绘制(否则,它们似乎作为组绘制).
I've given a reproducible sample below using the quakes dataset, and converted the column stations to character so that they plot individually (otherwise, they seem to plot as groups).
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)
我得到的是这样的:
而我想在图例中每列最多包含16个数据字段.
whereas I would like to have a maximum of 16 data fields per column in the legend.
推荐答案
您正在更改错误的指南.
You are changing the wrong guide.
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)
这篇关于guide_legend和ggplot2,格式为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!