[英]Combine Points with lines with ggplot2
我想 plot 一个看起来像这样的时间序列:
我用什么 plot :
qplot(Jahr, Wert, data=tu, group = Geschlecht, color = Altersgr) + facet_grid(Geschlecht ~ Land)
我的数据如下所示:
Land Altersgr Geschlecht Jahr Wert
1 DE < 20 J. m 2000 13.0
2 DE 20-<65 J. m 2000 25.7
3 DE 65+ J. m 2000 70.1
4 DE 65-<80 J. m 2000 44.2
5 DE 80+ J. m 2000 213.5
6 BB < 20 J. m 2000 26.8
到目前为止一切都很好。 但我必须用一条线连接相应的点(相同颜色)。 我不知道该怎么做。 如果我使用 geom_line() 我得到了这个结果:
这不是我想要的......我只是觉得我忽略了一些东西......
有什么建议么? 谢谢大家的帮助。
您可能会发现使用“group”aes将帮助您获得所需的结果。 例如:
tu <- expand.grid(Land = gl(2, 1, labels = c("DE", "BB")),
Altersgr = gl(5, 1, labels = letters[1:5]),
Geschlecht = gl(2, 1, labels = c('m', 'w')),
Jahr = 2000:2009)
set.seed(42)
tu$Wert <- unclass(tu$Altersgr) * 200 + rnorm(200, 0, 10)
ggplot(tu, aes(x = Jahr, y = Wert, color = Altersgr, group = Altersgr)) +
geom_point() + geom_line() +
facet_grid(Geschlecht ~ Land)
这产生了这里的情节:
以下使用iris数据集的示例工作正常:
dat = melt(subset(iris, select = c("Sepal.Length","Sepal.Width", "Species")),
id.vars = "Species")
ggplot(aes(x = 1:nrow(iris), y = value, color = variable), data = dat) +
geom_point() + geom_line()
对 Paul 的代码进行小改动,使其不会返回上述错误。
dat = melt(subset(iris, select = c("Sepal.Length","Sepal.Width", "Species")),
id.vars = "Species")
dat$x <- c(1:150, 1:150)
ggplot(aes(x = x, y = value, color = variable), data = dat) +
geom_point() + geom_line()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.