在 ggplotly plot 的 x 轴中将日期显示为实际日期,而不是仅显示为月份

Display dates as actual dates in x-axis of ggplotly plot and not as months only

我有下面的数据框,我想将 x 轴中的值显示为实际日期而不是月份。

Targets2<-structure(list(Week = structure(c(17903, 17903, 17910, 17910, 
17917, 17917, 17924, 17924, 17931, 17931, 17938, 17938, 17945, 
17945, 17952, 17952, 17959, 17959, 17966, 17966, 17973, 17973, 
17980, 17980, 17987, 17987, 17994, 17994, 18001, 18001, 18008, 
18008, 18015, 18015, 18022, 18022, 18029, 18029, 18036, 18036
), class = "Date"), Type = c("Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target", "Target", "Cumilative target", "Target", 
"Cumilative target"), Count = c(5, 5, 8, 13, 15, 28, 20, 48, 
25, 73, 25, 98, 25, 123, 25, 148, 25, 173, 25, 198, 25, 223, 
25, 248, 25, 273, 25, 298, 25, 323, 25, 348, 25, 373, 25, 398, 
25, 423, 25, 448)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-40L))

p <-
    ggplot(Targets2, aes(x = Week, y = Count, fill = Type))+
    geom_area(alpha = 0.6 , size = 0.5, colour = "white", stat = "identity", orientation = "x") +
    scale_fill_viridis(discrete = TRUE) +
    labs(fill = NULL)+
    theme_ipsum() +
    ggtitle("Figure 1: Weekly Cumulative Projected Enrollment vs Weekly Cumulative Actual Enrollment")+
    theme(legend.position = "bottom")
p

# not printed
ggplotly(p)

问题似乎在 ggplot 中,将传递给 plotly
您可以使用 scale_x_date 更改 x 轴格式。 (标签已旋转以提高可读性)

代码

p <-
  ggplot(Targets2, aes(x = Week, y = Count, fill = Type))+
  geom_area(alpha = 0.6 , size = 0.5, colour = "white", stat = "identity", orientation = "x") +
  scale_fill_viridis(discrete = TRUE) +
  scale_x_date(date_breaks = "1 week") +
  labs(fill = NULL)+
  ggtitle("Figure 1: Weekly Cumulative Projected Enrollment vs Weekly Cumulative Actual Enrollment")+
  theme(legend.position = "bottom",,
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

p

ggplotly(p)

输出