In R, can I compose legend items as in...
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
). 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)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.