I have data in R that is in the form of : week -year (e.g. 21-03, the 21st week of 2003). In my real data, these dates appear in character format.
I am trying to plot this data in R, but the date formatting is not working properly. I tried to recreate the data as close as I can, and then run the plots:
library(dplyr)
library(ggplot2)
library(reshape2)
library(scales)
library(lubridate)
Table_1 <- data.frame( "Col_A" = c("21-03", "21-03", "21-03", "21-04", "21-04", "21-04", "21-05", "21-05", "21-05"),
"Col_B" = c("AAA", "AAB", "AAC", "AAA", "ABB", "AAC", "AAA", "AAB", "AAC"),
"Col_C" = c(111, 122.5, 9, 727, 66.4, 3, 992, 88, 12) )
Table_1$col_A_Date <- as.Date(Table_1$col_A, format = "%W-%y")
Table_1$Col_B = as.factor(Table_1$Col_B)
Table_1$Col_C = as.numeric(Table_1$Col_C)
melt = melt(Table_1, id = c("Col_A"))
p = ggplot(melt, aes(x = Col_A, y=value, group = 1)) + geom_line(aes(color=variable)) +
facet_grid(variable ~., scales = "free_y") + scale_x_date(date_labels = "W%-%y", date_breaks = '1 month') + theme(axis.text, x = element_text(angle = 45))
final = p + scale_y_continuous(labels = comma)
Can someone please tell me what I am doing wrong?
Thanks