Home > Blockchain >  Legend to ggplot w/ subsets
Legend to ggplot w/ subsets

Time:09-28

Having a little trouble on where and how to add a legend to this graph.

With this code, the graph looks exactly how I want it to except it's missing the legend.

library(dplyr)
library(ggplot2)
library(scales)


## QUERY FOR MOUNTAIN-200 BLACK, 38
monthly.sales<-dbGetQuery(con, "
                          select
convert(date, DATEADD(Day,-1 * DATEPART(DAY,soh.OrderDate)  1,soh.OrderDate)) as SalesMonth
,sum(sod.LineTotal) MonthlySales
,'A' as 'ActOrPred'
from Sales.SalesOrderDetail sod
Join Sales.SalesOrderHeader soh 
    on sod.SalesOrderID=soh.SalesOrderID
where sod.ProductID=782
and year(soh.OrderDate) = 2013
group by DATEADD(Day,-1 * DATEPART(DAY,soh.OrderDate)  1,soh.OrderDate)
order by SalesMonth")

monthly.sales$SalesMonth <-as.Date(monthly.sales$SalesMonth)

lm.fit<-lm(MonthlySales ~ SalesMonth,data=monthly.sales)

predicted.sales<-data.frame(SalesMonth = seq(from=as.Date('2013-12-01'),
                                             to=as.Date('2014-06-01'),by="month"),
                            MonthlySales=0,ActOrPred="P")

predicted.sales$MonthlySales<-predict(lm.fit,newdata = predicted.sales)

all.months<-rbind(monthly.sales,predicted.sales)

all.months[13,2]=all.months[12,2]

ggplot(all.months,aes(x=SalesMonth,y=MonthlySales))  
  geom_line(data = subset(all.months,ActOrPred == 'A'),color="69b3a2",size=3)  
  geom_line(data = subset(all.months,ActOrPred == 'P'),color="gray",size=3,linetype="dashed")  
  expand_limits(y=c(0,310000))  
  scale_y_continuous(labels = comma)  
  scale_x_date(date_labels="%b %y",breaks ="2 month"
               ,expand=c(.06,1)) 
  labs(title = "Forecast: Mountain-200 Black, 38"
       ,x="Sales Month"
       ,y="Revenue")  
  theme_bw()  
    theme( 
    plot.title = element_text(color = "#2CC4C4",size = 12,face = "bold",hjust = 0.5),
    axis.line = element_line(color = "gray"),
    axis.text.x = element_text(size = 7),
    axis.text.y = element_text(size = 7),
    axis.title.x = element_text(size = 8),
    axis.title.y = element_text(size = 8),
    panel.border = element_blank())  
  geom_smooth(method='lm',position="identity",se=F,size=0.4,color="black")

graph1

When I add in the aesthetic in the geom_line, the legend appears but it's looking at the color codes, not "Actual" and "Forecast" which is what I need it to say.

ggplot(all.months,aes(x=SalesMonth,y=MonthlySales))  
  geom_line(data = subset(all.months,ActOrPred == 'A'),aes(color="69b3a2"),size=3)  
  geom_line(data = subset(all.months,ActOrPred == 'P'),aes(color="gray"),size=3,linetype="dashed")  
  expand_limits(y=c(0,310000))  
  scale_y_continuous(labels = comma)  
  scale_x_date(date_labels="%b %y",breaks ="2 month"
               ,expand=c(.06,1)) 
  labs(title = "Forecast: Mountain-200 Black, 38"
       ,x="Sales Month"
       ,y="Revenue")  
  theme_bw()  
    theme( 
    plot.title = element_text(color = "#2CC4C4",size = 12,face = "bold",hjust = 0.5),
    axis.line = element_line(color = "gray"),
    axis.text.x = element_text(size = 7),
    axis.text.y = element_text(size = 7),
    axis.title.x = element_text(size = 8),
    axis.title.y = element_text(size = 8),
    panel.border = element_blank())  
  geom_smooth(method='lm',position="identity",se=F,size=0.4,color="black")

enter image description here

Adding a look at the data in a table. It's probably an easy fix that I'm just overlooking.

table

CodePudding user response:

I'm not sure that I do not use your data but try

ggplot(all.months,aes(x=SalesMonth,y=MonthlySales))  
  geom_line(data = subset(all.months,ActOrPred == 'A'),aes(color="69b3a2"),size=3)  
  geom_line(data = subset(all.months,ActOrPred == 'P'),aes(color="gray"),size=3,linetype="dashed")  
  expand_limits(y=c(0,310000))  
  scale_y_continuous(labels = comma)  
  scale_x_date(date_labels="%b %y",breaks ="2 month"
               ,expand=c(.06,1)) 
  labs(title = "Forecast: Mountain-200 Black, 38"
       ,x="Sales Month"
       ,y="Revenue")  
  theme_bw()  
  theme( 
    plot.title = element_text(color = "#2CC4C4",size = 12,face = "bold",hjust = 0.5),
    axis.line = element_line(color = "gray"),
    axis.text.x = element_text(size = 7),
    axis.text.y = element_text(size = 7),
    axis.title.x = element_text(size = 8),
    axis.title.y = element_text(size = 8),
    panel.border = element_blank())  
  geom_smooth(method='lm',position="identity",se=F,size=0.4,color="black")  
  scale_color_manual(labels = c("Actual", "Forecast"), values = c("69b3a2", "gray") )

CodePudding user response:

Here is what my current chart code looks like. I still need to work on adjusting the legend a bit, like change the header and maybe position.

ggplot(all.months,aes(x=SalesMonth,y=MonthlySales))  
  geom_line(data = subset(all.months,ActOrPred == 'A'),aes(color=ActOrPred),size=3)  
  geom_line(data = subset(all.months,ActOrPred == 'P'),aes(color=ActOrPred),size=3,linetype="dashed")  
  expand_limits(y=c(0,310000))  
  scale_y_continuous(labels = comma)  
  scale_x_date(date_labels="%b %y",breaks ="2 month"
               ,expand=c(.06,1)) 
  scale_color_manual(labels=c("Actual","Forecast"),values=c("69b3a2","gray")) 
  labs(title = "Forecast: Mountain-200 Black, 38"
       ,x="Sales Month"
       ,y="Revenue")  
  theme_bw()  
    theme( 
    plot.title = element_text(color = "#2CC4C4",size = 12,face = "bold",hjust = 0.5),
    axis.line = element_line(color = "gray"),
    axis.text.x = element_text(size = 7),
    axis.text.y = element_text(size = 7),
    axis.title.x = element_text(size = 8),
    axis.title.y = element_text(size = 8),
    panel.border = element_blank())  
  geom_smooth(method='lm',position="identity",se=F,size=0.4,color="black")

and the chart

chart

  • Related