我正在尝试在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))
Run Code Online (Sandbox Code Playgroud)
jaz*_*rro 11
这是一种方法.您可以使用另外两个函数来控制形状和线型.scale_linetype_manual
允许您手动分配线型.同样,scale_shape_manual
允许您手动指定所需的任何形状.
# Example 3: differnt line styles & different markers
ggplot(d, aes(x=X1, y=value, color=variable)) +
geom_line(aes(linetype=variable), size=1) +
geom_point(aes(shape=variable, size=4)) +
scale_linetype_manual(values = c(1,2,1,1)) +
scale_shape_manual(values=c(0,1,2,3))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19398 次 |
最近记录: |