Re: Problems combining geom_errorbar & geom_text layers

663 views
Skip to first unread message

adam.l...@pnc.com

unread,
Apr 19, 2013, 4:22:59 PM4/19/13
to Annie K., ggp...@googlegroups.com
Hi Annie,

Geom_text works for me if I assign NULL to the previously assigned fill aesthetic in the geom_text call:

errorz <- ggplot( df, aes(fill = factor( fac2 ), x = factor( fac1 ), y = dv ) ) +
   geom_bar( position = "dodge", stat = "identity", aes(fill=factor(fac2)) ) +
   geom_errorbar( limits, stat = "identity", position = dodge, width = .25 ) +
   geom_text( data = labels, aes(fill = NULL, x=x,y=y,label=lab), size = 3.4 ) +
   theme_bw()
errorz

I noticed in your "whut" attempt, you define "fill" in the geom_bar call rather than the ggplot call. I would've thought this too would've solved the problem but as you show, it does not. Maybe an expert here can explain why the above approach works but your "whut" example does not.

Another option is to use annotate:

errorz <- ggplot( df, aes(group = factor( fac2 ), x = factor( fac1 ), y = dv ) ) +
   geom_bar( position = "dodge", stat = "identity", aes(fill=factor(fac2)) ) +
   geom_errorbar( limits, stat = "identity", position = dodge, width = .25 ) +
   annotate( geom="text",x = 1, y = 2, label = "hello", size = 3.4 ) +
   annotate( geom="text",x = 2.1, y = 2.1, label = "hi", size = 3.4) +        
   theme_bw()
errorz

Hope this helps!




Adam Loveland




From: "Annie K." <annie....@gmail.com>
To: ggp...@googlegroups.com
Date: 04/19/2013 07:34 AM
Subject: Problems combining geom_errorbar & geom_text layers
Sent by: ggp...@googlegroups.com





I can produce a bar plot with error bars in the appropriate places, and I can produce a bar plot with geom_text notation in the desired places, but when I try to combine the two layers I either get an Error or my error bars line up along the x-axis ticks instead of being distributed at the center-top point of each bar. The code below should reproduce the problem. I'm using ggplot 0.9.3.

library( ggplot2 )

# simplified dataset
df <- data.frame(
  fac1 = c( 0, 1, 0, 1 ),
  fac2 = c( 0, 0, 1, 1 ),
  dv = c( 0.5, -1, 2, -2 ),
  se = c( 0.2, 0.4, 0.2, 0.2 )
  )

# labels for geom_text layer
labels <- data.frame(
  x = c( 1, 2 ),
  y = c( 2.1, 2.1 ),
  label = c( "hello", "hi" )
  )

# limits and dodge info for geom_errorbar layer
limits <- aes( ymax = dv + se, ymin = dv - se )
dodge <- position_dodge( width = 0.9 )

# this produces a nice looking bar plot with some sweet error bars. excellent.
soClose <- ggplot( df, aes( fill = factor( fac2 ), x = factor( fac1 ), y = dv ) ) +
  geom_bar( position = "dodge", stat = "identity" ) +
  geom_errorbar( limits, stat = "identity", position = dodge, width = .25 ) +
  theme_bw()
soClose

# this throws me an error: "Error in factor(fac2) : object 'fac2' not found"
errorz <- ggplot( df, aes( fill = factor( fac2 ), x = factor( fac1 ), y = dv ) ) +
  geom_bar( position = "dodge", stat = "identity" ) +
  geom_errorbar( limits, stat = "identity", position = dodge, width = .25 ) +
  geom_text( data = labels, aes( x = x, y = y, label = label ), size = 3.4 ) +
  myX +
  myY +
  myLegend +
  theme_bw()
errorz


# what? this is all messed up. there are still four error bars, but now they've lined up on the x-axis tick levels
wut <- ggplot( df, aes( x = factor( fac1 ), y = dv ) ) +
  geom_bar( aes( fill = factor( fac2 ) ), position = "dodge", stat = "identity" ) +
  geom_errorbar( limits, stat = "identity", position = dodge, width = .25 ) +
  geom_text( data = labels, aes( x = x, y = y, label = label ), size = 3.4 ) +
  myX +
  myY +
  myLegend +
  theme_bw()  
wut


Does anyone know how I can get my geom_text to work with my soClose plot?

Thanks so much in advance!
Annie

--
--
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

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit
https://groups.google.com/groups/opt_out.


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.


Annie K.

unread,
Apr 20, 2013, 1:39:49 PM4/20/13
to ggp...@googlegroups.com
Adam's solution works! Thanks so much.

Would you (or someone else) mind providing a brief explanation about the relationship of the fill calls to the problem? I.e., why does ggplot require that fill = NULL in the geom_text, and why *should* including fill in geom_bar solve the problem?

Thanks again,
A

Tech Arec

unread,
Apr 20, 2013, 11:02:59 PM4/20/13
to Annie K., ggp...@googlegroups.com
Hi All 
Please check with your dataset some of N/A
BR
Tamilselvan


Sent from Samsung Mobile
--

Brian Diggs

unread,
Apr 22, 2013, 12:11:48 PM4/22/13
to Annie K., ggplot2
On 4/20/2013 10:39 AM, Annie K. wrote:
> Adam's solution works! Thanks so much.
>
> Would you (or someone else) mind providing a brief explanation about the
> relationship of the fill calls to the problem? I.e., why does ggplot
> require that fill = NULL in the geom_text, and why *should* including fill
> in geom_bar solve the problem?

When an aesthetic is defined in the initial ggplot call, it applies to
all of the layers unless specifically overwritten in a layer's aes call.
So specifying fill=factor(fac2) in the original ggplot call means that
that aesthetic applies to the geom_bar, geom_errorbar, and geom_text
layers. geom_bar and geom_errorbar work as expected (geom_bar uses it;
geom_errorbar ignores it). When you get to geom_text, that aesthetic is
still there. However, the data has changed so when the layer looks for
fac2 to map to fill, it can not find that in labels. Putting fill=NULL
in the aesthetic for geom_text overrides the mapping so that this is
does not occur.

The alternate approach is to not specify a fill in the original ggplot
call. Then there is nothing to inherit in all the layers. So the mapping
must be done in the geom_bar call instead. Were geom_errorbar to use a
fill aesthetic, it would also have to be mapped in the aesthetics for
that layer.

So basically the two approaches are: set globally, override with NULL in
layers that don't have it versus set only in the layer that needs it.
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
Reply all
Reply to author
Forward
0 new messages