带有ggplot的线图(分类x轴)

假设我有一个日期不同的数据集:

d <- c("2019-01-01","2019-01-02","2019-01-03","2019-01-04","2019-04-06","2019-04-03","2019-05-07","2019-05-03","2019-06-03","2019-07-03","2019-07-04","2019-08-03","2019-09-05","2019-09-03","2019-09-06","2019-09-08","2019-10-03","2019-11-03","2019-12-03","2019-12-03")

df <- data.frame(dates=as.Date(d))

现在,我想绘制一个具有每个月病例数的时间序列:

ggplot(data=df,aes(dates))+geom_line(stat="bin")

然后我尝试在x轴上放置12个月。不幸的是,使用geom_line()时,我只能使用连续变量。所以,这给了我一个错误:

ggplot(data=df,aes(format(dates,"%m")))+geom_line(stat="bin")

如果我使用geom_bar()可以工作,但是我需要线条而不是线条。感谢您的帮助!

WW30824 回答:带有ggplot的线图(分类x轴)

您在这里混杂了一些东西。

geom_*(stat="bin")首先根据您拥有的任何aes(group=?)对数据进行分组,然后将观察到的范围分为30个大小相等的bin。运行第一行时,您会收到警告:

  

stat_bin()使用bins = 30。用binwidth选择更好的价值。

如果查看该图,它并没有根据日期和月份进行分类,而是计算了1月1日至12月3日这30个时期中每个时期的发生次数。这大约是11天的时间。

您需要自己进行装箱:

library(dplyr)
library(lubridate)
df %>% group_by(month=month(dates)) %>% count
# A tibble: 10 x 2
# Groups:   month [10]
   month     n
   <dbl> <int>
 1     1     5
 2     4     2
 3     5     5
 4     6     7
 5     7     3
 6     8     1
 7     9     5
 8    10     1
 9    11     6
10    12     2

df %>% group_by(month=month(dates)) %>% count() %>%
  ggplot(aes(x=month,y=n)) + geom_line()

其余的,获取带有格式化的x轴标签。

df %>% group_by(month=month(dates)) %>% count() %>%
  ggplot(aes(x=month,y=n)) + geom_line() + 
  scale_x_continuous(breaks=1:12,labels=month.name)

您可能会喜欢theme(panel.grid.minor.x = element_blank())

,

这是您要寻找的吗?

# aggregate data
df_plot <- df %>% mutate(month = lubridate::floor_date(dates,"month")) %>% 
                 group_by(month) %>% summarise(count = n())

# plot data
ggplot(aes(x = month,y = count),data = df_plot) + geom_line() +
  scale_x_date(date_breaks = "months",date_labels = "%b \n%Y") +
  labs(title = "Datecount")

enter image description here

本文链接:https://www.f2er.com/3103505.html

大家都在问