【问题标题】:Add geom with its own scales and legend添加带有自己的比例和图例的 geom
【发布时间】:2012-10-19 18:11:06
【问题描述】:

这可以说是对这些问题的跟进:

Add legend to ggplot2 line plot

Add vline to existing plot and have it appear in ggplot2 legend?

我希望将几何图形添加到具有自己独立数据、比例和图例元素的绘图中。

我意识到这可能违反了图形原则的某些语法,但我经常发现自己想要图例中出现的垂直或水平线、单点、箭头等元素。

当其他几何映射中已经使用比例尺时,似乎会增加难度。

这里不是使用 iris 的漂亮示例:

p<-qplot(data=iris, x=Petal.Width,
         y=Sepal.Width,
         colour=Species,
         shape=as.factor(round(Petal.Length)),
         linetype=as.factor(round(Sepal.Length)),
         geom=c("line", "point"))

p<-p+geom_vline(xintercept=0.75, aes(linetype="Setosa vs Versicolor"))
p<-p+geom_vline(xintercept=1.75, aes(linetype="Versicolor vs Virginica"))

print(p) 

目前产生的,缺少 vline 的线型和关联图例。

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    更改您的geom_vline 调用,以在aes 调用中同时包含xinterceptlinetype。现在,linetype 没有任何效果(不仅仅是没有出现在图例中)。

    qplot(data=iris, x=Petal.Width,
          y=Sepal.Width,
          colour=Species,
          shape=as.factor(round(Petal.Length)),
          linetype=as.factor(round(Sepal.Length)),
          geom=c("line", "point")) +
    geom_vline(aes(xintercept=0.75, linetype="Setosa vs Versicolor")) +
    geom_vline(aes(xintercept=1.75, linetype="Versicolor vs Virginica"))
    
    ## Warning messages:
    ## 1: The shape palette can deal with a maximum of 6 discrete values because
    ## more than 6 becomes difficult to discriminate; you have 7. Consider
    ## specifying shapes manually. if you must have them. 
    ## 2: The shape palette can deal with a maximum of 6 discrete values because
    ## more than 6 becomes difficult to discriminate; you have 7. Consider
    ## specifying shapes manually. if you must have them. 
    ## 3: Removed 4 rows containing missing values (geom_point). 
    ## 4: The shape palette can deal with a maximum of 6 discrete values because
    ## more than 6 becomes difficult to discriminate; you have 7. Consider
    ## specifying shapes manually. if you must have them. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2019-03-29
      相关资源
      最近更新 更多