如何限制主要 y 轴和次要 y 轴?

时间:2021-05-18 09:10:30

标签: r ggplot2

我有这个代码,我想将主 y 轴限制在 0 到 3500 之间(左侧的 y 轴)。

第二个 y 轴我希望限制在 30 到 65 之间(右侧的 y 轴)。

两个 y 轴需要相互独立。

我的代码:

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

1 个答案:

答案 0 :(得分:0)

应用我概述的方法 here,产生以下代码:

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

reprex package (v1.0.0) 于 2021 年 5 月 18 日创建