0

有没有办法在 x 轴上显示所有日期?我有以下数据集,我想要一个 ggplot,其中在 x 轴上year_month是列,在 y 轴上是count列,但我希望它以比例显示所有月份,而不仅仅是像 ggplot2 通常那样做。

    library(ggplot2)
    library(lubridate)
    library(tsibble)
    library(dplyr)
    
    plt = structure(list(month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), year = c(2010, 
2012, 2012, 2012, 2013, 2013, 2014, 2014, 2014, 2014, 2014, 2014, 
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 
2014, 2014, 2014, 2014, 2014, 2014, 2014), website = c("efarsas", 
"efarsas", "efarsas", "efarsas", "efarsas", "efarsas", "boatos_org", 
"boatos_org", "boatos_org", "boatos_org", "boatos_org", "boatos_org", 
"boatos_org", "boatos_org", "boatos_org", "boatos_org", "boatos_org", 
"boatos_org", "boatos_org", "boatos_org", "boatos_org", "boatos_org", 
"boatos_org", "boatos_org", "boatos_org", "boatos_org", "boatos_org", 
"boatos_org", "boatos_org", "boatos_org"), count = c(1L, 3L, 
3L, 3L, 2L, 2L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L), year_month = structure(c(14610, 15340, 15340, 15340, 
15706, 15706, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071, 
16071, 16071, 16071, 16071, 16071, 16071, 16071, 16071), class = c("yearmonth", 
"vctrs_vctr"))), row.names = c(NA, -30L), groups = structure(list(
    month = c(1, 1, 1, 1), year = c(2010, 2012, 2013, 2014), 
    website = c("efarsas", "efarsas", "efarsas", "boatos_org"
    ), .rows = structure(list(1L, 2:4, 5:6, 7:30), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 4L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

我试过了

plt %>% 
  ggplot() +
  geom_line(aes(x = year_month, y= count, color = website)) +
  labs(color = "Fact-Checking Websites",
       x = "Month/Year (2010-2020)",
       y = "Quantity")+
   theme_minimal()

但没有取得成功,因为它只显示了一些years months写在 x 轴上的内容。

4

1 回答 1

1

您可以使用该breaks参数给出一个每月返回的函数。由于您的数据跨度超过 3 年,这是很多中断...

plt %>% 
  ggplot() +
  geom_line(aes(x = year_month, y= count, color = website)) +
  labs(color = "Fact-Checking Websites",
       x = "Month/Year (2010-2020)",
       y = "Quantity")+
   theme_minimal() +
  scale_x_yearmonth(
    breaks = function(range) seq(range[1], range[2], by = 1),
    date_labels = "%m/%Y"
  )

在此处输入图像描述

于 2020-12-03T22:07:18.013 回答