ggplot2:添加钻石形ppints的附加指南

时间:2015-09-23 04:14:29

标签: r ggplot2

我有以下数据:

groups = c(rep(1,5),rep(2,5),rep(3,5))
scores = c(seq(1,5),seq(1,5),seq(1,5))
times1 = rnorm(15, mean = 3 , sd = 2)
times2 = rnorm(15, mean = 1 , sd = 0.5)
df = data.frame(groups,scores, times1,times2)

我有以下情节

df = data.frame(groups,scores, times1,times2)
plt = ggplot(df, aes(x = scores, y = times1, color = factor(groups)))
plt = plt + geom_point(cex = 4) + geom_line() + theme_bw() 
plt = plt + geom_point(aes(x = scores, y=times2),pch = 23, cex =4)+ geom_line(aes(x = scores, y=times2))
plt = plt + facet_wrap(~ groups, ncol = 4, scales = "free_x") 
plt

导致

enter image description here

如何添加钻石点的指南,以及如何更改每个相应指南的标题。

1 个答案:

答案 0 :(得分:1)

如果你想要一个传奇的东西,它应该被指定为美学。也许像是

ggplot(df, aes(x = scores, color = factor(groups))) + 
  geom_point(aes(y=times1, shape="times1"), cex = 4) + 
  geom_line(aes(y=times1)) +
  geom_point(aes(y=times2, shape="times2"),cex =4) + 
  geom_line(aes(y=times2)) + 
  facet_wrap(~ groups, ncol = 4, scales = "free_x") + theme_bw()

如果您将数据正确地重新定义为ggplot所需的格式,那么与其他人工添加图层相比,它会更好。

ggplot(reshape2::melt(df, id=c("groups","scores")), 
    aes(x=scores,y=value, shape=variable, color=factor(groups))) +
    geom_point() + 
    geom_line() +
    facet_wrap(~groups)