在ggplot2中添加一条水平线来绘制图例和图例 [英] Add a horizontal line to plot and legend in ggplot2

查看:3769
本文介绍了在ggplot2中添加一条水平线来绘制图例和图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码创建了一个漂亮的情节,但是我想在y = 50处添加一条水平黑线,并且图例在图例中显示了带有文本cutoff的黑线,​​但是在图例中留下了点。我可以使用geom_line添加该行,但无法获取图例中的行。

  library(ggplot2)
the.data< - read.table(header = TRUE,sep =, ,
text =来源,年份,价值
S1,1976,56.98
S1,1977,55.26
S1,1978,68.83
S1,1979,59.70
S1,1980,57.58
S1,1981,61.54
S1,1982,48.65
S1,1983,53.45
S1,1984,45.95
S1 ,1985,51.95
S1,1986,51.85
S1,1987,54.55
S1,1988,51.61
S1,1989,52.24
S1,1990,49.28
S1,1991,57.33
S1,1992,51.28
S1,1993,55.07
S1,1994,50.88
S2,1993,54.90
S2, 1994,51.20
S2,1995,52.10
S2,1996,51.40
S3,2002,57.95
S3,2003,47.95
S3,2004,48.15
S3,2005,37.80
S3,2006,56.96
S3,2007,48.91
S3,2008,44.00
S3,2009,45.35
S3,2010 ,49.40
S3,2011,51.19)
ggplot(the.data,aes(x = year,y = value))+
geom_point(aes(color = so urce))+
geom_smooth(aes(group = 1))


解决方案< (1)试试这个:

  cutoff<  -  data.frame(x = c(-Inf ,Inf),y = 50,cutoff = factor(50))
ggplot(the数据,aes(year,value))+
geom_point(aes(color = source))+
geom_smooth(aes(group = 1))+
geom_line(aes(x,y,linetype = cutoff),cutoff)



(2)关于您的评论,如果您不想将截止日期作为单独的传说列出,只需在曲线上标注截止线就会更容易:



<$ (aes(color = source))+
geom_smooth(aes(group = 1))pre> ggplot(the.data,aes(year,value))+
geom_point )+
geom_hline(yintercept = 50)+
annotate(text,min(the.data $ year),50,vjust = -1,label =Cutoff)


This code creates a nice plot but I would like to add a horizontal black line at y=50 AND have the legend show a black line with the text "cutoff" in the legend, but leave points in the legend for the sources. I can add the line with geom_line but cannot get the line in the legend.

    library(ggplot2)
    the.data <- read.table( header=TRUE, sep=",", 
    text="source,year,value
    S1,1976,56.98
    S1,1977,55.26
    S1,1978,68.83
    S1,1979,59.70
    S1,1980,57.58
    S1,1981,61.54
    S1,1982,48.65
    S1,1983,53.45
    S1,1984,45.95
    S1,1985,51.95
    S1,1986,51.85
    S1,1987,54.55
    S1,1988,51.61
    S1,1989,52.24
    S1,1990,49.28
    S1,1991,57.33
    S1,1992,51.28
    S1,1993,55.07
    S1,1994,50.88
    S2,1993,54.90
    S2,1994,51.20
    S2,1995,52.10
    S2,1996,51.40
    S3,2002,57.95
    S3,2003,47.95
    S3,2004,48.15
    S3,2005,37.80
    S3,2006,56.96
    S3,2007,48.91
    S3,2008,44.00
    S3,2009,45.35
    S3,2010,49.40
    S3,2011,51.19") 
    ggplot(the.data, aes( x = year, y = value ) ) + 
        geom_point(aes(colour = source)) + 
        geom_smooth(aes(group = 1))

解决方案

(1) Try this:

cutoff <- data.frame( x = c(-Inf, Inf), y = 50, cutoff = factor(50) )
ggplot(the.data, aes( year, value ) ) + 
        geom_point(aes( colour = source )) + 
        geom_smooth(aes( group = 1 )) + 
        geom_line(aes( x, y, linetype = cutoff ), cutoff)

(2) Regarding your comment, if you don't want the cutoff listed as a separate legend it would be easier to just label the cutoff line right on the plot:

ggplot(the.data, aes( year, value ) ) + 
    geom_point(aes( colour = source )) + 
    geom_smooth(aes( group = 1 )) + 
    geom_hline(yintercept = 50) + 
    annotate("text", min(the.data$year), 50, vjust = -1, label = "Cutoff")

这篇关于在ggplot2中添加一条水平线来绘制图例和图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆