我有这种形式的数据集: 年,温度,距离海的距离。距离海的距离是远或近的分类变量。其他的分别是离散和连续的数据。我需要提出一个折线图,用于绘制变量距离海平面的水平的年份和温度,即我需要一个折线图,显示同一地块上近处和远处两个地方的年和温度之间的关系。帮助!
答案 0 :(得分:0)
使用ggolot2,你可能会想要这样的东西。
library(ggplot2)
year <- rep(c(2001:2010), each = 1, times = 2)
temperature <- runif(20, 0, 35)
distance <- rep(c("far","near"), each = 10, times = 1)
foo <- data.frame(year, distance, temperature)
foo$year <- as.factor(foo$year)
foo$temperature <- as.numeric(foo$temperature)
ggplot(foo, aes(x = year, y = temperature, color = distance, group = distance)) +
geom_line(stat="identity")
答案 1 :(得分:0)
对于包含来自jazzurro的数据的条形图:
ggplot(foo)+geom_bar(aes(year, temperature, fill=distance), stat='identity', position='dodge')
答案 2 :(得分:0)
一些基本R解决方案,使用相同的设置:
year <- rep(c(2001:2010), each = 1, times = 2)
temperature <- runif(20, 0, 35)
distance <- rep(c("far","near"), each = 10, times = 1)
foo <- data.frame(year, distance, temperature)
foo$year <- as.factor(foo$year)
foo$temperature <- as.numeric(foo$temperature)
然后你可以做
plot(year, temperature, data = foo[foo$distance == "near", ], type = "l")
lines(year, temperature, data = foo[foo$distance == "far", ])
或者如果数据未排序,您可以执行类似
的操作foo_sorted <- foo[order(foo$year), ]
plot(year, temperature, data = foo_sorted[foo_sorted$distance == "near", ], type = "l")
lines(year, temperature, data = foo_sorted[foo_sorted$distance == "far", ])
或者,您可以使用
ts.plot(cbind(foo$temperature[foo$distance == "near"], foo$temperature[foo$distance == "far])
或矩阵表示法中的相同内容
ts.plot(cbind(foo[foo$distance == "near", "temperature"], foo$temperature[foo$distance == "far])
您可能还想reshape
您的数据。
library(reshape2)
foo_wide <- cast(melt(foo), year ~ distance + variable)
ts.plot(foo_wide[, 2:3])
最后,zoo
包有一组非常好的时间序列对象绘图方法。