在ggplot2中打印矩形-输入无效:time_trans仅适用于类POSIXct的对象

在ggplot2中打印矩形-输入无效:time_trans仅适用于类POSIXct的对象,r,ggplot2,R,Ggplot2,我有这些数据 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"), StartD

我有这些数据

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
当我试图用下面的代码将新矩形添加到绘图中时,我得到了文章标题中指示的错误

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类


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

您的问题是您在第一次通话中运行了
coord\u flip()
。绘图被翻转,但ggplot仍然认为x和y是原始的x和y

因此,要修复此问题,只需在最终的
geom\u 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完美!非常有用。是否有一种方法也可以强制在绘图的“背面”添加新的着色,以便第一个绘图的矩形位于顶部。将新代码行添加到初始调用(例如:p1刚意识到打乱了您的订单,请参见编辑。同时清理评论可能您应该添加标记“甘特图”或只是在某处提及术语,这可能有助于将来的搜索。
p1 + geom_rect(data = dateRanges, aes(ymin = start , ymax = end, xmin = -Inf, xmax = Inf), inherit.aes= F, alpha = 0.4, fill = c("lightblue"))
p1
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