R newbie here. I'm trying to create multiple plots like in the attached screenshot:
(the graphs are just mockups) but am running into difficulty using ggplot / xyplot.
My goal is to create multiple graphs that will allow us to compare trends across months/years and separate lines on the graph for each dept. My data set currently contains the columns: profit, department, year, month. There are multiple rows containing profit values for each dept/year/month which I'll need to group and average.
The closest I've found to doing this is using a ggplot below, but my current code doesn't allow me to get the graphs with multiple lines for the various dept and multiple plots for the various years. It should be able to do it but Googling has led me in circles as to whether ggplot is the right option.
e.g.
ggplot(deptProfit,aes(x=Month,y=Profit))+
stat_summary(fun.y=mean,geom="line",color="blue",linetype=2)+
stat_summary(fun.y=mean,geom="point", pch=1,size=3)+
scale_x_continuous(breaks=deptProfit$Month)+
facet_grid(~Region)+
theme_bw()
Open to any suggestions on how to create the graphs in the screenshot. Thank you!
Sample Data:
Region=c(1,1,1,1,2,2,2,2,0,0,0,0,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,1,1,2,2)
Year= c(2013,2013,2017,2017,2013,2013,2017,2017,2013,2013,2017,2017,2013,2013,2017,2017,2013,2013,2014,2014,2015,2015,2016,2016,2014,2014,2015,2015,2016,2016,2017,2017)
Dept= c(sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing,sales,marketing)
Profit =c(16,24,29,24,41,51,71,66,78,73,88,117,23,23,25,28,65,51,54,44,49,66,76,94,20,18,25,25,24,17,69,56)
Month = c(9,12,9,7,4,1,5,5,9,6,3,3,7,10,7,9,7,7,7,12,4,1,8,6,5,7,5,3,9,6,10,8)
dept
is not a variable indeptProfit
. – Axemandata$month
is missing in your example data. As Axeman already said,dept
has to be changed toDept
? – Roman