1

I read this post about ggplot showing % instead of counts, but I cannot get it to work when I have a melted dataframe consisting of several factor variables.

This gives me a plot where the bars are still showing counts and not % as I were going for.

 # Data
  head(sabat)
     V1 V2 V3 V4 V5 V6 V7 V8
   1  2  2  2  2  2  2  1  2
   2  2  1  2  1  2  1  1  2
   3  2  2  2  2  2  2  2  1
   4  1  2  2  2  2  2  2  2
   5  2  1  2  2  2  2  1  2
   6  2  1  2  1  2  1  1  2

  # melt data.frame
    sabat.m        <- melt(sabat)

  # set 2 to NA because as only want to plot the yes answers (1=yes, 2=no)
  sabat.m$value[sabat.m$value==2] <- NA
  sabat.m <- na.omit(sabat.m)

  # Factor it
  sabat.m$value  <- factor(sabat.m$value)

  #structure of the melted data.frame
  str(sabat.m)

   data.frame': 1020 obs. of  2 variables:
   $ variable: Factor w/ 8 levels "V1","V2","V3",..: 1 1 1 1 1 1 1 1 1 1 ...
   $ value   : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
   - attr(*, "na.action")=Class 'omit'  Named int [1:2412] 1 2 3 5 6 7 8 10 11 12 ...
   .. ..- attr(*, "names")= chr [1:2412] "1" "2" "3" "5" ...

  # Plot it
  p <- ggplot(sabat.m, aes(x = variable)) +  
  geom_bar(aes(y = (..count..)/sum(..count..)), binwidth = 25) + 
  scale_y_continuous(labels = percent_format()) +
  coord_flip()

  p

Thanks in advance

Community
  • 1
  • 1
Einnor
  • 239
  • 1
  • 3
  • 12
  • Yes but as I understand it. Formatter only works in older versions of R – Einnor May 04 '13 at 21:09
  • My session info are the same: R version 2.15.3, scales_0.2.3, ggplot2_0.9.3.1. it plots its allright, but it's only the axis that has changed to %. the bars are stil representing counts – Einnor May 05 '13 at 07:51
  • 1
    With your sample data (6 lines of data) and code plot shows percents - for V1 it is aprox. 8%, for V2 23% and so on, add total is 100% as it should be. – Didzis Elferts May 05 '13 at 07:58
  • You're right. I see that now. But what I want to is to take percent of my original dataset's total length (being 529). Is that possible? so if for instances V1 have 115 1's in it, I want the bar for v1 to represent 115/529*100 – Einnor May 05 '13 at 08:27
  • 1
    Just replace sum(..count..) in geom_bar() with 529 – Didzis Elferts May 05 '13 at 08:31
  • 1
    If you only want the 1's in `sabat.m`, use `sabat.m <- subset(sabat.m, value == 1L, select = "variable")`. Next, there is no point in using a `binwidth` argument in `geom_bar()` since `variable` is a factor - that's probably why you're getting counts instead of proportions. In the `scale_y` line, you could also say `labels = percent` (assuming the `scales` package is loaded). Trying this on a simple example worked for me. – Dennis May 05 '13 at 11:37

0 Answers0