简体   繁体   English

在 ggplot2 x 轴中添加所有年月日期

[英]Adding all Year Month dates in a ggplot2 x-axis

Is there a way to show all dates in the x-axis?有没有办法在 x 轴上显示所有日期? I have the following dataset and I wanted a ggplot in which in the x-axis I've year_month column, and in the y-axis the count column, BUT I wanted it to show all months in the scale, not just some as the ggplot2 usually does.我有以下数据集,我想要一个 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"))

I tried我试过了

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()

But no success achieved, as it only shows some years months written in the x-axis.但没有取得成功,因为它只在 x 轴上显示了years months

You can use the breaks argument to give a function returning each month.您可以使用breaks参数给出每月返回的function。 Since your data spans over 3 years, this is a lot of 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"
  )

在此处输入图像描述

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

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