Reputation: 63984
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:
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.
Upvotes: 41
Views: 93418
Reputation: 67
library(ggrepel)
mtcars$name <- row.names(mtcars)
ggplot(mtcars, aes(wt, mpg, label=name)) +
geom_point() +
geom_text_repel(data=subset(mtcars, wt > 4 | mpg > 25), aes(label=name))
Upvotes: 2
Reputation: 121568
a ggplot2-like lattice solution :-)
library(latticeExtra)
xyplot(mpg~wt, data=mtcars,pch=19,
panel =function(x,y,...){
# panel.xyplot(x,y,...)
data=subset(mtcars, wt > 4 | mpg > 25)
panel.text(data$wt,data$mpg,label=row.names(data),
col='red',cex=2)
},par.settings = ggplot2like(), axis = axis.grid)
Upvotes: 3
Reputation: 60060
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:
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:
+
, don't save each intermediate step.ggsave()
ggplot
call, only modify the other things if necessaryMy version:
ggplot(mtcars, aes(wt, mpg, label=name)) +
geom_point() +
geom_text(data=subset(mtcars, wt > 4 | mpg > 25))
Upvotes: 90
Reputation: 15395
You could just get an extra variable:
carnames <- row.names(mtcars)
carnames[with(mtcars, !(wt > 4 | mpg > 25))] <- ""
p + geom_point() + geom_text(aes(wt,mpg,label=carnames))
Upvotes: 3
Reputation: 115382
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))
Upvotes: 6