这是我学习R和ggplot的第一天。我遵循了一些教程,并希望通过以下命令生成类似于的图:
qplot(age, circumference, data = Orange, geom = c("point", "line"), colour = Tree)它看起来像这个页面上的图:http://www.r-bloggers.com/quick-introduction-to-ggplot2/
我有一个手工创建的测试数据文件,它看起来像这样:
site temp humidity
1 1 1 3
2 1 2 4.5
3 1 12 8
4 1 14 10
5 2 1 5
6 2 3 9
7 2 4 6
8 2 8 7但当我尝试阅读并绘制它时:
test <- read.table('test.data')
qplot(temp, humidity, data = test, color=site, geom = c("point", "line"))图中的线条不是单独的系列,而是链接在一起:
http://imgur.com/weRaX
我做错了什么?
谢谢。
发布于 2012-04-05 02:07:28
您需要告诉ggplot2如何将数据分组到单独的行中。它不是读心术!;)
dat <- read.table(text = " site temp humidity
1 1 1 3
2 1 2 4.5
3 1 12 8
4 1 14 10
5 2 1 5
6 2 3 9
7 2 4 6
8 2 8 7",sep = "",header = TRUE)
qplot(temp, humidity, data = dat, group = site,color=site, geom = c("point", "line"))

请注意,您可能还想执行color = factor(site),以便强制使用离散色阶,而不是连续色阶。
https://stackoverflow.com/questions/10016785
复制相似问题