R ggplot2单线图与2个hlines传说 [英] R ggplot2 single line plot with 2 hlines legends
问题描述
head(wf)
Date Gals天GpD GpM
2016-10-21 6.0 1 6.0 186.0
2016-10-22 6.0 1 6.0 186.0
2016-10-23 12.4 1 12.4 384.4
2016-10- 24 26.8 1 26.8 830.8
2016-10-25 33.3 1 33.3 1032.3
2016-10-26 28.3 1 28.3 877.3
我想要做的是时间序列图日期与加尔斯,并为平均值和中位数水平线。我正面临的小故障正确地把一个传说放在情节上。我的代码到目前为止:
require(ggplot2)
p1 <-ggplot(data = wf,aes(Date, Gals,lty ='Gals'),color =black)+
geom_line(color =black)+
scale_x_date(date_labels =%b-%Y,date_breaks =2个月)+
xlab()+ ylab(加仑每日)+ scale_linetype('水用量')+
geom_hline(aes(yintercept = as.numeric(mean(wf $ Gals) ),linetype =Mean),
color =red,size = 1)+
geom_hline(aes(yintercept = as.numeric(median(wf $ Gals)),linetype =Median ),
color =orange,size = 1)
print(p1)
产生:
传说中的线条颜色是错误的。什么是正确的方法来做到这一点?
V ------------------------ V编辑原始问题以添加...
我希望这能起作用,我会使用Axeman的解决方案来获得这个:
<$ c (中位数(wf $加尔斯),2) - 中位数(中位数:,圆(中位数(wf $加尔斯),2) )
ggplot(wf,aes(Date,Gals))+
scale_x_date(date_labels =%b-%Y,date_breaks =4 months)+
geom_line(aes(lty =Gals,color =Gals,group = 1))+
geom_hline(aes(yintercept = mean(wf $ Gals),linetype =Mean,color =
Mean) ,size = 1)+
geom_hline(aes(yintercept = median(wf $ Gals),linetype =Median,color =
Median),size = 1)+
scale_linetype ('水用量')+
scale_color_manual('Water Usage-New',labels = c(Gals,l1,l2),
values = c('Gals'='black',' (')+ ylab(每加仑)
生成以下内容:
Water Usage-New是我需要的,但是我如何摆脱第一个图例或修改它而不添加新的图例 scale_color_manual(...)
?
设置美学,就像使用 color =red
,它不会显示在图例中。只有当你在 aes
中映射它们时才包含它们。您可以使用与线型相同的颜色来映射颜色,然后正确配置比例尺:
ggplot(wf,aes(Date, Gals))+
geom_line(aes(yty =Gals,color =Gals,group = 1))+
geom_hline(aes(yintercept = mean(wf $ Gals),linetype =平均值,颜色=平均),大小= 1)+
geom_hline(aes(yintercept = median(wf $ Gals),linetype =Median,color =Median),size = 1)+
scale_linetype('Water Usage')+
scale_color_manual(
'Water Usage',
values = c('Gals'='black','Mean'='red' ,'Median'='orange')
)+
xlab()+ ylab(加仑每日)
数据
wf < - data.table :: fread('Date Gals Days GpD GpM
201 6-10-21 6.0 1 6.0 186.0
2016-10-22 6.0 1 6.0 186.0
2016-10-23 12.4 1 12.4 384.4
2016-10-24 26.8 1 26.8 830.8
2016-10-25 33.3 1 33.3 1032.3
2016-10-26 28.3 1 28.3 877.3')
I have a simple dataframe (WF) that looks like this:
head(wf)
Date Gals Days GpD GpM
2016-10-21 6.0 1 6.0 186.0
2016-10-22 6.0 1 6.0 186.0
2016-10-23 12.4 1 12.4 384.4
2016-10-24 26.8 1 26.8 830.8
2016-10-25 33.3 1 33.3 1032.3
2016-10-26 28.3 1 28.3 877.3
What I'm trying to do is time series plot Date versus Gals and put a horizontal line for the mean and median. The glitch I'm facing is correctly putting a legend on the plot. My code so far:
require(ggplot2)
p1<-ggplot(data=wf, aes(Date, Gals, lty = 'Gals'), colour="black") +
geom_line(colour="black") +
scale_x_date(date_labels="%b-%Y", date_breaks="2 month") +
xlab("") + ylab("Gallons per Day") + scale_linetype('Water Usage') +
geom_hline(aes(yintercept=as.numeric(mean(wf$Gals)), linetype = "Mean"),
color = "red", size=1) +
geom_hline(aes(yintercept=as.numeric(median(wf$Gals)),linetype="Median"),
color = "orange", size=1)
print(p1)
yields:
The legend line color is wrong. What is the correct way to do this?
V------------------------V Edit original question to add...
I hope this works, I'll use Axeman's solution to obtain this:
l1<-paste("Mean:", round(mean(wf$Gals), 2))
l2<-paste("Median:", round(median(wf$Gals), 2))
ggplot(wf, aes(Date, Gals)) +
scale_x_date(date_labels="%b-%Y", date_breaks="4 month") +
geom_line(aes(lty = "Gals", color = "Gals", group=1)) +
geom_hline(aes(yintercept = mean(wf$Gals), linetype = "Mean", color =
"Mean"), size=1) +
geom_hline(aes(yintercept = median(wf$Gals), linetype = "Median", color =
"Median"), size = 1) +
scale_linetype('Water Usage') +
scale_color_manual('Water Usage-New', labels = c("Gals", l1, l2),
values=c('Gals' = 'black', 'Mean' = 'red', 'Median' =
'orange')) +
xlab("") + ylab("Gallons per Day")
Produces the following:
"Water Usage-New" is what I need, but how do I get rid of the first-legend or modify it without adding a new legend with scale_color_manual(...)
?
If you set an aesthetic, like using color = "red"
it won't show in the legend. Only if you map them in aes
with they included. You can map color the same you do with linetype, and then configure the scales correctly:
ggplot(wf, aes(Date, Gals)) +
geom_line(aes(lty = "Gals", color = "Gals", group = 1)) +
geom_hline(aes(yintercept = mean(wf$Gals), linetype = "Mean", color = "Mean"), size=1) +
geom_hline(aes(yintercept = median(wf$Gals), linetype = "Median", color = "Median"), size = 1) +
scale_linetype('Water Usage') +
scale_color_manual(
'Water Usage',
values = c('Gals' = 'black', 'Mean' = 'red', 'Median' = 'orange')
) +
xlab("") + ylab("Gallons per Day")
Data
wf <- data.table::fread('Date Gals Days GpD GpM
2016-10-21 6.0 1 6.0 186.0
2016-10-22 6.0 1 6.0 186.0
2016-10-23 12.4 1 12.4 384.4
2016-10-24 26.8 1 26.8 830.8
2016-10-25 33.3 1 33.3 1032.3
2016-10-26 28.3 1 28.3 877.3')
这篇关于R ggplot2单线图与2个hlines传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!