我有最小和最高温度数据,我想正确着色。
数据看起来像这样
MESS_DATUM LUFTTEMPERATUR_MAXIMUM LUFTTEMPERATUR_MINIMUM
20050719 22.1 16.5
20050720 22.8 12.8
20050721 22.6 12.0
20050722 19.1 13.8
20050723 21.9 12.8
20050724 24.8 11.4
20050725 25.8 16.9
20050726 24.7 16.3
20050727 31.6 17.0
20050728 34.5 19.2
20050729 27.3 18.2
20050730 25.5 13.4
20050731 24.5 11.5
20050801 24.4 12.5
20050802 22.1 14.6
20050803 24.9 15.2
20050804 23.4 11.3
20050805 23.4 9.1
20050806 21.0 12.0
20050807 19.2 9.8
20050808 19.2 10.8
20050809 21.0 11.2
20050810 20.2 10.2
这两行获得了单独的色谱,但我希望两条线使用相同的光谱。这是我用过的代码。
p2<-ggplot(dataByYear) +
geom_line(aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MINIMUM, colour = LUFTTEMPERATUR_MINIMUM)) +
geom_smooth(aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MINIMUM), color = "blue",size = 0.5) +
geom_line(aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MAXIMUM, colour = LUFTTEMPERATUR_MINIMUM)) +
geom_smooth(aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MAXIMUM), color = "red",size = 1) +
scale_colour_gradient2(low = "blue", mid = "green" , high = "red", midpoint = 10) +
scale_y_continuous(limits = c(-10,40), breaks = seq(-10,40,5)) +
ggtitle ("Daily average temperature") +
xlab("Date") + ylab ("Average Temperature ( ºC )")
看起来像这样
正如您所看到的,某种温度下颜色并不是唯一的。底线的红色可以是15°C以上的红色,但在顶线的相同温度下仍然是绿色。
我想要一个同样用于两条线的色标。 有人知道怎么做吗?
提前致谢。
答案 0 :(得分:2)
欢迎来到SO!这是一个快速解决方法:另外在融化数据上使用group
。不介意破碎的日期,我没有费心去修理它,你的数据框一切都应该是好的。不过,您可以根据需要查看常用色标。
ggplot(tidyr::gather(df, temp, value, -MESS_DATUM),
aes(x = MESS_DATUM, y = value, colour = value)) +
geom_line(aes(group = temp)) +
geom_smooth(data = df, aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MINIMUM), color = "blue",size = 0.5) +
geom_smooth(data = df, aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MAXIMUM), color = "red",size = 1) +
scale_colour_gradient2(low = "blue", mid = "green" , high = "red", midpoint = 10) +
scale_y_continuous(limits = c(-10,40), breaks = seq(-10,40,5)) +
ggtitle ("Daily average temperature") +
xlab("Date") + ylab ("Average Temperature ( ºC )")
答案 1 :(得分:1)
2小时后更新......
好的,我发现了我需要做的事情。
我的问题是我的数据框有更多的列,不应该由tidyr.gather()合并。我必须阅读tidyr.gather()的文档,但后来我发现我需要创建一个只包含3列的独立数据框。然后我可以合并它们并在图中使用它们。
我的解决方案是首先创建一个单独的数据框,然后在plot命令中调整变量名。
#Prepare data frame for ggplot
df <-
data.frame(
dataByYear$MESS_DATUM,
dataByYear$LUFTTEMPERATUR_MAXIMUM,
dataByYear$LUFTTEMPERATUR_MINIMUM
)
#Plot data
#tidyr::gather creates Key/Values pairs temp/Temperatures which are used to
#diplay the values equally colored
p2 <-
ggplot(
tidyr::gather(df, temp, Temperature,-dataByYear.MESS_DATUM),
aes(x = dataByYear.MESS_DATUM, y = Temperature, colour = Temperature)
) +
geom_line(aes(group = temp)) +
geom_smooth(
data = df,
aes(x = dataByYear.MESS_DATUM, y = dataByYear.LUFTTEMPERATUR_MINIMUM),
color = "blue",
size = 0.5
) +
geom_smooth(
data = df,
aes(x = dataByYear.MESS_DATUM, y = dataByYear.LUFTTEMPERATUR_MAXIMUM),
color = "red",
size = 1
) +
scale_colour_gradient2(
low = "blue",
mid = "green" ,
high = "red",
midpoint = 10
) +
scale_y_continuous(limits = c(-10, 40), breaks = seq(-10, 40, 5)) +
ggtitle ("Daily average temperature") +
xlab("Date") + ylab ("Average Temperature ( ºC )") +
scale_x_date(date_breaks = "1 month", date_labels = "%b")
现在,这是我的最终结果。看起来不错。