Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot legend issue w/ geom_point and geom_text

Tags:

r

legend

ggplot2

I am trying to use geom_point to illustrate the count of my data. I would also like to annotate a few of the points in my graph with geom_text. When I add the call to geom_text, it appears that it is plotting something underneath the points in the legend. I've tried reversing the order of the layers to no avail. I can't wrap my head around why it is doing this. Has anyone seen this before?

set.seed(42)
df <- data.frame(x = 1:10
    , y = 1:10
    , label = sample(LETTERS,10, replace = TRUE)
    , count = sample(1:300, 10, replace = FALSE)
)

p <- ggplot(data = df, aes(x = x, y = y, size = count)) + geom_point()
p + geom_text(aes(label = label, size = 150, vjust = 2))

alt text

like image 541
Chase Avatar asked Nov 19 '10 00:11

Chase


People also ask

What is Geom_point in Ggplot?

The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.

What does Geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.

How do I change the legend value in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do I add a legend in ggplot2?

You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.


1 Answers

This happened to me all the time. The trick is knowing that aes() maps data to aesthetics. If there's no data to map (e.g., if you have a single value that you determine), there's no reason to use aes(). I believe that only things inside of an aes() will show up in your legend.

Furthermore, when you specify mappings inside of ggplot(aes()), those mappings apply to every subsequent layer. That's good for your x and y, since both geom_point and geom_text use them. That's bad for size = count, as that only applies to the points.

So these are my two rules to prevent this kind of thing:

  1. Only put data-based mappings inside of aes(). If the argument is taking a single pre-determined value, pass it to the layer outside of aes().
  2. Map data only for those layers that will use it. Corollary: only map data inside of ggplot(aes()) if you're confident that every subsequent layer will use it. Otherwise, map it at the layer level.

So I would plot this thusly:

p <- ggplot(data = df, aes(x = x, y = y)) + geom_point(aes(size = count)) 
p + geom_text(aes(label = label), size = 4, vjust = 2) 
like image 127
Matt Parker Avatar answered Oct 11 '22 10:10

Matt Parker