简体   繁体   English

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

[英]How to limit primary y-axis and secondary y-axis?

I've got this code and i want to limit the primary y-axis between 0 and 3500 (the y-axis on the left).我有这段代码,我想将主 y 轴限制在 0 到 3500 之间(左侧的 y 轴)。

The second y-axis i want a limit between 30 and 65 (the y-axis on the right).第二个 y 轴我想要 30 到 65 之间的限制(右侧的 y 轴)。

The two y-axis need to be independent of each other.两个 y 轴需要相互独立。

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

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)reprex package (v1.0.0) 于 2021 年 5 月 18 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM