I want to create a line chart with multiple lines per acquiYear
, where the x-axis should represent the month
and the y-axis the nrOrders
.
My data table looks like this:
structure(list(delivYear = c("2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018"
), acquiYear = c("2014", "2014", "2014", "2014", "2014", "2014",
"2014", "2014", "2014", "2014", "2014", "2014", "2015", "2015",
"2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015",
"2015", "2015", "2016", "2016", "2016", "2016", "2016", "2016",
"2016", "2016", "2016", "2016", "2016", "2016", "2017", "2017",
"2017", "2017", "2017", "2017", "2017", "2017", "2017", "2017",
"2017", "2017", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018"), quater = c("Q1",
"Q1", "Q1", "Q2", "Q2", "Q2", "Q3", "Q3", "Q3", "Q4", "Q4", "Q4",
"Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q3", "Q3", "Q3", "Q4", "Q4",
"Q4", "Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q3", "Q3", "Q3", "Q4",
"Q4", "Q4", "Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q3", "Q3", "Q3",
"Q4", "Q4", "Q4", "Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q3", "Q3",
"Q3", "Q4", "Q4", "Q4"), month = c("Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan",
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan",
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec"), nrOrders = c("0", "0", "0", "0", "0", "0", "0",
"0", "1", "1", "2", "0", "2", "4", "5", "3", "7", "3", "5", "4",
"3", "7", "8", "7", "2", "24", "16", "33", "9", "27", "16", "10",
"27", "9", "31", "35", "11", "11", "25", "15", "18", "19", "19",
"8", "27", "34", "43", "51", "0", "11", "2", "0", "0", "0", "0",
"0", "4", "5", "1", "0"), eur = c("0", "0", "0", "0", "0", "0",
"0", "0", "180", "8760", "17760", "0", "6730", "5354", "2840",
"22190", "29210", "8725", "7226", "12500", "2090", "13780", "21434",
"15719", "29000", "112366", "38148", "125900", "10285", "54281",
"23777", "111512", "76907", "18306", "124393", "103220", "8890",
"52082", "119095", "28482", "62841", "54356", "54119", "42999",
"112459", "169798", "123808", "504226", "0", "55336", "3017",
"0", "0", "0", "0", "0", "15880", "1844", "252", "0")), row.names = c(NA,
-60L), class = c("data.table", "data.frame"))
How can I create a chart with multiple lines per year?
Here is one possible easy way using ggplot2
library("ggplot2")
df$nrOrders <- as.numeric(df$nrOrders)
df$month = factor(df$month, levels = month.abb) ## Order the months
ggplot(data=df, aes(x=month, y=nrOrders, group= acquiYear)) +
geom_line(aes(color = acquiYear)) +
geom_point()
Could also be coded directly as follow :
library(ggplot2)
ggplot(data=df, aes(x=factor(month, levels = month.abb), y=as.numeric(nrOrders), group= acquiYear)) +
geom_line(aes(color = acquiYear)) +
geom_point() +
xlab("month")
I you want an histogram, you can do it as follow:
ggplot(data=df, aes(x=factor(month, levels = month.abb), y=as.numeric(nrOrders), fill= acquiYear)) +
geom_bar(stat='identity', position='dodge', linewidth = 1) +
geom_point() +
xlab("month") +
ylab("nrOrders")+
scale_fill_manual(values = c("grey10", "grey20", "grey30", "grey40", "grey50"))
I however, do not think that an histogram is appropriate for such data.