在R图的图例中覆盖两个圆

在R图的图例中覆盖两个圆,r,plot,legend,R,Plot,Legend,在R中,我可以像在 我将数据绘制为红点,由红色或橙色的外圆覆盖,其中这些外圆的大小表示属性,外圆的颜色表示属性。我怎样才能在传说中重复这一点 到目前为止,我只有一个传说 legend("topleft", legend = c("elevation of centerline", "gradient", "boulders", "boulders in steps", "boulders not in steps"), lty=c(1,1,0,0,0), pch=c(NA,

在R中,我可以像在

我将数据绘制为红点,由红色或橙色的外圆覆盖,其中这些外圆的大小表示属性,外圆的颜色表示属性。我怎样才能在传说中重复这一点

到目前为止,我只有一个传说

legend("topleft",
  legend = c("elevation of centerline", "gradient", "boulders", "boulders in steps", "boulders not in steps"),    
  lty=c(1,1,0,0,0), pch=c(NA, NA, 19, 19,19), col=c("black", "gray", "red", "orange", "green"),
  pt.cex=c(0.8, 0.8, 0.8, 2, 2)
)
我试着给第三个元素一个向量

 pch=c(NA, NA, c(19, 19), c(19,19))

没有运气。

如果您试图创建“项目A”和“项目B”符号,除了当前使用的其他
图例
参数(例如
lty
pch
col
,和
pt.cex
)之外,还应该使用
pt.bg
pt.lwd
的变体。使用以下示例并调整代码以查看这些参数如何相互作用:

x=seq(1,10,1)
y<-seq(5,50,5)
z<-rep(c(1:2),5)
df<-data.frame(x,y,z)
df$z<-factor(df$z)

plot(y~x,data=df,type="l",lty=1,lwd=1,col="grey60")
points(y~x,data=df[df$z==1,],pch=16,col="orange",cex=df$x[df$z==1])
points(y~x,data=df[df$z==1,],pch=16,col="red",cex=1)
points(y~x,data=df[df$z==2,],pch=16,col="green",cex=df$x[df$z==2])
points(y~x,data=df[df$z==2,],pch=16,col="red",cex=1)

legend("topleft",legend=c("Item A","Item B"),pch=c(21,21),col=c("green","orange"),
       pt.bg=c("red","red"),pt.lwd=c(6,6),lty=c(0,0),pt.cex=c(2,2),cex=1)
x=seq(1,10,1)

y如果您试图创建“项目A”和“项目B”符号,除了当前使用的其他
图例
参数(例如
lty
pch
col
pt.cex
)之外,还应使用
pt.bg
pt.lwd
的变体。使用以下示例并调整代码以查看这些参数如何相互作用:

x=seq(1,10,1)
y<-seq(5,50,5)
z<-rep(c(1:2),5)
df<-data.frame(x,y,z)
df$z<-factor(df$z)

plot(y~x,data=df,type="l",lty=1,lwd=1,col="grey60")
points(y~x,data=df[df$z==1,],pch=16,col="orange",cex=df$x[df$z==1])
points(y~x,data=df[df$z==1,],pch=16,col="red",cex=1)
points(y~x,data=df[df$z==2,],pch=16,col="green",cex=df$x[df$z==2])
points(y~x,data=df[df$z==2,],pch=16,col="red",cex=1)

legend("topleft",legend=c("Item A","Item B"),pch=c(21,21),col=c("green","orange"),
       pt.bg=c("red","red"),pt.lwd=c(6,6),lty=c(0,0),pt.cex=c(2,2),cex=1)
x=seq(1,10,1)

y我知道这个问题很老了,但作为参考,我认为第二次调用图例来覆盖图中的红点更容易

legend("topleft",
  legend = c("elevation of centerline", "gradient", "boulders in steps", "boulders not in steps"),    
  lty=c(1,1,0,0), pch=c(NA, NA, 19,19), col=c("black", "gray", "orange", "green"),
  pt.cex=c(0.8, 0.8, 2, 2)
)


legend("topleft",
  legend = c("elevation of centerline", "gradient", "boulders in steps", "boulders not in steps"),   
  lty=c(1,1,0,0), pch=c(NA, NA, 19,19), col=c(NA, NA, "red", "red" ),
  pt.cex=c(0.8,0.8,0.8,0.8)
)

我知道这个问题由来已久,但作为参考,我认为第二次调用图例以覆盖图中的红点更容易

legend("topleft",
  legend = c("elevation of centerline", "gradient", "boulders in steps", "boulders not in steps"),    
  lty=c(1,1,0,0), pch=c(NA, NA, 19,19), col=c("black", "gray", "orange", "green"),
  pt.cex=c(0.8, 0.8, 2, 2)
)


legend("topleft",
  legend = c("elevation of centerline", "gradient", "boulders in steps", "boulders not in steps"),   
  lty=c(1,1,0,0), pch=c(NA, NA, 19,19), col=c(NA, NA, "red", "red" ),
  pt.cex=c(0.8,0.8,0.8,0.8)
)

你已经试过了吗?Ggplot可以做到。你已经试过了吗?Ggplot可以做到这一点。好的,这是一个解决方案。有点老套,但适用于这种特殊情况!谢谢。好的,这是一个解决办法。有点老套,但适用于这种特殊情况!谢谢