1
votes

R newbie here. I'm trying to create multiple plots like in the attached screenshot:

enter image description here

(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)

1
If you could provide the data (a sample would be just fine) I trust the answers will be rapid and satisfactory.missuse
This seems to indicate that dept is not a variable in deptProfit.Axeman
data$month is missing in your example data. As Axeman already said, dept has to be changed to Dept?Roman
My bad! Included the month data in the sample above and silly me, did not realise the variables were case sensitive. Complete newbie here.SarahLee

1 Answers

0
votes

You were almost there. If I'm understanding correctly, I don't think you need stat_summary.

There's a lot of missing data here, but hopefully this is a start:

First create the data frame deptProfit:

deptProfit <- data.frame(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))

Now we can plot:

library(ggplot2)    
ggplot(deptProfit,aes(x=Month, y=Profit, color = Dept))+
  geom_line(linetype = 2) +
  geom_point(pch = 1, size = 3) +
  scale_x_continuous(breaks=1:12)+
  facet_grid(Year ~ Region)+
  theme_bw()

In the above, we map Dept to the color aesthetic, so they will be plotted separately. Now that the aesthetic is properly mapped, we can just call on geom_line and geom_point. Finally, with facet_grid, we can set the horizontal (Year) and vertical (Dept) categories.

This should give you the following:

ggplot2