R 添加具有自己比例和图例的几何图形

R 添加具有自己比例和图例的几何图形,r,ggplot2,legend,R,Ggplot2,Legend,这是对这些问题的后续行动: 我希望在绘图中添加一个几何图形,它将有自己独立的数据、比例和图例元素 我意识到这可能违反了一些图形原则的语法,但我经常发现自己想要图例中出现的垂直或水平线、单点、箭头等元素 qplot(data=iris, x=Petal.Width, y=Sepal.Width, colour=Species, shape=as.factor(round(Petal.Length)), linetype=as.factor(rou

这是对这些问题的后续行动:

我希望在绘图中添加一个几何图形,它将有自己独立的数据、比例和图例元素

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

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. 
当比例已经用于其他几何映射时,似乎增加了难度

下面是一个使用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) 

p更改
geom\u vline
调用,在
aes
调用中同时包含
xintercept
linetype
。现在,
线型
没有任何效果(不仅仅是没有显示在图例中)


更改
geom\u vline
调用,以在
aes
调用中同时包含
xintercept
linetype
。现在,
线型
没有任何效果(不仅仅是没有显示在图例中)