0

My plot is almost ready, I just cannot reverse the secondary axis of a line (geom_line) plotted over a dodge barplot. My code so far:

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"))

I just need the y axis at the right (Temp) to be reversed and start at the top from zero 0 to -15

This is the result I have now This is the result I have for now

And my dataframe looks something like this:

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)
capuchin7
  • 47
  • 6
  • Please provide a reproducible question [reprex] i.e. include `coll <- data.frame(...)` where ... is the variables and values of coll. – Peter May 11 '20 at 16:28
  • Sure, I have edited my post, hope you may take a look at it, thanks – capuchin7 May 11 '20 at 18:23

1 Answers1

0

Is your coefficient really what you want?

-3 / -(1/50) = 150, whereas -6 / -(1/50) = 300 so this means that a lower temperature appears higher on your graph

So I have adapted the data to make this work intuitively.

However you really should be aware that two y-axis with different scales is not considered good practice.

See this link for a good discussion on the issue: ggplot with 2 y axes on each side and different scales

Having said all that here you go. I've tidied up your data frame: I hope you can see that this may be a simpler approach.


# 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)))

enter image description here

Peter
  • 11,500
  • 5
  • 21
  • 31
  • Thank you very much Peter. Sure the code looks tidier now and the plot is almost done. However, the temperature values are negative if you check the dataframe, but in the plot they are positive. So, is there any way to fix this? – capuchin7 May 12 '20 at 04:23
  • I have checked it and applied to my code and it works well. Thank you very much Peter it was really helpful. Just in case, do you know how I can add the breaks in the secondary plot? I mean, instead of having them every -5, to plot them every -2. I tried breaks=seq(0, -15, by = 2) but did not work – capuchin7 May 12 '20 at 07:50
  • You need seq(0, -15, by = -2). – Peter May 12 '20 at 08:54
  • Awesome it work out well, thanks Peter you help me a lot – capuchin7 May 12 '20 at 09:35