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.