带有子集的 ggplot 图例
Posted
技术标签:
【中文标题】带有子集的 ggplot 图例【英文标题】:Legend to ggplot w/ subsets 【发布时间】:2021-11-20 02:00:48 【问题描述】:在向此图表添加图例的位置和方式方面遇到了一些麻烦。
使用此代码,图表看起来完全符合我的要求,只是缺少图例。
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")
当我在 geom_line 中添加美学时,会出现图例,但它正在查看颜色代码,而不是我需要它说的“实际”和“预测”。
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")
添加查看表中的数据。这可能是一个简单的修复,我只是忽略了。
编辑:进行调整以获得图例。现在我需要更改图例的设置。如果没有别的,需要更改图例的标题
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()) +
theme(legend.title = element_text("Here")) +
geom_smooth(method='lm',position="identity",se=F,size=0.4,color="black")
【问题讨论】:
【参考方案1】:我不确定我没有使用您的数据,但请尝试
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") )
【讨论】:
这有帮助。我不得不对 geom_line 上的美学进行一次调整以上是关于带有子集的 ggplot 图例的主要内容,如果未能解决你的问题,请参考以下文章