【问题标题】:Plotting rectangles in ggplot2 - Invalid input: time_trans works with objects of class POSIXct only在 ggplot2 中绘制矩形 - 输入无效:time_trans 仅适用于 POSIXct 类的对象
【发布时间】:2015-09-15 16:08:28
【问题描述】:

我有这个数据

df <- structure(list(IndID = structure(c(16L, 15L, 14L, 13L, 12L, 11L, 
10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .Label = c("16", "15", 
"14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", 
"2", "1"), class = "factor"), StartDate = structure(c(1313042400, 
1312956000, 1313560800, 1363672800, 1374040800, 1374040800, 1374040800, 
1374040800, 1374040800, 1374040800, 1374040800, 1365832800, 1365919200, 
1366178400, 1395727200, 1395813600), class = c("POSIXct", "POSIXt"
)), EndDate = structure(c(1377928800, 1378015200, 1378015200, 
1386572400, 1410760800, 1410760800, 1410760800, 1410674400, 1410760800, 
1406959200, 1399356000, 1427868000, 1394517600, 1428213600, 1428040800, 
1420959600), class = c("POSIXct", "POSIXt"))), .Names = c("IndID", 
"StartDate", "EndDate"), row.names = c(NA, -16L), class = "data.frame")

  IndID  StartDate    EndDate
1     1 2011-08-11 2013-08-31
2     2 2011-08-10 2013-09-01
3     3 2011-08-17 2013-09-01
4     4 2013-03-19 2013-12-09
5     5 2013-07-17 2014-09-15
6     6 2013-07-17 2014-09-15

并且可以制作这个情节

    library(lubridate)
    require(gglopt2)
    df$IndID <- factor(df$IndID, levels = rev(df$IndID))



 p1 <- ggplot(df, aes(x=IndID, fill = IndID))+
      geom_rect(aes(x = IndID, xmin = as.numeric(IndID) - 0.45, xmax = as.numeric(IndID) + 0.45, ymin = StartDate, ymax = EndDate))+
      coord_flip()+
      xlab("IndID")+
      ylab("Time")+
      ggtitle("Individual Monitoring Periods")+
      geom_text(aes(y = StartDate + as.difftime(8, unit = "weeks"), label = paste(month(StartDate, label = T), year(StartDate), sep = "-"))) +
      geom_text(aes(y = EndDate - as.difftime(9, unit = "weeks"), label = paste(month(EndDate, label = T), year(EndDate), sep = "-")))

    p1

另外,我想在每年的 6 月 19 日到 10 月 19 日之间为该区域遮阴。为此,我制作了一个data.frame 日期,然后以新的data.frame 将其转换为POSIXct 格式。 (是的,这很笨重..)

temp <- data.frame(
    start = as.Date(c('2011-06-19', '2012-06-19', '2013-06-19', '2014-06-19', '2015-06-19')), 
    end   = as.Date(c('2011-10-19', '2012-10-19', '2013-10-19', '2014-10-19', '2015-10-19')))
str(temp)
dateRanges <- data.frame(
    start = as.POSIXct(temp [,1], "%Y-%m-%d") + hours(6),
    end   = as.POSIXct(temp [,2], "%Y-%m-%d") + hours(6))
str(dateRanges)

当我尝试使用以下代码将新矩形添加到绘图时,我收到帖子标题中指示的错误。

p1 + geom_rect(data = dateRanges, aes(xmin = start , xmax = end, ymin = -Inf, ymax = Inf), inherit.aes= F, alpha = 0.4, fill = c("lightblue"))

据我所知,通过查看 dateRanges 的 str(),它们被正确格式化为 POSIXct 类。

我确实看到了similar post here,但我仍在努力将这些点与相关问题联系起来并解决我的问题。提前感谢您的任何建议。

【问题讨论】:

  • 也许您应该添加标签“甘特图”或只是在某处提及该术语,这可能有助于将来的搜索。

标签: r ggplot2


【解决方案1】:

您的问题是您在初次通话中运行了coord_flip()。情节颠倒了,但ggplot仍然认为x和y是原来的x和y。

所以,要解决这个问题,只需在最后的geom_rect 中切换你的 x 和 y aes

p1 + geom_rect(data = dateRanges, aes(ymin = start , ymax = end, xmin = -Inf, xmax = Inf), inherit.aes= F, alpha = 0.4, fill = c("lightblue"))
p1

编辑:要制作后面的栏杆需要一点点摆弄。首先,我们必须调用 geom_rect 来使条形图在后面,然后我们将 xmin 和 xmax 从 aes 调用中移出,以避免弄乱轴和因子级别:

p1 <- ggplot(df, aes(x=IndID, fill = IndID))+
  geom_rect(data = dateRanges, aes(ymin = start, ymax = end), xmin = -Inf, xmax = Inf, alpha = 0.4, inherit.aes=FALSE, fill = "lightblue")+
  geom_rect(data = df, aes(x = IndID, xmin = as.numeric(IndID) - 0.45, xmax = as.numeric(IndID) + 0.45, ymin = StartDate, ymax = EndDate))+
  coord_flip()+
  xlab("IndID")+
  ylab("Time")+
  ggtitle("Individual Monitoring Periods")+
  geom_text(aes(y = StartDate + as.difftime(8, unit = "weeks"), label = paste(month(StartDate, label = T), year(StartDate), sep = "-"))) +
  geom_text(aes(y = EndDate - as.difftime(9, unit = "weeks"), label = paste(month(EndDate, label = T), year(EndDate), sep = "-")))

p1

【讨论】:

  • 完美!很有帮助。有没有办法将新的阴影也强制到绘图的“背面”,以便第一个绘图的矩形位于顶部。
  • 将新代码行添加到初始调用中(例如:p1
  • 刚刚意识到搞砸了您的订购,请参阅编辑。同时清理 cmets
猜你喜欢
  • 1970-01-01
  • 2020-05-03
  • 2022-01-13
  • 2020-07-20
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 2021-07-30
相关资源
最近更新 更多