..n.. and ..count.. in geom_text

286 views
Skip to first unread message

G Maubach

unread,
Apr 19, 2017, 3:51:42 AM4/19/17
to ggp...@googlegroups.com

Hi All, 

I have the following code: 

-- cut  

(g03_02_p02 <- ggplot(data = d_kzb_input) + 

geom_bar( 

mapping = aes(x = v03_02_r01, y = round(..prop.. * 100, 0)), 

fill = c_ww_palette["blue"]) + 

scale_y_continuous(limits = c(0, c_y_limit)) + 

theme_classic() + 

ggtitle(paste0("Question 3", 

"(n = ", <<cases>>, ")")) + # How can I refer to the number of cases for this plot? Is there something like "..n.."? 

xlab("Orders") + 

ylab("Percent") + 

geom_text( 

aes(label = ..count..), # How can I refer to the counts for the labels of the columns? 

color = "white", 

position = position_stack(vjust = 0.5))) 

-- cut -- 

I would like to refer to the internal statistics of the geom_bar(): 

How can I refer to the number of cases for this plot? Is there something like "..n.."? 

How can I refer to the counts for the labels of the columns? 

Kind regards 

Georg


Crump, Ron

unread,
Apr 19, 2017, 8:58:53 AM4/19/17
to G Maubach, ggp...@googlegroups.com
Dear Georg,

>I have the following code: 
>-- cut  
>(g03_02_p02 <- ggplot(data = d_kzb_input) + 
>geom_bar( 
>mapping = aes(x = v03_02_r01, y = round(..prop.. * 100, 0)), 
>fill = c_ww_palette["blue"]) + 
>scale_y_continuous(limits = c(0, c_y_limit)) + 
>theme_classic() + 
>ggtitle(paste0("Question 3", 
>"(n = ", <<cases>>, ")")) + # How can I refer to the number of cases for
>this plot? Is there something like "..n.."? 
>xlab("Orders") + 
>ylab("Percent") + 
>geom_text( 
>aes(label = ..count..), # How can I refer to the counts for the labels of
>the columns? 
>color = "white", 
>position = position_stack(vjust = 0.5))) 
>-- cut --

You could try to help us to help you by providing a minimal reproducible
example including some data and some code (without all the extraneous
theme calls etc) that works up to a point (e.g. before the label is added,
or adding a different label to that which you want).

Anyway, maybe somebody else can say how you might access the values you
are indicating (I don't think you can, but I could be mistaken), otherwise
this might help get you started:

# some example data, the rounding is essentially binning.
# From your code I guessed your x variable is a factor or
# something without many different values?

DF<-data.frame(v03=round(rnorm(1000),1))

DF2 <- as.data.frame(table(DF$v03)) # counts
# Make Var1 numeric to space my Normally distributed example
# data appropriately in the tails
DF2$x <- as.numeric(as.character(DF2$Var1))

# Plot
ggplot(DF2)+geom_bar(aes(x=Var1,y=round(Freq*100/sum(Freq),0)),stat='identi
ty')+geom_text(aes(x=Var1,y=round(Freq*100/sum(Freq),0),label=Freq))
# NB the round(Freq*100/sum(Freq),0) is to mimic your rounding of ..prop..

#
# or, since I'm not sure why you'd want to chuck away
# info by rounding your y-variable:
ggplot(DF2)+geom_bar(aes(x=x,y=Freq),stat='identity')+geom_text(aes(x=x,y=F
req,label=Freq),nudge_y=0.5)
# or, if you prefer them as percentages:
ggplot(DF2)+geom_bar(aes(x=x,y=Freq*100/sum(Freq)),stat='identity')+geom_te
xt(aes(x=x,y=Freq*100/sum(Freq),label=Freq),nudge_y=0.05)


Ron.

PS I was under the impression that it was bad practice to post the same
question to multiple lists (e.g. R-help and here in this instance). Is
this generally accepted now? There are quite often queries across some
combination of SO, R-help and here.

Wouter van der Bijl

unread,
Apr 20, 2017, 2:06:06 PM4/20/17
to ggplot2, georg....@gmail.com, R.E....@warwick.ac.uk
I suppose one could use stat_count(aes(label = ..count..), geom = "text") ?

W.

Crump, Ron

unread,
Apr 21, 2017, 7:38:37 AM4/21/17
to Wouter van der Bijl, ggplot2, georg....@gmail.com, Crump, Ron
Hi Wouter,

>I suppose one could use stat_count(aes(label = ..count..), geom = "text")
>?

That's neat. I have a bit of a blind spot with the stat_* functions. I'll
have to try harder to remember them. That would certainly satisfy Georg's
requirements.

Ron.

Reply all
Reply to author
Forward
0 new messages