Re: add species labels using geom_text

412 views
Skip to first unread message

Ben Bond-Lamberty

unread,
Nov 12, 2012, 1:04:15 PM11/12/12
to Heather Wright, ggp...@googlegroups.com
Heather,

Working backwards through your questions: qplot is just a convenient
wrapper for ggplot and its related functions. I use it frequently for
quick looks at things, but to build up a complex plot, you'll probably
want to use ggplot.

In your example, you're pulling data out of the data frame to plot it.
Don't! Keeping things in a single df makes things easier.

I think you want to do something like this:

qplot(rank,logabun,data=sp52,label=species,geom=c("text"))

Here, you're telling qplot to plot rank (x) versus logabun (y), with
labels pulled from species, and use geom_text. If you wanted to have
points as well as lines, you'd change that last part to
geom=c("point","text"). Alternatively, using ggplot:

ggplot( sp52, aes( rank, logabun ) ) + geom_text( aes( label=species ) )

Hope this is useful,
Ben



On Mon, Nov 12, 2012 at 12:40 PM, Heather Wright
<heather...@maine.edu> wrote:
> Hi ggplot2 users, I want to add species labels (text) to an existing plot
> using geom_text and I don't know how to specify the layer from a colum of
> text names.
> Here's a visual example of what I'm trying to do:
> http://docs.ggplot2.org/current/geom_text.html
>
> My data matrix consists of rank abundance data with an initial column
> containing the species names. I'm only interested in plotting rank vs.
> logabun and putting the text of each corresponding species near the data
> point. My question is: do I have to add each species as a new layer when I
> define geom_text() ?
>
> I am using R Studio on windows 7 32-bit
> Rstudio: Version 0.97.168
> R version 2.15.0 (2012-03-30)
> Input data: sp52.csv
> ---------------------------------------------------------------------------
> #reproducible code using .csv data
> sp52 <- read.csv("sp52.csv",header=TRUE, sep=",")
> rank<-sp52$rank
> logabun<-sp52$logabun
> species<-sp52$species
> str(sp52)
> data.frame': 52 obs. of 9 variables:
> $ species : Factor w/ 52 levels "Asterionellopsis.glacialis",..: 16 45 29
> 15 44 2 22 38 37 49 ...
> $ rank : int 3 4 5 6 8 11 12 13 14 15 ...
> $ abundance : num 825507 759812 526840 264905 159652 ...
> $ proportion: num 8.5 7.9 5.4 2.7 1.7 1.4 1.3 1.2 1.2 1.2 ...
> $ plower : num 7 6 4.5 1.9 1 0.6 0.6 0.8 0.9 0.8 ...
> $ pupper : num 10.1 9.7 6.4 3.6 2.3 2.2 2 1.6 1.5 1.5 ...
> $ accumfreq : num 56.3 64.2 69.6 72.3 75.7 80.2 81.5 82.7 83.9 85.1 ...
> $ logabun : num 5.9 5.9 5.7 5.4 5.2 5.1 5.1 5.1 5.1 5.1 ...
> $ rankfreq : num 0.9 1.2 1.5 1.7 2.3 3.2 3.5 3.8 4.1 4.4 ..
>
> #plot
> qplot(rank,logabun)
> #result file: qplot_logabun.rank_52sp.png
> # add species label to each point
> ggplot(sp52, aes(x=rank, y=logabun, label=rownames(species)))
>
> #alternately
> ggplot(sp52, aes(x=rank,y=logabun, label=species[1:52]))
> Error: No layers in plot
>
> Obviously, I need to define my layers using geom_text().
> If this is the case, do I need to specify the x,y values from the list of
> species for each discrete species label?
> Example:
>> sp52_plot <- ggplot(aes(x=3, y=5.9, label="Chaetoceros.tenuissimus"))
> Error: ggplot2 doesn't know how to deal with data of class uneval
>
> Any insight into geom_text() for this? and should I use ggplot or qplot?
> Thanks
>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Felipe Carrillo

unread,
Nov 12, 2012, 1:44:58 PM11/12/12
to Heather Wright, ggp...@googlegroups.com
In this case you don't want rownames, replace it with species
myplot <- ggplot(sp52, aes(x=rank, y=logabun,label=species)) 
myplot  + geom_point(colour='red') + geom_text(size=3,vjust=1)
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

Heather Wright

unread,
Nov 13, 2012, 4:12:45 AM11/13/12
to ggp...@googlegroups.com, Heather Wright
Thanks very much for the replies.
I was able to use: 
ggplot( sp52, aes( rank, logabun ) ) + geom_text( aes( label=species ) ) 
for a labeled plot of all the species, now to fiddle with the angle, spacing, etc. 

For some reason qplot coding does not work
Error in sprintf(gettext(fmt, domain = domain), ...) : 
  invalid type of argument[1]: 'symbol
I'm guessing that the text needs to be defined first in the same manner using geom_text? 

Using ggplot accomplishes the same quality plot.
Regards,
Heather Anne

On Monday, November 12, 2012 7:04:21 PM UTC+1, Ben wrote: 

I think you want to do something like this:

qplot(rank,logabun,data=sp52,label=species,geom=c("text"))

Heather Wright

unread,
Nov 13, 2012, 4:31:38 AM11/13/12
to ggp...@googlegroups.com, Heather Wright, Felipe Carrillo
Thanks, I was able to reproduce this perfectly and play around with the adjustment so I can finally read the species labels! Easy now that I see how to do it. 

I ended up using:
sp52_plot + geom_point(colour="red")+geom_text(size=4,vjust=1,hjust=1,angle=90)

In RStudio this cuts off the species names, so I need to extend the axes range or adjust size of plot, padding, etc. 
Thanks again.

Felipe Carrillo

unread,
Nov 13, 2012, 11:10:46 AM11/13/12
to Heather Wright, ggp...@googlegroups.com
The labels can be better read by extending the y axis and perhaps making the point a little bigger
myplot  + geom_point(colour='red',shape=1,size=3) +
geom_text(aes(label=species),size=2.5,hjust=1.05,angle=90) + ylim(c(0,6))
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
From: Heather Wright <heather...@maine.edu>
To: ggp...@googlegroups.com
--
Reply all
Reply to author
Forward
0 new messages