【发布时间】:2014-12-08 10:13:03
【问题描述】:
我正在尝试在 ggplot2 中创建一个折线图,它结合了某些变量的不同线条样式和其他变量的不同标记。
示例 1 使用不同的线型绘制每个变量,示例 2 使用不同的标记绘制每个变量,示例 3 使用不同的线和标记绘制每个变量。
我试图用不同的线条样式(实线、虚线)绘制 X2 和 X3,然后将 X4 和 X5 绘制为具有不同标记(圆形、正方形等)的实线。
有什么办法吗?
library(ggplot2)
library(reshape2)
set.seed <- 1
df <- data.frame(cbind(seq(1,10,1),matrix(rnorm(100,1,20), 10, 4)))
d <- melt(df, id="X1")
# Example 1: different line styles
ggplot(d, aes(x=X1, y=value, color=variable)) +
geom_line(aes(linetype=variable), size=1)
# Example 2: different markers for each line
ggplot(d, aes(x=X1, y=value, color=variable)) +
geom_line() + geom_point(aes(shape=variable, size=4))
# Example 3: differnt line styles & different markers (You see this graph below)
ggplot(d, aes(x=X1, y=value, color=variable)) +
geom_line(aes(linetype=variable), size=1) +
geom_point(aes(shape=variable, size=4))
【问题讨论】:
-
作为第一个问题,这是用可重现的样本写得很好。干得好!:)