今天早上,我正在为如何正确设置ggplot的主轴和次轴而斗争。我有一个看起来像这样的数据
df <- data.frame(Day = seq(1:10),
Temperature = seq(4,7,length=10),
Moisture = seq(5,9,length=10))
Day Temperature Moisture
1 1 4.000000 5.000000
2 2 4.333333 5.444444
3 3 4.666667 5.888889
4 4 5.000000 6.333333
5 5 5.333333 6.777778
6 6 5.666667 7.222222
7 7 6.000000 7.666667
8 8 6.333333 8.111111
9 9 6.666667 8.555556
10 10 7.000000 9.000000我只想把温度分配给主y轴,将湿度分配给次y轴。
到目前为止,我的努力没有奏效:任何建议都是非常感谢的。之前在这个主题中的帖子对我没有帮助。
ggplot(df,aes(Day)) +
geom_line(aes(y=Temperature), color="#45BF44") +
geom_line(aes(y=Moisture), color="#02BEC4") +
scale_y_continuous(limits = c(3.5,7),
name = "Temp",
sec.axis = sec_axis(~ 1.2+ ., name="Moist"))

我希望蓝线的5与次轴上的5一致,而不是主轴上的5。
发布于 2021-07-05 17:57:50
这是你所期望的吗?
library(ggplot2)
ggplot(df,aes(Day)) +
geom_line(aes(y=Temperature), color="#45BF44") +
geom_line(aes(y=Moisture-1.2), color="#02BEC4") +
scale_y_continuous(limits = c(3.5,7),
name = "Temp",
sec.axis = sec_axis(~ 1.2+ ., name="Moist"))

https://stackoverflow.com/questions/68253659
复制相似问题