【问题标题】:R: Remove range x axis without data (NA) in a graphR:删除图表中没有数据(NA)的范围x轴
【发布时间】:2021-04-15 18:48:37
【问题描述】:

我正在尝试从 ggplot 中删除一系列 x 轴。我的数据 x 代表年和周:

202045:2020 年第 45 周

202053:2020 年的最后一周(任何一年都有 52-53 周,没有更多...)

 summary(df$year_week)

最小。第一曲。中位数平均第三曲。最大限度。 202045 202047 202050 202054 202052 202101

可悲的是我的数据从 2020 年的上周“跳跃”到 2021 年的第一周,并以“幽灵”周显示 x 轴,例如:

year_week=rep(c(202045,202046,202047,202048,202049,202050,202051,202052,202053,202101),times=1)
cases=rnorm(200, 44, 33)
df=data.frame(year_week, cases)

ggplot(df, aes(x=year_week, y=cases))+
geom_line()+
theme(axis.text.x = element_text(angle = 45,  
    hjust = 0.85, size=9))+
scale_x_continuous(limits=c(202045, 202101))

我尝试用 NA 删除,但结果是一样的

df$year_week[df$year_week>202053 & df$year_week<202101]= NA
df$cases[df$year_week>202053 & df$year_week<202101]= NA

ggplot(na.omit(df), aes(x=year_week, y=cases))+
geom_line()+
theme(axis.text.x = element_text(angle = 45,  
    hjust = 0.85, size=9))+
scale_x_continuous(limits=c(202045, 202101))

df %>%
filter(!is.na(cases)) %>%
ggplot(aes(x=year_week, y=cases))+
geom_line()+
theme(axis.text.x = element_text(angle = 45,  
    hjust = 0.85, size=9))+
scale_x_continuous(limits=c(202045, 202101))

我的预期图表是:(任何年份都不存在第 60 周或第 80 周)

【问题讨论】:

    标签: r ggplot2 time-series x-axis


    【解决方案1】:

    问题是您的 year_week 变量是一个数字。但是,当周停止在 52(或 53)时,例如202052 在下一年的第一周开始之前,您会得到 48 = 202101 - 202052 - 1 周的间隔。您可以通过使用as.characteryear_week 变量转换为字符来防止这种情况发生。或者你可以做一些格式化,例如拆分年份和星期,并在其间添加连字符、空格、...,就像我在代码中所做的那样:

    注意:转换为字符时必须使用group aes。

    year_week=rep(c(202045,202046,202047,202048,202049,202050,202051,202052,202053,202101),times=1)
    cases=rnorm(200, 44, 33)
    df=data.frame(year_week, cases)
    
    df$year_week <- paste(substr(df$year_week, 1, 4), substr(df$year_week, 5, 6), sep = "-")
    
    library(ggplot2)
    ggplot(df, aes(x=year_week, y=cases, group = 1))+
      geom_line()+
      theme(axis.text.x = element_text(angle = 45,  
                                       hjust = 0.85, size=9))
    

    【讨论】:

      【解决方案2】:

      您可以制作两个单独的图,一个用于 2020 年之前,另一个从 2021 年开始,并使用刻面将它们以较小的边距彼此相邻。我认为这可以实现您的目标,而不会因 x 轴标签的任意跳跃而使您的观众感到困惑。

      可能是这样的:

      df %>% 
        mutate(
          period = case_when(
            year_week < 202101 ~ "Before 2021",
            year_week >= 202101 ~ "After 2021"
          ),
          period = factor(
            period, 
            levels = c("Before 2021", "After 2021"), 
            ordered = T
          )
        ) %>% 
        ggplot() +
        geom_line(
          aes(
            year_week,
            cases
          )
        ) +
        facet_wrap(
          ~period,
          ncol = 2, 
          scales = "free_x"
        )+
        theme(axis.text.x = element_text(angle = 45,  
                                         hjust = 0.85, size=9))
      

      与您的问题没有直接关系的另一个问题是,您正在为 x 轴值上的每个值绘制多个 y 值,这会导致使用 geom_line 由粗糙的对角线连接的难看的垂直线。

      【讨论】:

      • 谢谢,一年没有 53 周了。不可能用 x 轴标签的任意跳跃来混淆我的观众。对不起,也许我的数据不是最好的......
      • 是的,您每周有 20 个值,这不适用于 geom_line。如果您喜欢我的回答解决您的 x 轴问题,请继续并给它一个绿色复选标记。
      • 我会等待其他更好的解决方案,感谢您的时间。
      猜你喜欢
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2020-04-07
      • 2017-10-19
      • 1970-01-01
      相关资源
      最近更新 更多