【问题标题】:create boxplot with year bars from continuous date data with fill从带有填充的连续日期数据创建带有年份条的箱线图
【发布时间】:2019-04-09 13:13:52
【问题描述】:

我正在尝试用两种不同的填充方式制作一个箱线图,其中包含多年来的连续日期数据(但每年只有一个条形图)。我的数据看起来很简单:

date,       number,  type
2007-06-07  2        tot
2007-06-09  3        tot
2007-06-12  0        gps

所以我导入了我的数据并使用 te as.Date 函数格式化了日期,并且它起作用了。我将所有的 NA 都更改为 0。然后我尝试了以下代码:

p1 <- ggplot(sights, aes(x =date, y = number, group=date, fill = type)) + 
 geom_boxplot(alpha=0.7)

 p1

我有一个类似的情节: plot

所以我尝试了如下代码:

  p1 <- p1+scale_x_date(breaks = as.Date(c("2005", "2006", "2007","2009", "2010", "2011", "2012", "2013",  "2014","2015", "2016","2017","2018")))                                          
p1

但它不起作用。能得到一些帮助真是太棒了!我也想在一个月后做同样的事情;)

编辑:我在 2101 年左右有一些异常值。我删除了它们,现在我得到: plot2

【问题讨论】:

  • 您的完整数据中是否存在日期不正确 (> 2100) 的案例?
  • 天哪,我有!它总是杀死小东西..让我看看它是如何变化的..
  • 我删除了异常值,但我仍然没有得到我正在寻找的图表。我用新的编辑了帖子

标签: r date ggplot2 boxplot


【解决方案1】:

这个输出接近你想要的吗?

sights %>% 
  mutate(year = format.Date(date, "%Y")) %>% 
  ggplot(aes(x = year, y = number, fill = type)) + 
  geom_boxplot(alpha=0.7)

【讨论】:

  • 哦,这很好!我考虑过手动添加仅包含年份的另一列,但是使用 format.date 效果很好!只是没有组 =year 评论 :) 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多