I have a csv data file I got from strava. The date format is in "Apr 21, 2020, 7:43:57 PM"
. I want to create a an ordered graph where each bar is a month.
But either the bars get smaller when using scale_x_discrete(limits=df_cleaned$Date)
or R reorders everything when I'm not using scale_x_discrete. The expected output is exactly that but without R reordering by alphabetical order or changing bar sizes.
I succeeded in created a date column i the format "Apr 2020" using
df_cleaned <- df %>%
separate(Activity.Date, into = c("DayMonth", "Year", "Hour"), sep=",") %>%
mutate(Year = substr(Year, 2, 5),
Month = substr(DayMonth, 1, 3),
Date = paste(Month, Year),...)
When I'm looking at df_cleaned$Date, the data is exactly in the order that I want. A solution proposed online is :
df_cleaned$Date <- factor(df_cleaned$Date, levels = df_cleaned$Date)
but it outputs
Error in 'levels<-'('*tmp*', value = as.character(levels)) : factor level [2] is duplicated
Another solution offered is using as.Date(df$Activity.Date, "%b %d, %Y")
but all it does is transform everything into a bunch of NA.