如何更改图表中的开始月份

Posted

技术标签:

【中文标题】如何更改图表中的开始月份【英文标题】:How can I change the start month in the graph 【发布时间】:2022-01-21 08:05:17 【问题描述】:

我正在 R 中创建一个图表,显示每月的骑行总数。实际上,开始月份是2020年4月,结束月份是2021年3月。这种安排对于别人的分析和解释具有重要意义。但是,由于 R 自动从 1 => 12 开始排序,所以我想问一下是否有其他方法可以重新排列?

library(ggplot2);library(dplyr)

Bike %>%
  group_by(member_casual, Date_month) %>%
  summarise(.groups = 'drop',
            average_duration = mean(Time_duration)) %>%
  arrange(member_casual, Date_month) %>%
  mutate(Average_Time = hms(average_duration)) %>%
  ggplot(aes(
    x = Date_month,
    y = Average_Time,
    group = member_casual,
    colour = member_casual
  )) +
  geom_line() + geom_point(size = 3) +
  scale_colour_manual(name = "Membership Type",
                      values = c(casual = '#ff9900', member = '#0099cc')) +
  labs(
    title = "Average Ride Duration By Month",
    x = "Month",
    y = "Average Time",
    subtitle = "April 2020 to March 2021"
  )

【问题讨论】:

什么是Bike?请通过reproducible examples提问,谢谢! Date_month 更改为您喜欢的排序因素 【参考方案1】:

如果您也有年份,您可以使用包lubridate 创建日期并沿 x 轴绘制日期。那么顺序应该是固定的,我认为名字看起来也更好。

以 Andrew 的示例为基础,我们可以这样做:

library(dplyr)
library(ggplot2)

data %>% 
  mutate(date = lubridate::make_date(year = year, month = month)) %>% 
  ggplot(aes(
    x = date,
    y = value,
    group = group
  )) +
  geom_line() + 
  geom_point(size = 3) +
  scale_colour_manual(name = "Membership Type",
                      values = c(casual = '#ff9900', member = '#0099cc')) +
  labs(
    title = "Average Ride Duration By Month",
    x = "Date",
    y = "Average Time",
    subtitle = "April 2020 to March 2021"
  )

输出:

数据:


data <- structure(
  list(
    group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
    month = 1:12,
    year = c(rep(2021, 3), rep(2020, 9)),
    value = c(
      22.1462195669301,
      34.7227226593532,
      11.3724566600285,
      37.9181093303487,
      11.8698693322949,
      17.4393743579276,
      21.4907402452081,
      17.6362748816609,
      29.6476712427102,
      36.9375107018277,
      25.3655201010406,
      20.449273835402
    )
  ),
  class = "data.frame",
  row.names = c(NA, -12L)
)

【讨论】:

【参考方案2】:

正如@rawr 提到的,您可以使用factor 设置沿x 轴的顺序。

library(tidyverse)

data %>%
  ggplot(aes(
    x = factor(month, levels = c(4:12, 1:3)),
    y = value,
    group = group
  )) +
  geom_line() + 
  geom_point(size = 3) +
  scale_colour_manual(name = "Membership Type",
                      values = c(casual = '#ff9900', member = '#0099cc')) +
  labs(
    title = "Average Ride Duration By Month",
    x = "Month",
    y = "Average Time",
    subtitle = "April 2020 to March 2021"
  )

输出

数据

data <- structure(
  list(
    group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
    month = 1:12,
    value = c(
      22.1462195669301,
      34.7227226593532,
      11.3724566600285,
      37.9181093303487,
      11.8698693322949,
      17.4393743579276,
      21.4907402452081,
      17.6362748816609,
      29.6476712427102,
      36.9375107018277,
      25.3655201010406,
      20.449273835402
    )
  ),
  class = "data.frame",
  row.names = c(NA, -12L)
)

【讨论】:

谢谢大家 :> 我找到了解决这个问题的其他方法。我用年份提取了 date_month,所以结果符合预期。

以上是关于如何更改图表中的开始月份的主要内容,如果未能解决你的问题,请参考以下文章

如何使用VBA更改Excel图表中的数据标签宽度?

如何使用ggplot2格式化带有轴标签和旋转的R中的雷达图

如何更改 xAxis 标签 iOS 图表 (Swift 2.3)

如何更改图表的颜色。R中PerformanceAnalytics库中的相关性?

根据R中的selectinput值更改图表

如何从 Perl 中的随机纪元时间戳获取纪元形式的月份开始?