Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将第二个y轴和折线图嵌入其中_R_Ggplot2_Scale_Axis - Fatal编程技术网

R 如何将第二个y轴和折线图嵌入其中

R 如何将第二个y轴和折线图嵌入其中,r,ggplot2,scale,axis,R,Ggplot2,Scale,Axis,首先是示例代码和我正在使用的操作 library(tidyverse) library(scales) Month1 <- c(201812,20191,20192,20193,20194,20195,20196, 20197,20198,20199,201910,201911,201912,20201 ,20202,20203,20204,20205,20206,20207 ,20208,20209,202010,2020

首先是示例代码和我正在使用的操作

   library(tidyverse)
   library(scales)
   Month1 <- c(201812,20191,20192,20193,20194,20195,20196,
        20197,20198,20199,201910,201911,201912,20201
        ,20202,20203,20204,20205,20206,20207
        ,20208,20209,202010,202011)
   annualjobgrowth<- c(44400,46000,42600,40500,42800,40500,36000,
                34000,32300,29900,21900,24500,21000,
                23300,16000,-6200,-275600,-249500,-149200,
                -136500,-129900,-122800,-113900,-109500)
    Rate <- 
   c(3.3,3.4,3.1,3.0,3.1,2.9,2.6,2.5,2.3,2.1,1.6,1.7,1.5,1.7,1.1,-0.4,
    -19.5,-17.6,-10.5,-9.6,-9.1,-8.6,-8.0,-7.7)
   cesyoy <- data.frame(Month1,annualjobgrowth, Rate)

   secondces<-cesyoy %>% mutate(year = substr(as.character(Month1),1,4),
                         month = substr(as.character(Month1),5,7),
                         date = as.Date(paste(year,month,"1",sep ="-"))) %>%
   ggplot() + geom_col(aes(x = date, y = annualjobgrowth),fill = "#00abff")+
   geom_line(aes(x=date, y=Rate),stat="identity")+
   scale_y_continuous(labels=comma,breaks=c(50000,0,-50000,-100000,-150000,-200000,-250000))+
   scale_x_date(date_breaks="1 month", date_labels="%b\n")+
   theme(axis.text.x = element_text(angle = 90, size=rel(0.5))) +
   scale_fill_brewer(palette="Dark2")

我的问题是如何将其与现有代码融合。最终结果将是我现有的条形图,其中有一条线表示汇率变量。如何添加第二个缩放连续项?

一种方法是预计算主次轴最大值之间的比率。然后你可以用这个比例来衡量两者

Ratio <- max(cesyoy$annualjobgrowth)/max(cesyoy$Rate)

cesyoy %>% 
  mutate(year = substr(as.character(Month1),1,4),
         month = substr(as.character(Month1),5,7),
         date = as.Date(paste(year,month,"1",sep ="-"))) %>%
ggplot() + 
  geom_col(aes(x = date, y = annualjobgrowth), fill = "#00abff")+
  geom_line(aes(x=date, y=Ratio*Rate),stat="identity")+
  scale_y_continuous(labels=scales::comma,breaks=c(50000,0,-50000,-100000,-150000,-200000,-250000), name = "Growth",
                     sec.axis = sec_axis(~./Ratio, name = "Rate"))+
  scale_x_date(date_breaks="1 month", date_labels="%b\n")+
  theme(axis.text.x = element_text(angle = 90, size=rel(0.5))) +
  scale_fill_brewer(palette="Dark2") 
比率%
突变(年份=子字符(如字符(Month1),1,4),
月份=子字符(如字符(Month1),5,7),
日期=截止日期(粘贴(年,月,“1”,九月=“-”))%>%
ggplot()+
geom_col(aes(x=日期,y=年度就业增长),fill=“#00abff”)+
几何线(aes(x=日期,y=比率*比率),stat=“identity”)+
连续缩放(标签=缩放::逗号,断点=c(50000,0,-50000,-100000,-150000,-200000,-250000),name=“增长”,
秒轴=秒轴(~./比率,name=“速率”))+
缩放日期(日期间隔=“1个月”,日期标签=“%b\n”)+
主题(axis.text.x=element_text(角度=90,大小=rel(0.5)))+
缩放填充酿酒器(调色板=“深色”)

一种方法是预计算主次轴最大值之间的比率。然后你可以用这个比例来衡量两者

Ratio <- max(cesyoy$annualjobgrowth)/max(cesyoy$Rate)

cesyoy %>% 
  mutate(year = substr(as.character(Month1),1,4),
         month = substr(as.character(Month1),5,7),
         date = as.Date(paste(year,month,"1",sep ="-"))) %>%
ggplot() + 
  geom_col(aes(x = date, y = annualjobgrowth), fill = "#00abff")+
  geom_line(aes(x=date, y=Ratio*Rate),stat="identity")+
  scale_y_continuous(labels=scales::comma,breaks=c(50000,0,-50000,-100000,-150000,-200000,-250000), name = "Growth",
                     sec.axis = sec_axis(~./Ratio, name = "Rate"))+
  scale_x_date(date_breaks="1 month", date_labels="%b\n")+
  theme(axis.text.x = element_text(angle = 90, size=rel(0.5))) +
  scale_fill_brewer(palette="Dark2") 
比率%
突变(年份=子字符(如字符(Month1),1,4),
月份=子字符(如字符(Month1),5,7),
日期=截止日期(粘贴(年,月,“1”,九月=“-”))%>%
ggplot()+
geom_col(aes(x=日期,y=年度就业增长),fill=“#00abff”)+
几何线(aes(x=日期,y=比率*比率),stat=“identity”)+
连续缩放(标签=缩放::逗号,断点=c(50000,0,-50000,-100000,-150000,-200000,-250000),name=“增长”,
秒轴=秒轴(~./比率,name=“速率”))+
缩放日期(日期间隔=“1个月”,日期标签=“%b\n”)+
主题(axis.text.x=element_text(角度=90,大小=rel(0.5)))+
缩放填充酿酒器(调色板=“深色”)