ojp
ojp

Reputation: 1033

Scaling two separate y-axis labels ggplot2

I am plotting two seperate y-axis labels to show the relationship between electricity consumption and temperature. I seem to lose control of the first y-axis. I want this scale to be approximately 0.2 - 0.4. my temperature scale is fine. I want my consumption scale to look like this.

enter image description here

But after adding the second scale with the same code it changes to this.

enter image description here

here is my code:

ggplot()+   
    geom_line(data=average_total, aes(x=day,y=average_day,col="Consumption"))+
    scale_y_continuous(sec.axis = sec_axis(~.*50, name = "Temperature [C]"))+
    geom_line(data=temp, aes(x=day, y=day_temp/50, col="Temperature"))+
    scale_color_manual("",values=c("skyblue4","green4"),
                    labels=c("Consumption","Temperature"))+
labs(title = "Yearly average consumption\nand ambient temperature",
       x = "Year day",
       y = "Electricity consumption Kwh")+
theme_linedraw()

And my data:

day average_day
<dbl>   <dbl>
1   0.3226814
2   0.3248489
3   0.3254643
4   0.3286167
5   0.3281448
6   0.3346636

day day_temp
<dbl>   <dbl>
1   7.49
2   10.82
3   11.41
4   10.79
5   10.66
6   8.61

Any ideas on how I can adjust the first y-axis scale without affecting the temperature axis?

Upvotes: 1

Views: 460

Answers (2)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6759

I have made some changes to your code. In stead of using your original linear transformation, ~.*50, I have used ~.*200-40, and corresponding back transformation as y=(day_temp+40)/200. I have also changed your example data to show the ranges of both variables. You may want to adjust those values to fit your real data.

library(ggplot2)

average_total <- data.frame(day = c(1,2,3,4,5,6),
                            average_day = c(0.38, 0.25, 0.21, 0.22, 0.38, 0.40))
temp <- data.frame(day = c(1,2,3,4,5,6),
                   day_temp = c(0.5,20,30,38,25,4))

ggplot()+   
  geom_line(data=average_total, aes(x=day,y=average_day,col="Consumption"))+
  scale_y_continuous(sec.axis = sec_axis(~.*200-40, name = "Temperature [C]"))+
  geom_line(data=temp, aes(x=day, y=(day_temp+40)/200, col="Temperature"))+
  scale_color_manual("",values=c("skyblue4","green4"),
                     labels=c("Consumption","Temperature"))+
  labs(title = "Yearly average consumption\nand ambient temperature",
       x = "Year day",
       y = "Electricity consumption Kwh")+
  theme_linedraw()

Created on 2019-12-23 by the reprex package (v0.3.0)

Upvotes: 2

D.sen
D.sen

Reputation: 922

First, the data provided does not fully represent the data you've plotted, so the plots will differ.

I believe you are asking how to control the y_axis breakpoints on the primary ( electricity consumption) axis. You can do that by adding breaks = seq(.25, .35, by = .025) to your scale_y_continuous function call.


library(ggplot2)

average_total <- data.frame(day = c(1,2,3,4,5,6),
                            average_day = c(0.3226814, 0.3248489, 0.3254643, 0.3286167, 0.3281448, 0.3346636))
temp <- data.frame(day = c(1,2,3,4,5,6),
                   day_temp = c(7.49,10.82,11.41,10.79,10.66,8.61))

# Initial PLot 

ggplot()+   
  geom_line(data=average_total, aes(x=day,y=average_day,col="Consumption"))+
  scale_y_continuous(sec.axis = sec_axis(~.*50, name = "Temperature [C]"))+
  geom_line(data=temp, aes(x=day, y=day_temp/50, col="Temperature"))+
  scale_color_manual("",values=c("skyblue4","green4"),
                     labels=c("Consumption","Temperature"))+
  labs(title = "Yearly average consumption\nand ambient temperature",
       x = "Year day",
       y = "Electricity consumption Kwh")+
  theme_linedraw()


# Adjusted Plot

ggplot()+   
  geom_line(data=average_total, aes(x=day,y=average_day,col="Consumption"))+
  scale_y_continuous(breaks = seq(.25, .35, by = .025),
                     sec.axis = sec_axis(~.*50, name = "Temperature [C]"))+
  geom_line(data=temp, aes(x=day, y=day_temp/50, col="Temperature"))+
  scale_color_manual("",values=c("skyblue4","green4"),
                     labels=c("Consumption","Temperature"))+
  labs(title = "Yearly average consumption\nand ambient temperature",
       x = "Year day",
       y = "Electricity consumption Kwh")+
  theme_linedraw()

Created on 2019-12-22 by the reprex package (v0.3.0)

Because you are working with two separate y_axis, on different scales, you're either going to a.) have a truncated primary axis (as seen above) or b.) you'll have a full axis (which you can do by setting the limits) but the low-level breakpoint will result in an axis value every .025 units. Which will be very overwhelming.

Upvotes: 0

Related Questions