使用因子排序ggplot x轴日期

时间:2019-07-01 19:14:47

标签: r ggplot2

如果我有一个data.frame df

library(lubridate)
library(ggplot2)
library(dplyr)

day.start = today()

df = data.frame(date = seq.Date(from = today() - days(10), to = today() + days(10), 'day'))
df$day.idx = as.numeric(df$date - day.start + 1)
df$day.idx = ifelse(df$day.idx < 1, df$day.idx + nrow(df), df$day.idx)
df = df %>% arrange(day.idx)            
df$value = 1:nrow(df)

我可以像这样绘制值与日期的关系

ggplot(df) + 
  geom_line(aes(x = date, y = value))

但是我想对自定义年份进行调整,因此我希望x轴从今天的日期开始。

## I want the x axis to start with day.start
df = df %>% mutate(date = factor(date, levels = as.character(df$date)))

## how to change x axis to behave like dates again?
ggplot(df) + 
  geom_point(aes(x = date, y = value)) +
  geom_line(aes(x = date, y = value)) ## where is this line?

所以第二个图看起来更好,但是如何格式化x轴使其看起来又像(间隔很好)日期呢?

 for

4 个答案:

答案 0 :(得分:0)

如果您的代码保持不变,我会收到以下消息:

  

geom_path:每个组仅包含一个观察值。您需要调整小组的审美吗?

所以ggplot不知道要“连接点”,这就是为什么该消息提到调整群体审美的原因。为了解决这个问题,我给gorup分配了1。

ggplot(df, aes(x = date, y = value, group = 1)) + 
  geom_point() +
  geom_line()

enter image description here

但是,我不建议您使用这种方法,因为这样您的日期就会乱七八糟,就像我们调整您的x轴文字可以看到的那样:

df %>% 
  ggplot(aes(x = date, y = value, group = 1)) + 
  geom_point() +
  geom_line() + 
  theme(axis.text.x = element_text(angle = 75, hjust = 1))

enter image description here

编辑: 我建议将您的date字段保留为日期,并使用filter仅保留想要的观测值,然后再将其输送到ggplot中:

df %>% 
  mutate(date = as_date(date)) %>% 
  filter(date >= today()) %>% 
  ggplot(aes(x = date, y = value)) + 
  geom_line() + 
  scale_x_date()

enter image description here

答案 1 :(得分:0)

尝试:

df[df$date > today(), ] %>% 
   mutate(date = as.Date(date)) %>% 
   ggplot(aes(x = date, y = value)) + geom_line() + geom_point()

编辑:一种选择是使用filter而不是df[df$date > today(), ](检查@OTStats的答案)

答案 2 :(得分:0)

如果您希望x轴保持与示例相同的顺序,并且只希望它可读,则建议更改文本的角度:

ggplot(df) + 
  geom_point(aes(x = date, y = value), group = 1) +
  geom_line(aes(x = date, y = value), group = 1) +  
  theme(axis.text.x = element_text(angle = 75, hjust = 1))

答案 3 :(得分:0)

通过以下答案找到了解决方案:https://stackoverflow.com/a/26611593/5319229

事实证明,这与因素无关。

bdscale软件包非常有用。

library(lubridate)
library(ggplot2)
library(dplyr)

day.start = today()

df = data.frame(date = seq.Date(from = today() - days(10), to = today() + days(10), 'day'))
df$day.idx = as.numeric(df$date - day.start + 1)
df$day.idx = ifelse(df$day.idx < 1, df$day.idx + nrow(df), df$day.idx)
df = df %>% arrange(day.idx)            
df$value = 1:nrow(df)

ggplot(df) + 
  geom_point(aes(x = date, y = value)) +
  geom_line(aes(x = date, y = value, group = 1))+
  bdscale::scale_x_bd(business.dates = df$date, labels = scales::date_format('%b %d'))