R ggplot2 single line plot with 2 hlines legends

扶醉桌前 提交于 2019-12-13 03:47:54

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/48032917/r-ggplot2-single-line-plot-with-2-hlines-legends

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!