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