1

I've got this code and i want to limit the primary y-axis between 0 and 3500 (the y-axis on the left).

The second y-axis i want a limit between 30 and 65 (the y-axis on the right).

The two y-axis need to be independent of each other.

My code:

    library(ggplot2)
SAMLET <- data.frame(Aar=c(2004, 2004, 2005, 2005, 2006, 2006, 2007, 2007, 2008, 2008, 2009, 2009, 2010, 2010, 2011, 2011, 2012, 2012, 2013, 2013, 2014, 2014, 2015, 2015, 2016, 2016, 2017, 2017, 2018, 2018, 2019, 2019),
                     Type=c("FV", "BG", "FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG"),
                     mia_kr=c(1918050,938350.0583, 2312210, 1035680.125, 2842071, 1182431.742, 2910107, 1301530.525, 2795094,1398043.475,2564554,1437696.875,2660361,1473309.825,2598022,1502774.742,2604446,1563715.417,2624976,1607972.45,2716382,1635247.125,2899526,1651717.642,3026961,1676296.525,3175199,1710432.208,3300686,1776903.6,3399871,1864377.058))

SAMLET_2 <- data.frame(Aar=c(2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019),
                       BG=c(0.489221,0.447918,0.416046, 0.447245, 0.500177624,0.560603081,0.553800715,0.578430337,0.600402318,0.612566534,0.601994537,0.569650916,0.55378861,0.538685043,0.538343726,0.548366999 ))


 SAMLET$mia_kr <- SAMLET$mia_kr/1000   

ggplot() + 
  geom_bar(mapping = aes(x= SAMLET$Aar, y= SAMLET$mia_kr, fill = SAMLET$Type), stat="identity",position = "identity")+
  geom_line(mapping = aes(x= SAMLET_2$Aar, y = SAMLET_2$BG*10000, group = 1, colour = "BLG"),size = 1) +
  scale_y_continuous(labels = scales::format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE),
                     sec.axis = sec_axis(~ ./100,labels = scales::format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE),"%" ))+
  
  xlab(" ") +
  ylab("Mia. kr.") +
  labs(title = "My dataplot", caption = "source: calc")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5), plot.caption = element_text(hjust = +3), axis.text.x = element_text(angle = 45, hjust = 0.9))+
  theme(legend.title=element_blank())+
  scale_fill_discrete(labels=c("BG", "FV", "BLG"))+
  scale_color_manual(values=c("#0000ff"))
  • "The two y-axis need to be independent of each other"; that's not how secondary axes work in ggplot2. The secondary one always needs to be a monotonic transformation of the primary. Have look at [this question](https://stackoverflow.com/q/3099219/11374827). – teunbrand May 18 '21 at 09:51
  • Looked at that post already. And this seems to be possible in answer number 5, the one about weather. But can't figure out how to reproduce this. – user15949318 May 18 '21 at 10:11

1 Answers1

0

Applying the approach I've outlined here, yields the following code:

library(ggplot2)
library(scales)

# Function factory for secondary axis transforms
train_sec <- function(primary, secondary) {
  from <- range(secondary)
  to   <- range(primary)
  # Forward transform for the data
  forward <- function(x) {
    rescale(x, from = from, to = to)
  }
  # Reverse transform for the secondary axis
  reverse <- function(x) {
    rescale(x, from = to, to = from)
  }
  list(fwd = forward, rev = reverse)
}

SAMLET <- data.frame(Aar=c(2004, 2004, 2005, 2005, 2006, 2006, 2007, 2007, 2008, 2008, 2009, 2009, 2010, 2010, 2011, 2011, 2012, 2012, 2013, 2013, 2014, 2014, 2015, 2015, 2016, 2016, 2017, 2017, 2018, 2018, 2019, 2019),
                     Type=c("FV", "BG", "FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG","FV", "BG"),
                     mia_kr=c(1918050,938350.0583, 2312210, 1035680.125, 2842071, 1182431.742, 2910107, 1301530.525, 2795094,1398043.475,2564554,1437696.875,2660361,1473309.825,2598022,1502774.742,2604446,1563715.417,2624976,1607972.45,2716382,1635247.125,2899526,1651717.642,3026961,1676296.525,3175199,1710432.208,3300686,1776903.6,3399871,1864377.058))

SAMLET_2 <- data.frame(Aar=c(2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019),
                       BG=c(0.489221,0.447918,0.416046, 0.447245, 0.500177624,0.560603081,0.553800715,0.578430337,0.600402318,0.612566534,0.601994537,0.569650916,0.55378861,0.538685043,0.538343726,0.548366999 ))


SAMLET$mia_kr <- SAMLET$mia_kr/1000

sec <- train_sec(c(0, 3500), c(30, 65))


ggplot() + 
  geom_bar(mapping = aes(x= Aar, y= mia_kr, fill = Type), 
           stat="identity",position = "identity", data = SAMLET)+
  geom_line(mapping = aes(x= Aar, y = sec$fwd(BG * 100), 
                          group = 1, colour = "BLG"),
            size = 1, data = SAMLET_2) +
  scale_y_continuous(
    labels = scales::format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE),
    sec.axis = sec_axis(~ sec$rev(.),
                        labels = scales::format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE),"%" )
  )+
  xlab(" ") +
  ylab("Mia. kr.") +
  labs(title = "My dataplot", caption = "source: calc")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5), 
        plot.caption = element_text(hjust = +3), 
        axis.text.x = element_text(angle = 45, hjust = 0.9))+
  theme(legend.title=element_blank())+
  scale_fill_discrete(labels=c("BG", "FV", "BLG"))+
  scale_color_manual(values=c("#0000ff"))

Created on 2021-05-18 by the reprex package (v1.0.0)

teunbrand
  • 33,645
  • 4
  • 37
  • 63