简体   繁体   English

在 R 图的图例中叠加两个圆圈

[英]Overlay two circles in a legend of an R plot

In R, can I compose legend items as in...在 R 中,我可以像...一样编写图例项吗?

在此处输入图片说明

I have data plotted as red dots, overlain by red or orange outer circles, where the size of these outer circles represent an attribute and the color of the outer circle represents an attribute.我将数据绘制为红点,由红色或橙色外圈覆盖,其中这些外圈的大小代表一个属性,外圈的颜色代表一个属性。 How can I repeat that in the legend?我怎么能在传说中重复呢?

在此处输入图片说明

So far I'm only having the legend...到目前为止,我只有传说......

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)
)

I tried to give the third element a vector eg我试图给第三个元素一个向量,例如

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

without luck.没有运气。

If you are trying to create the "Item A" and "Item B" symbols, you should use variations of pt.bg and pt.lwd in addition to the other legend arguments you are currently using (eg lty , pch , col , and pt.cex ).如果你正在尝试创建“项目A”和“B项”的符号,你应该使用的变化pt.bgpt.lwd除了其他legend你目前正在使用的参数(如ltypchcol ,和pt.cex )。 Use the following example below and adjust the code to see how these parameters interact with one another:使用下面的示例并调整代码以查看这些参数如何相互交互:

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)

在此处输入图片说明

I know this question is old, but for reference I think it's easier to call the legend a second time to overwrite the red dots in.我知道这个问题很旧,但作为参考,我认为再次调用图例来覆盖红点会更容易。

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)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM