ggplot2: Making changes to symbols in the legend

余生长醉 提交于 2019-11-30 18:27:50

问题


I'm having a problem making the symbols in the legend of my plot match those in the plot itself.

Suppose the data has four columns like this

data = data.frame(x = sample(1:10, 10, replace=TRUE), y = sample(1:10, 10, replace=TRUE), 
           Rank = sample(1:10, 10, replace = TRUE), Quantified = factor(sample(1:2, 10, replace = TRUE))
)

I would like points to be different sizes (distinguished by 'Rank') and represented by different symbols (crosses and open circles, distinguished by 'Quantified').

My code is

ggplot(data, aes(x = x, y = y)) +
          geom_point(aes(size = Rank, shape = Quantified)) +
          scale_shape_manual("Quantified", labels = c("Yes", "No"), values = c(1, 4)
 )

The symbols in the plot are as I want them.

My problem is that I would like the circles in the top legend to be unfilled as they are in the plot.

I've tried a variety of commands in different parts of the code (e.g., fill = "white") but nothing seems to work quite right.

Any suggestions?


回答1:


Now that I'm sure it's what you want:

library(scales)
ggplot(data, aes(x = x, y = y)) +
          geom_point(aes(size = Rank, shape = Quantified)) +
          scale_shape_manual("Quantified", labels = c("Yes", "No"), values = c(1, 4)) + 
          guides(size = guide_legend(override.aes = list(shape = 1)))



来源:https://stackoverflow.com/questions/12751159/ggplot2-making-changes-to-symbols-in-the-legend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!