首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何向R中的折线图添加第二个x轴

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

Stack Overflow用户
提问于 2021-01-12 08:40:20
回答 1查看 121关注 0票数 1

首先是数据和操作。我该如何给它添加第二个y轴呢?所需的值将在第一个值(Dec)下为2018,在下一个12下为2019,然后在下一个11下为2020。这是我使用annotate的地方吗?此外,年份将是水平的,并在一年的第一个月(或在2018年的情况下为12月)下方直接左对齐。

我见过其他类似的问题,但答案中有xlim这样的项,这是不需要的,因为数据集中只有24个项。

代码语言:javascript
复制
 #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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-12 08:52:11

试试这个:

代码语言:javascript
复制
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 (%)")

输出:

另一种选择:

代码语言:javascript
复制
#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 (%)")

输出:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65676515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档