我有这样一个数据集:年份,温度,离海洋的距离。离海的距离是一个很远或更近的绝对变量。其余数据分别为离散数据和连续数据。我需要给出一个线图,为两个不同的海洋距离的水平绘制年份和温度,也就是说,我需要一个线图,显示在同一地块附近和很远的两个地方的年和温度之间的关系。救命!!
发布于 2014-08-29 01:22:04
有关jazzurro提供的数据的条形图:
ggplot(foo)+geom_bar(aes(year, temperature, fill=distance), stat='identity', position='dodge')

发布于 2014-08-29 00:20:07
使用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")发布于 2014-08-29 01:42:21
和一些基本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包有自己的一组非常好的时间序列对象绘图方法。
https://stackoverflow.com/questions/25559806
复制相似问题