R-ggplot2问题,日期作为x轴的字符 [英] R - ggplot2 issues with date as character for x-axis

查看:19
本文介绍了R-ggplot2问题,日期作为x轴的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R和ggplot2的新手,我无法弄清楚如何解决我要创建的图形的问题.

I'm new to using R and ggplot2, and I cannot figure out how to fix the issue with the graph I am trying to create.

这是当前图表的外观.我在x轴上有日期,但是由于某些原因,它们不适用于年份,只能按月和日排序.

Here is what the graph looks like at the moment. I have dates on the x-axis, but for some reason they don't work with the year, but only order by the month and day.

以下是我正在处理的数据的屏幕截图:

Here is a screenshot of the data I am working with:

如您所见,这里的顺序看起来正确.

As you can see, the order looks correct here.

我制作了一个可重现的示例,其中发生了相同的问题

I produced a re-creatable sample where the same issue occurs

Week <- c("1/6/2019", "1/26/2020", "6/7/2020")
Coronavirus <- c(0, 16, 67)
Grubhub <- c(65, 23, 59)
UberEats <- c(52, 80, 68)
Doordash <- c(27, 35, 50)

my.data <- data.frame(Week, Coronavirus, Grubhub, UberEats, Doordash)
my.data

test.output <- ggplot(data = my.data, aes(x = Week, group = 1)) +
  geom_line(aes(y = Coronavirus), color = "red") +
  geom_line(aes(y = Grubhub), color = "darkgreen") +
  geom_line(aes(y = UberEats), color = "blue") +
  geom_line(aes(y = Doordash), color = "purple") +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title = "Weekly Google Search Term Interest by Category", y = "Search Term Interest", x = "Week [2020]") 
test.output

此处在x轴上的日期顺序也不正确.

The order of the dates on the x-axis are incorrect here as well.

关于如何解决此问题的任何建议?

Any suggestions on how I can fix the issue?

推荐答案

  1. "1/6/2019" 不是日期,而是字符串. ggplot2 (以及大多数其他事情)绝对不能推断您希望将其作为日期处理.它做什么是知道"的.是因为它是一个字符串,并且由于它不是 factor ,所以按字典顺序(不是按年排序)对事物进行排序.请注意,这与您观察到的结果相符,因为它是字符串中的前几个字符,所以它首先按月排序,然后按天排序.

  1. "1/6/2019" is not a date, it is a string. ggplot2 (and most other things) should never infer that you want it dealt with as a date. What it does "know" is that it is a string, and since it is not a factor, it orders things lexicographically (not year-first). Note that this matches your observation that it sorts first by month, then day, since those are the first few characters in the strings.

如果您希望将演示文稿保留在%m/%d中,则一旦将 Week 列设置为适当的 Date 类,/%Y" 格式,则需要添加 scale_x_date .

Once we make the Week column a proper Date class, if you want to keep the presentation in the "%m/%d/%Y" format, you need to add scale_x_date.

Week <- c("1/6/2019", "1/26/2020", "6/7/2020")
Coronavirus <- c(0, 16, 67)
Grubhub <- c(65, 23, 59)
UberEats <- c(52, 80, 68)
Doordash <- c(27, 35, 50)

my.data <- data.frame(Week, Coronavirus, Grubhub, UberEats, Doordash)
my.data$Week <- as.Date(my.data$Week, format = "%m/%d/%Y")
my.data

test.output <- ggplot(data = my.data, aes(x = Week, group = 1)) +
  geom_line(aes(y = Coronavirus), color = "red") +
  geom_line(aes(y = Grubhub), color = "darkgreen") +
  geom_line(aes(y = UberEats), color = "blue") +
  geom_line(aes(y = Doordash), color = "purple") +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title = "Weekly Google Search Term Interest by Category", y = "Search Term Interest", x = "Week [2020]") 
test.output

如果您更喜欢%m/%d/%Y" ,则

test.output + scale_x_date(date_labels = "%m/%d/%Y")

这篇关于R-ggplot2问题,日期作为x轴的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆