Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting legend outside plot in R

Tags:

plot

r

legend

I am trying to put the legend outside the plot using xpd=TRUE but the legend keeps plotting within the plot. How can I fix this?

x = c(0,0,1,1,1)
y = c(0.4991,1.1423,1.2258,1.158,0.5148)
dat<-cbind(x,y)
point_shape = c(10,15,10,15,1)
dat<-data.frame(x,y,point_shape)

myTicks<-c(0,1)
plot(dat[,1],dat[,2], yaxt="n", xaxt="n", xlab="", ylab="",pch = dat$point_shape)
abline(0.4991,0.7267)
abline(1.1423,0.0157)
abline(0.4991,0.0157,lty=2)
axis(side = 1, at = myTicks)
axis(side = 2, at = myTicks)


legend("bottomleft", legend = c("apple", "orange", "tree"),
       bty = "n", xpd=FALSE, mar(c(7,7,7,7)), cex = 1, pch = c(10, 15, 1))
like image 875
song0089 Avatar asked Nov 14 '25 23:11

song0089


1 Answers

Use inset and make xpd to true

legend("bottomleft", legend = c("apple", "orange", "tree"), inset=c(-0.15,0),
   bty = "n", xpd=TRUE, mar(c(7,7,7,7)), cex = 1, pch = c(10, 15, 1))
like image 73
Prany Avatar answered Nov 17 '25 11:11

Prany



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!