1

I've tried multiple solutions to this from previous versions of ggplot answers, however, I have yet to find a way to change the scale from count to percent.

The structure of the data includes: deid = an id variable; variable a one-level factor; and value = the participants response.

 $ deid     : int  123 155 332 224 654 325 865 654 555 434 ...
 $ variable : Factor w/ 1 level "tx.rating": 1 1 1 1 1 1 1 1 1 1 ...
 $ value    : int  9 8 9 9 5 7 7 7 7 10 ...

Here is my code:

ggplot(tx.plot, aes(value, fill = variable)) + 
  geom_histogram(alpha = .6, binwidth = .5)

That creates:

enter image description here

I am hoping to get the percent responses from 0 - 10, not the count. I have tried to add scale_y_continuous and scale_y_discrete with the labels = percent command, however it has not worked and I receive error messages every time.

b222
  • 966
  • 1
  • 9
  • 19
  • 1
    What if you calculate those percentages for each 0 to 10 value and save them as column `Prc` and then plot `value` and `Prc` in x an y axes? Some answers here might help you : http://stackoverflow.com/questions/29869862/ggplot2-how-to-add-percentage-or-count-labels-above-percentage-bar-plot – AntoniosK Oct 03 '15 at 17:46
  • Thanks, @AntoniosK, that is helpful – b222 Oct 03 '15 at 20:03

2 Answers2

2

Something like:

set.seed(1492)
tx.plot  <- data.frame(deid=sample(100:900, 50),
                       variable=factor(1),
                       value=sample(10, 50, replace=TRUE))
library(scales)
ggplot(tx.plot, aes(value, fill = variable)) + 
  geom_histogram(aes(y=..count../sum(..count..)), 
                 alpha = .6, binwidth = .5) +
  scale_x_discrete(limits=c(0,10)) +
  scale_y_continuous(label=percent) +
  labs(y="percent")

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
0
ggplot(tx.plot, aes(x = value)) + geom_density(aes(group=variable))
Navin Manaswi
  • 964
  • 7
  • 19