2

Is there a way that R will automatically find the best position for the legend? How to display a rectangle with color before the legend and adjust the border of the legend? Below is my code and the result

questions <- seq(1, 50, by=1)
probs <- dgeom(questions, prob=0.25)


plot(questions, probs, type='l', ylab='prob')
polygon(c(questions[15:50], 50, 15), 
        c(probs[15:50], 0, 0), col="red")

polygon(c(questions[8:15], 15, 8), 
        c(probs[8:15], 0, 0), col="blue")

polygon(c(questions[8:15], 15, 8), 
        c(probs[8:15], 0, 0), col="blue")

polygon(c(questions[0:8], 8, 0), 
        c(probs[0:8], 0, 0), col="green")

legend("topright", 
       col=c("green", "blue", "red"), 
       legend=c("Majority", "Minority", "Infinity"))

enter image description here

You may notice that the very left line is not straight. How can I fix this?

jay.sf
  • 60,139
  • 8
  • 53
  • 110

1 Answers1

0

Use a different device, such as png, pdf, jpg, postscript, ...

You are probably using RStudio were usually plots are shown in the Plots window. Usually the user adjusts this window until the plot has the desired format. However, legends and text among other things might be distorted then. You may just rerun your code but this is tedious and if you export the plot with specified dimensions, it's also not guaranteed that the plot will look like in this preview, especially legends and text.

This preview is the "RStudioGD" device (somehow combined with a png device). You can see it using dev.list() after you created a plot.

The solution to get a stable plot result is using a different device such as png, pdf, jpg, postscript etc., where you specify the dimensions in the call rather than by moving the mouse. Open the saved plot now to check the result. On Linux you may keep the saved plot open and it will refresh itself.

Note, that you need to start the new device, plot, then stop the device using device.off(). Otherwise the plot won't be stored and new plots will be sent to the new device forever until you close it (or quit R).

Also see this question: How to save a plot as image on the disk?

Basic setup

Here using png device.

png('file', .)  ## start device
plot(.)  ## create plot
lines(.)
...
device.off()  ## stop device

If you get a clash with your devices, just close all devices using graphics.off().

Your plot

png('myplot1234.png', 480, 480)  ## start device

plot(questions, probs, type='l', ylab='prob')
polygon(c(questions[15:50], 50, 15), 
        c(probs[15:50], 0, 0), col="red")

polygon(c(questions[8:15], 15, 8), 
        c(probs[8:15], 0, 0), col="blue")

polygon(c(questions[8:15], 15, 8), 
        c(probs[8:15], 0, 0), col="blue")

polygon(c(questions[0:8], 8, 0), 
        c(probs[0:8], 0, 0), col="green")

legend("topright", 
       col=c("green", "blue", "red"), 
       pch=15, 
       legend=c("Majority", "Minority", "Infinity"))

dev.off()  ## stop device

The plot should be saved as 'myplot1234.png' in your working directory. You may also specify a path of course 'path/myplot1234.png'. (Notice. that you need to specify some pch= in the legend, otherwise no colors are shown.)

enter image description here


Data:

questions <- seq(1, 50, by=1)
probs <- dgeom(questions, prob=0.25)
jay.sf
  • 60,139
  • 8
  • 53
  • 110