【问题标题】:Organize scale of x axis of time series graph组织时间序列图的x轴比例
【发布时间】:2020-04-27 01:26:01
【问题描述】:

我的数据如下所示:

# Data
df <- data.frame("Hospital" = c("Buge Hospital", "Buge Hospital", "Greta Hospital", "Greta Hospital",
                                "Makor Hospital", "Makor Hospital"),
                 "Period" = c("Jul-18","Aug-18", "Jul-19","Aug-19", "Jul-20","Aug-20"),
                 "Medical admissions" = c(12,56,0,40,5,56),
                 "Surgical admissions" = c(10,2,0,50,20,56),
                 "Inpatient admissions" = c(9,5,6,0,60,96))

现在这个数据有一个名为 period 的列,它是不同年份的月度数据,2018、2019 和 2020

如果我绘制这个数据,它看起来是这样的

library(ggplot2
# Melt data into long format
df2 <- melt(data = df,
                id.vars = c("Hospital","Period"), 
                measure.vars = names(df[3:5]))

# Stacked barplot
ggplot( df2, aes(x = Period, y = value, fill = variable, group = variable)) +
  geom_bar(stat = "identity") +
  theme(legend.position = "none") +
  ggtitle(unique(df2$Hospital))+
  scale_x_date(date_labels = %Y)+
  labs(x = "Month", y = "Number of People", fill = "Type")

绘图很好,但 x 轴没有按升序排列,我尝试使用 scale_x_date 函数但绘图仍然相同。我想要的是 2018 年开始的几个月,然后是 2019 年和 2020 年的几个月。我的意思是 x 轴要根据这样的年份按升序排列 8 月 18 日7 月 18 日8 月 19 日7 月 19 日8 月 20 日,7 月 20 日

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    要解决您的问题,您需要将 Period 转换为日期格式。

    例如,您可以使用 lubridate 包中的 parse_date 函数:

    library(lubridate)
    library(tidyr)
    library(dplyr)
    
    df %>% mutate(Date = parse_date(as.character(Period), format = "%b-%y")) %>%
      pivot_longer(cols = Medical.admissions:Inpatient.admissions, names_to = "Var", values_to = "Val")
    
    # A tibble: 18 x 5
       Hospital       Period Date       Var                    Val
       <fct>          <fct>  <date>     <chr>                <dbl>
     1 Buge Hospital  Jul-18 2018-07-01 Medical.admissions      12
     2 Buge Hospital  Jul-18 2018-07-01 Surgical.admissions     10
     3 Buge Hospital  Jul-18 2018-07-01 Inpatient.admissions     9
     4 Buge Hospital  Aug-18 2018-08-01 Medical.admissions      56
     5 Buge Hospital  Aug-18 2018-08-01 Surgical.admissions      2
     6 Buge Hospital  Aug-18 2018-08-01 Inpatient.admissions     5
     7 Greta Hospital Jul-19 2019-07-01 Medical.admissions       0
     8 Greta Hospital Jul-19 2019-07-01 Surgical.admissions      0
     9 Greta Hospital Jul-19 2019-07-01 Inpatient.admissions     6
    10 Greta Hospital Aug-19 2019-08-01 Medical.admissions      40
    11 Greta Hospital Aug-19 2019-08-01 Surgical.admissions     50
    12 Greta Hospital Aug-19 2019-08-01 Inpatient.admissions     0
    13 Makor Hospital Jul-20 2020-07-01 Medical.admissions       5
    14 Makor Hospital Jul-20 2020-07-01 Surgical.admissions     20
    15 Makor Hospital Jul-20 2020-07-01 Inpatient.admissions    60
    16 Makor Hospital Aug-20 2020-08-01 Medical.admissions      56
    17 Makor Hospital Aug-20 2020-08-01 Surgical.admissions     56
    18 Makor Hospital Aug-20 2020-08-01 Inpatient.admissions    96
    

    那么,您可以使用scale_x_date 在您的 x 轴上设置适当的标签选项:

    library(lubridate)
    library(tidyr)
    library(dplyr)
    library(ggplot2)
    
    df %>% mutate(Date = parse_date(as.character(Period), format = "%b-%y")) %>%
      pivot_longer(cols = Medical.admissions:Inpatient.admissions, names_to = "Var", values_to = "Val") %>%
      ggplot(aes(x = Date, y = Val, fill= Var, group = Var))+
      geom_col()+
      scale_x_date(date_breaks = "month", date_labels = "%b %Y")+
      labs(x = "Month", y = "Number of People", fill = "Type")+
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
    

    它回答了你的问题吗?


    编辑:使用 `lubridate v1.7.8

    lubridate 版本 1.7.8 上,parse_date 不再存在。您必须将其替换为 parse_date_time,如下所示:

    library(lubridate)
    library(dplyr)
    
    df %>% mutate(Date = ymd(parse_date_time2(as.character(Period), orders = "%b-%y"))) %>% ....
    

    【讨论】:

    • 是否可以显示隐藏月份?
    • 查看我的更新答案。让我知道它是否是您正在寻找的。​​span>
    • 完美。谢谢
    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    相关资源
    最近更新 更多