0

I am creating a bar chart through ggplot to display survey responses. I want to add data labels, but when I add a data label it displays 14 digits after the decimal. I do not need any digits after the decimal. Where in my code can I have ggplot round the y values?

I have tried several other code suggestions that I have found online, but because this is a calculated field through the below code, the percentages are not displayed in my imported data, the imported data only has the survey responses and I am dropping them into the displayed categories.

ggplot(activeanglers, aes(factor(YearsFishing))) + 
  geom_bar(aes(fill = factor(..x..)), stat="count") + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  ylab("relative frequencies") + xlab("How many years have you fished?") + ylab("Percent of Respondents") + scale_x_discrete(breaks=c("1","2","3", "4", "5", "NA"),labels=c("First Year", "2-5", "6-9", "10+", "Missing", "NA")) + theme(legend.position = "none") + geom_text(aes(label=stat(prop)*100, group=1),stat='count',nudge_y=0.125)
Betsey
  • 1
  • Without having your data, we can't run your code, and without seeing any output, we can only guess as to what you have and what you're trying to change. A general guess is that you can / often should calculate summary stats beforehand so you can control things like rounding and formatting. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making a question that's easy to answer – camille Aug 22 '19 at 18:41
  • The labels are defined at the end (`geom_text`). Can you do something like this: `geom_text(aes(label=round(stat(prop)*100), group=1), ...` – David Klotz Aug 22 '19 at 18:42
  • @DavidKlotz that did it! Thank you so much! :) – Betsey Aug 22 '19 at 18:46
  • @DavidKlotz I am having the same issue with the y-axis tick marks. Where would I put the round command for that? I tried in a few places and could not get it to work. – Betsey Aug 22 '19 at 21:24

0 Answers0