Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do selective labeling with GGPLOT geom_point()

Tags:

plot

r

ggplot2

With this code:

library(ggplot2) p <- ggplot(mtcars, aes(wt, mpg)) p + geom_point() p + geom_point() + geom_text(aes(wt, mpg, label=row.names(mtcars))) 

I obtain this graph:

enter image description here

How can I modify the code above so that it only labels point where wt > 4 or mpg > 25, while the rest of the points remain unlabeled.

like image 486
neversaint Avatar asked Feb 22 '13 01:02

neversaint


People also ask

How do I add labels in Ggplot?

To add labels at specified points use annotate() with annotate(geom = "text", ...) or annotate(geom = "label", ...) . To automatically position non-overlapping text labels see the ggrepel package.

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 add labels in R?

Use the title( ) function to add labels to a plot. Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function. # labels 25% smaller than the default and green.

Which of the following arguments can be used to add labels in Ggplot?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .


2 Answers

Supply a data argument to geom_text:

library(ggplot2) mtcars$name <- row.names(mtcars) p <- ggplot(mtcars, aes(wt, mpg)) p + geom_point() p + geom_point() +    geom_text(data=subset(mtcars, wt > 4 | mpg > 25),             aes(wt,mpg,label=name)) 

Resulting plot:

plot1

PS: I'm really not a fan of the p + geom() style of constructing ggplots, I'm pretty sure hadley did it in the original ggplot2 book to demonstrate different modifications of the same plot, but people seem to have picked it up and run with it. Here's how I'd do it:

  • Just add the different components of the plot together with +, don't save each intermediate step.
  • Don't bother saving it to a variable unless you really need to, you can still save it to a file if you need to with ggsave()
  • Put all the aesthetics that are going to apply to the whole plot in the first ggplot call, only modify the other things if necessary

My version:

ggplot(mtcars, aes(wt, mpg, label=name)) +   geom_point() +   geom_text(data=subset(mtcars, wt > 4 | mpg > 25)) 
like image 159
Marius Avatar answered Sep 23 '22 01:09

Marius


You can pass a subset argument to a layer. In your case this would require having the rownames as a column, so they are evaluated properly. You will need to explicitly load plyr to get the function . which makes the syntax easy.

# shamelessly using @marius initial code library(ggplot2) library(plyr) mtcars$name <- row.names(mtcars) p <- ggplot(mtcars, aes(wt, mpg))  p + geom_point() + geom_text(aes(wt,mpg,label=name), subset = .(wt > 4 | mpg > 25)) 
like image 45
mnel Avatar answered Sep 21 '22 01:09

mnel