I am currently trying to produce a histogram using ggplot, but need to convert the y axis from counts to percentages.
This is a reprex of my current work with ggplot:
ggplot(null_distribution,aes(x=stat))+geom_histogram(binwidth=.5)+scale_y_continuous(labels = percent_format())
#> Error in ggplot(null_distribution, aes(x = stat)): could not find function "ggplot"
I have tried using percent_format, as well as scale_y_continuous. The issue that I'm having is that I cannot remove the extra 0s from my percentages. In other words, 40% is appearing as 40000%. Any help would be appreciated!
You didn't provide the data so I simulated some to get a working example. There are two important points:
ggplot provides some special notation to access internal variables which makes plotting percentages in histograms straightforward (e.g. ..count..)
percent_format() returns a function that will take the y values and multiple them by 100 and add a percent sign. So you are taking the counts on the y-axis and they are going from 40 to 4000%. The function is working correctly, you are just asking it the wrong question.
Thank you for your help! It looks like this got me closer by dropping some of the 0s. It now appears as 300%, 200%, 100%. Any ideas on how to fix these? This is a screenshot of a portion of the data:
Screenshots are not very useful (and not a good thing to do here) if you need more specific help, please provide a proper reproducible example including sample data on a copy/paste friendly, take a look to this guide to learn how to do one.
The advice from @andresrcs is good. You will receive much better help if you can provide the minimum amount of code or data needed to be able to completely reproduce the issue you are facing. Screenshots aren't helpful because they can't easily be turned into usable code or data.
That said, I again tried to simulate data that looks like yours, and I cannot reproduce the issue of percentages >100%.
data <- tibble(replicate = seq(1:100),
stat = sample(seq(-2, 2, by = 0.5), 100, replace = T))
head(data)
# A tibble: 6 x 2
replicate stat
<int> <dbl>
1 1 0
2 2 1.5
3 3 -0.5
4 4 0.5
5 5 -1
6 6 -0.5
ggplot(data, aes(x = stat)) +
geom_histogram(aes(y = stat(density)), binwidth = 0.5) +
scale_y_continuous(labels = scales::percent_format())