简体   繁体   中英

ggplot2 - removing size legend in R

I want to remove the size 20 legend on the right. Below is my code:

qplot(lingLocation[ind,ind_water_fountain]/lingLocation[ind,1], 
     lingLocation[ind,ind_soda]/lingLocation[ind,1],
     color = lingLocation[ind,ind_frosting]/lingLocation[ind,1], size = 20) +
     # opts(legend.position = "none") +
     scale_colour_gradient("Frosting %", low = "blue", high = "red") +
     ylab("Soda %") +
     xlab("Water Fountain %")

Thanks!

我的情节

Try removing the size=20 argument from the qplot call, and stick it in a geom_point call.

For example, change your code to this:

qplot(lingLocation[ind,ind_water_fountain]/lingLocation[ind,1], 
     lingLocation[ind,ind_soda]/lingLocation[ind,1],
     color = lingLocation[ind,ind_frosting]/lingLocation[ind,1]) +
     # opts(legend.position = "none") +
     scale_colour_gradient("Frosting %", low = "blue", high = "red") +
     ylab("Soda %") +
     xlab("Water Fountain %") +
     geom_point(size=20)

I don't use the qplot function, but I think that should work. I believe ggplot2 adds a scale for every aesthetic call in each geom, which is what qplot is probably doing. But since you are applying the size to all points, having the size parameter outside of the aes should achieve the same effect, and the size=20 will not generate a legend for it (I think). And geom_point should inherit the aesthetics from qplot (I hope).

Let me know if this works!

EDIT:

I just tested it. Placing size inside geom_point should work. However, the size settings from inside qplot seem to differ from the size settings when called from geom_point , so maybe setting a smaller size inside geom_point should do the trick (say, geom_point(size=5) )

qplot assumes that the size specification is a mapping and so creates a scale. Here, you just want to set it to 20, so replace size = 20 with size = I(20) . That sets the size and does not create a scale (and probably makes the points bigger than you anticipated).

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.

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