【问题标题】:How to reverse only secondary y axis in ggplot?如何在ggplot中仅反转次y轴?
【发布时间】:2020-05-11 16:24:23
【问题描述】:

我的绘图几乎准备好了,我只是无法反转绘制在闪避条形图上的线的辅助轴 (geom_line)。到目前为止我的代码:

coeff<--1/50
ggplot(coll,aes(Date,value,fill=variable))+
  geom_bar(stat="identity",position="dodge")+
  ylab("Precipitation")+xlab("Month")+ylim(0,900)+
  geom_line(aes(x=Date,y=Temp/coeff,col="black"),col="black")+
  geom_point(aes(x=Date,y=Temp/coeff,col="black"),col="black")+
  scale_y_continuous(sec.axis = sec_axis(~.*coeff, name = "Temp"))

我只需要将右侧的 y 轴 (Temp) 反转并从顶部从零开始 0 到 -15

这是我现在得到的结果

我的数据框看起来像这样:

Date<-c("1/1/2019","2/1/2019","3/1/2019","4/1/2019","5/1/2019","6/1/2019","7/1/2019",
        "8/1/2019","9/1/2019","10/1/2019","11/1/2019","12/1/2019")%>%data.frame()
names(Date)[names(Date) == "."] <- "Date"
Date<-as.POSIXct(Date$Date,format="%m/%d/%Y")
Month<-c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")%>%data.frame()
names(Month)[names(Month) == "."] <- "Month"
Temp<-c(NA,-3,-6,-13,-12,-6,-5,-1,-4,-7,-8,NA)%>%data.frame()
names(Temp)[names(Temp) == "."] <- "Temp"
variable<-c(rep("bar1",12,sep=","),rep("bar2",12,sep=","),rep("bar3",12,sep=","))%>%data.frame()
names(variable)[names(variable) == "."] <- "variable"
value<-rnorm(36,400,100)
coll<-cbind(Date,Month,Temp,variable,value)

【问题讨论】:

  • 请提供一个可重复的问题minimal reproducible example,即包括coll &lt;- data.frame(...),其中...是coll的变量和值。
  • 好的,我已经编辑了我的帖子,希望你能看一下,谢谢

标签: r ggplot2 plot bar-chart yaxis


【解决方案1】:

你的系数真的是你想要的吗?

-3 / -(1/50) = 150,而-6 / -(1/50) = 300,这意味着较低的温度在您的图表上显得较高

所以我已经调整了数据以直观地完成这项工作。

但是您确实应该知道,两个不同比例的 y 轴不被认为是好的做法。

查看此链接以获得有关该问题的良好讨论:ggplot with 2 y axes on each side and different scales

说了这么多,就到这里吧。我已经整理了你的数据框:我希望你能看到这可能是一种更简单的方法。


# packages

library(ggplot2)

# data

coll <- data.frame(
  Date = c("1/1/2019", "2/1/2019", "3/1/2019", "4/1/2019", "5/1/2019", "6/1/2019", "7/1/2019",
        "8/1/2019", "9/1/2019", "10/1/2019", "11/1/2019", "12/1/2019"),
  Month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
  Temp = c(NA,-3,-6,-13,-12,-6,-5,-1,-4,-7,-8,NA), 
  variable = c(rep("bar1", 12, sep = ","), rep("bar2", 12, sep = ","), rep("bar3" , 12, sep = ",")),
  value = rnorm(36,400,100))

# Data wrangling

coll$Date <- as.POSIXct(coll$Date, format = "%m/%d/%Y")

# Create additional variable for modified temperature to scale with the preciptation y axis
# tranformation coefficient
coeff <- 50

coll$temp_mod <- (coll$Temp * coeff) + 700





# plot

ggplot(coll, aes(Date, value, fill = variable))+
  geom_bar(stat = "identity", position = "dodge")+
  ylab("Precipitation")+
  xlab("Month")+
  ylim(0, 900)+
  geom_line(aes(Date, temp_mod))+
  geom_point(aes(Date, temp_mod))+
  scale_y_continuous(sec.axis = sec_axis(~-rev(.) / coeff, name = "Temp", breaks = seq(0, -15, by = -2)))

【讨论】:

  • 非常感谢彼得。当然现在代码看起来更整洁了,情节也差不多完成了。但是,如果您检查数据框,温度值是负值,但在图中它们是正值。那么,有什么办法可以解决这个问题吗?
  • 我已经检查并应用到我的代码中,它运行良好。非常感谢彼得,这真的很有帮助。以防万一,你知道我如何在次要情节中添加中断吗?我的意思是,不是每-5一次,而是每-2一次绘制一次。我尝试了 break=seq(0, -15, by = 2) 但没有用
  • 你需要 seq(0, -15, by = -2)。
  • 太棒了,效果很好,谢谢彼得你帮了我很多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 2011-01-04
  • 1970-01-01
相关资源
最近更新 更多