I want to create a graph which has 36 ticks on the x-axis displaying text.
I have df
for this example i'll want 12 ticks displaying text.
period agg1 agg2
201601 1 2
201602 2 2
201603 3 2
. . .
. . .
. . .
201612 4 1
period
is numeric, in the form YYYYMM so I've changed it to a date
for (row in 1:nrow(df)){
df[row,'period'] <- df[row,'period'] %>%
mutate(period = as.Date(as.character(period*100+25),"%Y%m%d"))
}
melted the data
long_df <- melt(df, id = "period")
I plotted the data using:
df_plot <- ggplot(data = long_df,
aes(x = period, y = value, colour = variable)) +
geom_line()
# adding titles & labels
df_plot + labs(title = "Aggregates", colour = "Method") +
xlab("Period") +
ylab("Agg, £million") +
scale_x_discrete(breaks = c("201601", "201602", "201603", "201604", "201605", "201606", "201607", "201608", "201609", "201610", "201611", "201612"))
This creates a graph where the ticks are in the correct place, but the text doesn't show at the tick.
I tried adding the labels = c()
argument but it doesn't show anything
df_plot + labs(title = "Aggregates", colour = "Method") +
xlab("Period") +
ylab("Agg") +
scale_x_discrete(breaks = c("201601", "201602", "201603", "201604", "201605", "201606", "201607", "201608", "201609", "201610", "201611", "201612"),
labels = c("201601", "201602", "201603", "201604", "201605", "201606", "201607", "201608", "201609", "201610", "201611", "201612"))
I found this: ggplot x-axis labels with all x-axis values but according to the solution my labels
argument should work?