0

I have the dataframe below and I want to display the values in the x-axis as actual dates and not as months.

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)
firmo23
  • 7,490
  • 2
  • 38
  • 114

1 Answers1

1

The problem seems to be in ggplot and will be passed on to plotly.
You can change the x-axis format with scale_x_date. (The labels have been rotated for better readability)

Code

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)

Output enter image description here

tamtam
  • 3,541
  • 1
  • 7
  • 21