简体   繁体   English

如何在 R 的折线图中添加第二个 x 轴

[英]how to add a second x-axis to a line chart in R

First is the data and the manipulations.首先是数据和操作。 How would I add a second y-axis to this.我将如何为此添加第二个 y 轴。 the desired would have 2018 under the first value (Dec), 2019 under the next 12, and then 2020 under the next 11. would this be something where I would use annotate.期望值将在第一个值(Dec)下有 2018 年,在下一个 12 下有 2019 年,然后在下一个 11 下有 2020 年。这将是我使用注释的地方。 Furthermore, the year would be horizontal and left justified directly below the first month of the year (or dec in the case of 2018)此外,年份将是水平的,并且在一年的第一个月(或 2018 年的 12 月)正下方左对齐

I have seen other questions like this but the answers have items such as xlim that is not needed being that there will be only 24 items in the dataset.我见过其他类似的问题,但答案有 xlim 等项目,因为数据集中只有 24 个项目,所以不需要这些项目。

 #Data generation
 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)
       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,Rate)

      #Chart
      library(ggplot2)
      library(dplyr)
      library(lubridate)
      library(scales)
      library(odbc)


     ## chart
     linechart<-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_line(aes(x=date,y=Rate),color="red")+scale_y_continuous(labels = 
     scales::percent)+scale_x_date(date_breaks="1 month", date_labels="%b\n")+theme(panel.grid.major 
     = element_blank(),axis.text.x = element_text(angle = 90, size=rel(0.6)))+ggtitle("Employment 
     Growth (%)")
     print(linechart)

Try this:尝试这个:

library(ggplot2)
library(dplyr)
library(lubridate)
library(scales)
## chart
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_line(aes(x=date,y=Rate),color="red")+
  scale_y_continuous(labels =scales::percent)+
  scale_x_date(date_breaks="1 month", date_labels="%b\n",
               sec.axis = dup_axis(labels = function(x) format(x,'%Y')))+
  theme(panel.grid.major= element_blank(),
  axis.text.x = element_text(angle = 90, size=rel(0.6)))+ggtitle("Employment 
     Growth (%)")

Output: Output:

在此处输入图像描述

Another option:另外的选择:

#Code2
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_line(aes(x=date,y=Rate),color="red")+
  scale_y_continuous(labels =scales::percent)+
  scale_x_date(date_breaks="1 month", date_labels="%b\n",
               expand = c(0, 0))+
  facet_wrap(.~year,scales = 'free_x',strip.position = 'bottom')+
  theme(panel.grid.major= element_blank(),
        axis.text.x = element_text(angle = 90, size=rel(0.6)),
        panel.spacing = unit(0, "lines"),
        strip.placement = 'outside',
        strip.background = element_blank())+
        ggtitle("Employment 
     Growth (%)")

Output: Output:

在此处输入图像描述

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

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