0

Trying to facet out a barplot of survey responses. Each facet should have the percent answers to the question while annotating each facet with the number of respondants. Everything seems to work fine until add the annotation with geom_text which messes up the scale. Cant figure out why.

This seems to work fine

ggplot(dat2, aes(Q1a)) +geom_bar(aes(y = ..prop.., group = D3b) ) + 
coord_flip() + facet_wrap(~ D3b) + labs(x="",y="") + 
scale_y_continuous(labels = percent)

and gets me this, which has the barplots scaled the way I need them enter image description here

But when I add geom_text the scale gets all messed up

ggplot(dat2, aes(Q1a)) +geom_bar(aes(y = ..prop.., group = D3b) )+ 
coord_flip() + facet_wrap(~ D3b)  + labs(x="",y="") + 
scale_y_continuous(labels = percent)+ geom_text(data=dat2.cor, 
aes(x=0,y=0,label=n), nudge_x = 10, nudge_y=20, 
colour="black",inherit.aes=FALSE)

Like so:enter image description here

dat2.cor is a separate dataset I created with data annotate the facets which looks like this:

enter image description here

Anybody have any idea whats going on here?

989
  • 12,579
  • 5
  • 31
  • 53
DSaeger
  • 51
  • 2
  • 2
    What are you looking to do? `nudge_y=20` adds 20, or 2000%, which is why your scale is messed up. – Peyton Jun 23 '16 at 20:22
  • I'm just trying to add annotation to the facets without changing the scale. – DSaeger Jun 24 '16 at 16:18
  • I thought nudge_ x and nudge_y moved the geom text around the facet, is there a way to do this without effecting the scale. When I remove nudge_x and nudge_y it still scales each facet to 100%. I think it would be more readable if they were scaled to 40% as in the first example – DSaeger Jun 24 '16 at 17:08
  • You ought to be able to just set `x=` and `y=` for `geom_text`. As for the scale, if you're having issues, you can do something like `scale_y_continuous(limit=c(0, 0.4))`. Does that get you closer to what you want? You might want to consider adding some example data: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Peyton Jun 24 '16 at 19:07
  • That did it! Wish I could give you credit @Peyton, maybe I can? – DSaeger Jun 27 '16 at 20:07

0 Answers0