我正在尝试使用ggplot绘制温度和降水量图。
花了一些时间获得两个y轴(与好的绘图理论相反,我知道!),我的x轴现在不存在。
是否有可能我的代码中缺少某些内容?
月是这样创建的
Month <- c(1,2,3,4,5,6,7,8,9,10,11,12)
该图的其余部分使用以下内容创建
gp1 <-ggplot() +
geom_bar(mapping = aes(x = Month, y = Prec * 40 / 1100), stat = "identity",fill="cornflowerblue") +
geom_point(mapping = aes(x = Month, y = meanTemp),col="orange") +
geom_line(mapping = aes(x = Month, y = meanTemp),col="orange") +
geom_point(mapping = aes(x = Month, y = maxTemp),col="red") +
geom_line(mapping = aes(x = Month, y = maxTemp),col="red") +
geom_point(mapping = aes(x = Month, y = minTemp),col="gold") +
geom_line(mapping = aes(x = Month, y = minTemp),col="gold") +
scale_x_discrete(breaks=c(1,2,3,4,5,6,7,8,9,10,11,12), labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")) +
scale_y_continuous(name = expression("Temperature ("~degree~"C)"), limits = c(0, 40))
gp1<- gp1 %+% scale_y_continuous(name = expression("Temperature ("~degree~"C)"), sec.axis = sec_axis(~ . * 1100 / 40 , name = "Precipitation (mm)"), limits = c(0, 40))
gp1<-gp1+theme_classic()
gp1
产生的数字看起来 as such
答案 0 :(得分:1)
问题是您的x轴是连续的,而不是离散的。
这是因为您为x美学指定了数值。如果要为x美学指定文本或因子值,则x轴将是离散的。
将scale_x_discrete
换成scale_x_continuous
解决了我的实验中的问题。