假设我在ggplot中有以下绘图:
它是使用以下代码生成的:
x <- seq(0, 10, by = 0.2)
y1 <- sin(x)
y2 <- cos(x)
y3 <- cos(x + pi / 4)
y4 <- sin(x + pi / 4)
df1 <- data.frame(x, y = y1, Type = as.factor("sin"), Method = as.factor("method1"))
df2 <- data.frame(x, y = y2, Type = as.factor("cos"), Method = as.factor("method1"))
df3 <- data.frame(x, y = y3, Type = as.factor("cos"), Method = as.factor("method2"))
df4 <- data.frame(x, y = y4, Type = as.factor("sin"), Method = as.factor("method2"))
df.merged <- rbind(df1, df2, df3, df4)
ggplot(df.merged, aes(x, y, colour = interaction(Type, Method), linetype = Method, shape = Type)) + geom_line() + geom_point()
我只希望有一个图例可以正确显示形状,颜色和线型(interaction(类型,方法)图例最接近我想要的图例,但它没有正确的形状/线型) 。
我知道如果我使用scale_xxx_manual并且为所有图例指定了相同的标签,那么它们将被合并,但是我不想手动设置标签:如果有新的Method或Types,我不想拥有修改我的代码:想要一些通用的东西。
正如下面的答案所指出的,在这种特殊情况下,有几种方法可以完成工作。所有建议的解决方案都需要使用scale_xxx_manual function
s或withguides
函数手动设置图例线的类型和形状。
但是,建议的解决方案在一般情况下仍然不起作用:例如,如果我使用新的“ method3”方法向数据集添加新的数据框,则该方法不再起作用,我们必须手动添加新的图例形状和线型:
y5 <- sin(x - pi / 4)
df5 <- data.frame(x, y = y5, Type = as.factor("sin"), Method = as.factor("method3"))
df.merged <- rbind(df1, df2, df3, df4, df5)
override.shape <- c(16, 17, 16, 17, 16)
override.linetype <- c(1, 1, 3, 3, 4)
g <- ggplot(df.merged, aes(x, y, colour = interaction(Type, Method), linetype = Method, shape = Type)) + geom_line() + geom_point()
g <- g + guides(colour = guide_legend(override.aes = list(shape = override.shape, linetype = override.linetype)))
g <- g + scale_shape(guide = FALSE)
g <- g + scale_linetype(guide = FALSE)
print(g)
这给出了:
现在的问题是:如何自动生成override.shape
和override.linetype
向量?
请注意,向量大小为5,因为我们有5条曲线,而interaction(Type, Method)
因子的大小为6(我没有cos / method3组合的数据)
这是一般情况下的解决方案:
# Create the data frames
x <- seq(0, 10, by = 0.2)
y1 <- sin(x)
y2 <- cos(x)
y3 <- cos(x + pi / 4)
y4 <- sin(x + pi / 4)
y5 <- sin(x - pi / 4)
df1 <- data.frame(x, y = y1, Type = as.factor("sin"), Method = as.factor("method1"))
df2 <- data.frame(x, y = y2, Type = as.factor("cos"), Method = as.factor("method1"))
df3 <- data.frame(x, y = y3, Type = as.factor("cos"), Method = as.factor("method2"))
df4 <- data.frame(x, y = y4, Type = as.factor("sin"), Method = as.factor("method2"))
df5 <- data.frame(x, y = y5, Type = as.factor("sin"), Method = as.factor("method3"))
# Merge the data frames
df.merged <- rbind(df1, df2, df3, df4, df5)
# Create the interaction
type.method.interaction <- interaction(df.merged$Type, df.merged$Method)
# Compute the number of types and methods
nb.types <- nlevels(df.merged$Type)
nb.methods <- nlevels(df.merged$Method)
# Set the legend title
legend.title <- "My title"
# Initialize the plot
g <- ggplot(df.merged, aes(x,
y,
colour = type.method.interaction,
linetype = type.method.interaction,
shape = type.method.interaction)) + geom_line() + geom_point()
# Here is the magic
g <- g + scale_color_discrete(legend.title)
g <- g + scale_linetype_manual(legend.title,
values = rep(1:nb.types, nb.methods))
g <- g + scale_shape_manual(legend.title,
values = 15 + rep(1:nb.methods, each = nb.types))
# Display the plot
print(g)
结果如下:
总结一下,这些技巧是:
interaction
用于所有数据表示形式(颜色,形状,线型等)scale_xxx_manual
。scale_xxx_manual
允许您提供比实际曲线数更长的值向量,因此很容易根据“类型”和“方法”因子的大小来计算样式向量值本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句