ggplot2 中两个离散尺度的图例
Legends for two discrete scales in ggplot2
我有想要沿两个维度绘制的数据:VK(离散)和 relPrec(连续)
另外,我有两个维度的属性:
type <- c("relPrecAir", "relPrecInv", "relPrectotal")
p <- c(0.9, 0.95, 0.99)
我想用形状表示 type
,用颜色表示 p
。我设法使用以下代码做到这一点:
precisionTable <- structure(list(relPrecTotal = c(0, 0, 0, 0.0070300473755567, 0.00842647198124317, 0.0112374693913644), relPrecAir = c(0, 0, 0, 0.00700370093237813, 0.00839489217055491, 0.011195354831821), relPrecInv = c(0, 0, 0, 0.0108917034179065, 0.0130551941937278, 0.0174102928813975), p = c(0.9, 0.95, 0.99, 0.9, 0.95, 0.99), VK = c(0, 0, 0, 1.15, 1.15, 1.15), case = c("demandStochastic", "demandStochastic", "demandStochastic", "demandStochastic", "demandStochastic", "demandStochastic")), .Names = c("relPrecTotal", "relPrecAir", "relPrecInv", "p", "VK", "case"), row.names = c(NA, -6L), class = "data.frame")
ggplot(precisionTable, aes(VK, relPrecTotal, color=as.factor(p))) + geom_line() + geom_point(shape=19) +
geom_line(aes(VK,relPrecAir, color=as.factor(p))) + geom_point(aes(VK,relPrecAir), shape=17) +
geom_line(aes(VK,relPrecInv, color=as.factor(p))) + geom_point(aes(VK,relPrecInv), shape=15) +
scale_color_discrete(name="p")
为 p
显示了一个图例。但是我无法用形状值为 type
绘制第二个图例。我该如何添加它?
您可以 melt
您的 data.frame 以避免 ggplot
命令中的多个步骤,并创建一个您将在其中映射的新变量(此处 ptShape
)单个 geom_point
调用的 aes
:
library(reshape2)
pmelt = melt(precisionTable, id.vars=c('VK', 'p', 'case'))
pmelt$ptShape = rep(c(19,17,15), each=6)
ggplot(pmelt, aes(x=VK, y=value)) +
geom_line(aes(colour=as.factor(p), shape=as.factor(ptShape))) +
geom_point(aes(colour=as.factor(p), shape=as.factor(ptShape)), size=5)
我们可以通过为参数 relPrecTotal
、relPrecAir
和 relPrecInv
添加新的分组来处理它,然后使用 ggplot 继续绘制它。
编辑 覆盖颜色图例中的形状
library(reshape2)
precisionTable2 <- melt(precisionTable, measure.vars = c("relPrecTotal", "relPrecAir", "relPrecInv"))
ggplot(precisionTable2, aes(VK, value)) +
geom_line(aes(color=as.factor(p), shape=as.factor(variable))) +
geom_point(aes(color=as.factor(p),shape=as.factor(variable))) +
scale_color_discrete(name='p') +
scale_shape_discrete(name='relPrec') +
guides(color=guide_legend(override.aes = list(shape=NA)))
我有想要沿两个维度绘制的数据:VK(离散)和 relPrec(连续)
另外,我有两个维度的属性:
type <- c("relPrecAir", "relPrecInv", "relPrectotal")
p <- c(0.9, 0.95, 0.99)
我想用形状表示 type
,用颜色表示 p
。我设法使用以下代码做到这一点:
precisionTable <- structure(list(relPrecTotal = c(0, 0, 0, 0.0070300473755567, 0.00842647198124317, 0.0112374693913644), relPrecAir = c(0, 0, 0, 0.00700370093237813, 0.00839489217055491, 0.011195354831821), relPrecInv = c(0, 0, 0, 0.0108917034179065, 0.0130551941937278, 0.0174102928813975), p = c(0.9, 0.95, 0.99, 0.9, 0.95, 0.99), VK = c(0, 0, 0, 1.15, 1.15, 1.15), case = c("demandStochastic", "demandStochastic", "demandStochastic", "demandStochastic", "demandStochastic", "demandStochastic")), .Names = c("relPrecTotal", "relPrecAir", "relPrecInv", "p", "VK", "case"), row.names = c(NA, -6L), class = "data.frame")
ggplot(precisionTable, aes(VK, relPrecTotal, color=as.factor(p))) + geom_line() + geom_point(shape=19) +
geom_line(aes(VK,relPrecAir, color=as.factor(p))) + geom_point(aes(VK,relPrecAir), shape=17) +
geom_line(aes(VK,relPrecInv, color=as.factor(p))) + geom_point(aes(VK,relPrecInv), shape=15) +
scale_color_discrete(name="p")
为 p
显示了一个图例。但是我无法用形状值为 type
绘制第二个图例。我该如何添加它?
您可以 melt
您的 data.frame 以避免 ggplot
命令中的多个步骤,并创建一个您将在其中映射的新变量(此处 ptShape
)单个 geom_point
调用的 aes
:
library(reshape2)
pmelt = melt(precisionTable, id.vars=c('VK', 'p', 'case'))
pmelt$ptShape = rep(c(19,17,15), each=6)
ggplot(pmelt, aes(x=VK, y=value)) +
geom_line(aes(colour=as.factor(p), shape=as.factor(ptShape))) +
geom_point(aes(colour=as.factor(p), shape=as.factor(ptShape)), size=5)
我们可以通过为参数 relPrecTotal
、relPrecAir
和 relPrecInv
添加新的分组来处理它,然后使用 ggplot 继续绘制它。
编辑 覆盖颜色图例中的形状
library(reshape2)
precisionTable2 <- melt(precisionTable, measure.vars = c("relPrecTotal", "relPrecAir", "relPrecInv"))
ggplot(precisionTable2, aes(VK, value)) +
geom_line(aes(color=as.factor(p), shape=as.factor(variable))) +
geom_point(aes(color=as.factor(p),shape=as.factor(variable))) +
scale_color_discrete(name='p') +
scale_shape_discrete(name='relPrec') +
guides(color=guide_legend(override.aes = list(shape=NA)))