'Strange behaviour when I add secondary axis in my ggplot
This is one application from my earlier post on similar subject - Clubbing 2 ggplots in a single windows and putting 2nd one in Secondary axis
Let say I have below ggplot, and I want to have secondary axis for the variable y2
library(ggplot2)
library(quantmod)
dat = data.frame(x = as.yearqtr(seq(as.Date('2002-01-01'), length.out = 17, by = '95 day')),
y1 = c(-0.0042, 0.0221, 0.0387, 0.0523, -0.0743, -0.0903, 0.0997, 0.0982, 0.0956, -0.0961, -0.0696, -0.0818, 0.0766, 0.0654, 0.1275, 0.1178, 0.0878),
y2 = c(19747, 19343, 19007, 18336, 17534, 16548, 15536, 14400, 13428, 12686, 11877, 11250, 10625, 10122, 9740, 9314, 8892))
ggplot(dat, aes(x = x)) +
geom_col(aes(y = (y2 - 14000) / 8000), fill = 'red') +
geom_line(aes(y = y1, colour = 'TempMax'), size = 1.2) +
scale_y_continuous(name = 'Temp', limits = c(-3, 3),
sec.axis = sec_axis(~(.x * 8000 + 14000), name = 'var'))
With this I am getting below plot
This plot looks strange to me. Why my bars for y2 going to negative territory?
I also tried with changing the transformation as,
ggplot(dat, aes(x = x)) +
geom_col(aes(y = (y2 - 8000) / 8000), fill = 'red') +
geom_line(aes(y = y1, colour = 'TempMax'), size = 1.2) +
scale_y_continuous(name = 'Temp', limits = c(-3, 3),
sec.axis = sec_axis(~(.x * 8000 + 8000), name = 'var'))
With this, I get below plot
As you see, the range for secondary y axis still going into negative zone. I would ideally expect to see limit of secondary y axis starting from 0. Any idea on how can I make this correction will be very helpful.
Solution 1:[1]
In order to adjust/manipulate/force the sec.axis to start at 0 you can use expand = c(-3, 3)
Sample code:
library(ggplot2)
library(quantmod)
ggplot(dat, aes(x = x)) +
geom_col(aes(y = (y2 - 14000) / 8000), fill = 'red') +
geom_line(aes(y = y1, colour = 'TempMax'), size = 1.2) +
scale_y_continuous(name = 'Temp',
sec.axis = sec_axis(~(.x * 8000 + 14000), name = 'var'), expand = c(-3, 3))
Plot:
With regards to your remark - standalone basis of the sec.axis
with expand =c (-3,3)
and with limits=c(-3,3)
For the second code, I would use expand=c(0,1)
You could scale the x axis as well
Sample code:
ggplot(dat, aes(x = x)) +
geom_col(aes(y = (y2 - 8000) / 8000), fill = 'red') +
geom_line(aes(y = y1, colour = 'TempMax'), size = 1.2) +
scale_y_continuous(name = 'Temp', expand = c(0, 0),
sec.axis = sec_axis(~(.x * 8000 + 8000), name = 'var'))+
scale_x_continuous(expand=c(0,0))+
theme_bw()
Plot:
dat = data.frame(x = as.yearqtr(seq(as.Date('2002-01-01'), length.out = 17, by = '95 day')),
y1 = c(-0.0042, 0.0221, 0.0387, 0.0523, -0.0743, -0.0903, 0.0997, 0.0982, 0.0956, -0.0961, -0.0696, -0.0818, 0.0766, 0.0654, 0.1275, 0.1178, 0.0878),
y2 = c(19747, 19343, 19007, 18336, 17534, 16548, 15536, 14400, 13428, 12686, 11877, 11250, 10625, 10122, 9740, 9314, 8892))
Solution 2:[2]
Would this suit your needs?
ggplot(dat, aes(x = x)) +
geom_col(aes(y = (y2 / 8000), fill = 'red') +
geom_line(aes(y = y1, colour = 'TempMax'), size = 1.2) +
scale_y_continuous(name = 'Temp', limits = c(-0.2, 3),
sec.axis = sec_axis(~(.x * 8000), name = 'var'))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Gnueghoidune |








