Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2使用2条线和正确的图例绘制单个数据_R_Plot_Ggplot2_Legend - Fatal编程技术网

R ggplot2使用2条线和正确的图例绘制单个数据

R ggplot2使用2条线和正确的图例绘制单个数据,r,plot,ggplot2,legend,R,Plot,Ggplot2,Legend,在Axeman的帮助下,我已经能够用平均值/中值线绘制一个基准。为了完成注释,我想将平均值/中值放入图例标签中。我的代码: require(data.table) require(ggplot2) 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

在Axeman的帮助下,我已经能够用平均值/中值线绘制一个基准。为了完成注释,我想将平均值/中值放入图例标签中。我的代码:

require(data.table)
require(ggplot2)
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')
nmu <- mean(wf$Gals)
textmu <- paste("Mean:", round(nmu, 2))
nmed <- median(wf$Gals)
textmed <- paste("Median:", round(nmed, 2))

p <- ggplot(wf, aes(Date, Gals)) +  
     geom_line(aes(lty = "Gals", color = "Gals", group = 1)) +
     geom_hline(aes(yintercept = nmu,  linetype = textmu, color = textmu), 
                color='red',size=1)+
     geom_hline(aes(yintercept = nmed, linetype = textmed, color = textmed), 
                color='orange',size=1)+
     #scale_linetype('Water Usage') +
     scale_color_manual('Water Usage', 
                        values=c('Gals'='black',textmu='red', textmed='orange')) +
     xlab("") + 
     ylab("Gallons per Day")

print(p)
require(data.table)
需要(ggplot2)

wf通过熔化
数据。表格
您可以:

require(data.table)
require(ggplot2)
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')

wf[,`:=`(Mean=round(mean(Gals),2),Median=round(median(Gals),2))]

p <- ggplot(melt(wf,id.vars = c(1,3))
[variable%in%c("Gals","Mean","Median")], aes(Date, 
value,color=variable,group=variable)) +  
geom_line() +
scale_color_manual('Water Usage',values=c('Gals'='black',Mean='red', 
Median='orange')) +
xlab("") + 
ylab("Gallons per Day")

print(p)
require(data.table)
需要(ggplot2)

wf准备一个标签向量,然后使用我在我的:


标签根据问题,这不会将平均值和中值放在图例标签中。结果不错。这正是我想要做的,保持data.frame尽可能简单,并学习更多ggplot2知识。感谢Axeman和社区!
labels <- c(Gals = 'Gals',
            Mean = paste("Mean:", round(mean(wf$Gals), 2)),
            Median = paste("Median:", round(median(wf$Gals), 2)))

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', labels = labels) +
  scale_color_manual(
    'Water Usage', 
    values = c('Gals' = 'black', 'Mean' = 'red', 'Median' = 'orange'),
    labels = labels
  ) +
  xlab("") + ylab("Gallons per Day")