【发布时间】:2020-07-28 00:55:55
【问题描述】:
我有一个数据框,其中包含 6 个变量中的 90 个对象(一个是日期,另一个是 3 个月的车站温度 - 这里添加了一部分)。我想根据日期绘制站点温度数据,但是station1数据应该用点+线绘制,而其他站点仅以点为单位。
df <- data.frame(Date= c(1:20),
Station1 = c(31.7,19.5,23.6,20.5,25.2,35.5,38.0,30.3,20.1,20.6,23.6,33.6,21.1,22.7,24.8,23.5,21.8,20.8,26.9,21.2),
Station2= c(10.3,12.2,13.3,13.4,13.4,14.5,25.1,22.7,16.0,15.8,13.0,16.0,16.9,16.4,17.2,15.8,15.6,16.7,16.8,16.9),
Station3 = c(26.4,15.8,18.0,15.6,22.6,30.4,31.7,26.5,18.2,19.9,23.2,28.0,16.7,20.1,21.4,19.4,20.1,19.8,25.0,20.3),
Station4 = c(8.6,8.8, 7.1,9.3,8.5,13.1,21.6,20.1,12.3,11.7,9.6,14.2,15.9,13.1,13.6,13.1,12.4,11.3,12.5,14.3),
Station5 = c(31.6,22.8,17.0,18.6,28.9,35.5,38.7,30.3,25.7,21.9,28.3,32.7,24.2,26.5,28.1,24.4,24.0,24.6,28.5,22.5))
我尝试过不同的方式,
x <- df$Date
y1 <- df$Station1
y2 <- df$Station2
y3 <- df$Station3
y4 <- df$Station4
y5 <- df$Station5
g1 <- ggplot(df, aes(x)) +
geom_line(aes(y=y1), color = "red")+
geom_point(aes(y=y1), color = "red")+
geom_point(aes(y=y2), color = "#00FF00") +
geom_point(aes(y=y3), color = "blue") +
geom_point(aes(y=y4), color = "#FF9933") +
geom_point(aes(y=y5), color = "purple")
这并没有给我一个传奇,我也无法添加一个。
我尝试使用 tidyr,
PD <- df %>%
gather(type, Temperature, Station1, Station2, Station3, Station4, Station5)
ggplot(PD, aes(x = Date, y= Temperature, color = type)) + geom_point()
它创建点图,但不能为第一站做点+线。
这也在尝试,但由于日期变成了行名,所以没有奏效。
df1 <- data.frame(St1 = c(df$Station1),
St2 = c(df$Station2),
St3 = c(df$Station3),
St4 = c(df$Station4),
St5 = c(df$Station5),
row.names = c(c(df$DATE)))
q <- df1 %>%
rownames_to_column() %>%
gather(key = key, value = value, St1,St2) %>%
mutate(rowname = factor(rowname)) %>%
ggplot(aes(as.Date(rowname), value, color = key)) +
geom_point() +
geom_line() +
labs(x="Dates", y="temperature", title ="Station temperatures")+
theme_bw()
非常感谢任何帮助,因为我对 R 非常陌生。提前致谢!
【问题讨论】:
-
你需要把颜色属性放在
aes中...然后你可以使用scale_color_manual和guides来构造你的图例 -
谢谢@VictorMaxwell。这是一个帮助。 :)