简体   繁体   中英

Why does my data visual in ggplot2 display 4 significant figures when I specify 3?

I'm trying to create a bar chart the displays the unemployment rate for people who majored in the social sciences for 4 different age groups. In my visual, I want to display the unemployment rate on each bar, but I'm having trouble getting the labels on my bars to display 3 significant figures.

Here is my code:

social_vs <- social_ur %>%
  ggplot(aes(
    x = age_group,
    y = unemployment_rate_avg,
    fill = age_group
  )) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = scales::percent(signif(unemployment_rate_avg, digits = 3)), vjust = 2.0))

social_vs1 <- social_vs +
  scale_y_continuous(name = "Unemployment Rate", labels = scales::percent, n.breaks = 4) +
  scale_x_discrete(name = "Age Group", labels = c(
    "25 to 34",
    "35 to 44",
    "45 to 54",
    "55 to 64")) +
  scale_fill_brewer(palette = "Set2") +
  theme_grey() +
  theme(legend.position = "none") +
  labs(title = "Social: unemployment rate by age group")

social_vs1

As you can see, I specify

signif(unemployment_rate_avg, digits = 3)

yet my visual still displays 4 significant figures.

I've included a png of the visual created by running the above code.

Social Science Unemployment Rates

Try this (not tested as no data was shared):

library(ggplot2)
#Code
social_vs <- social_ur %>%
  ggplot(aes(
    x = age_group,
    y = unemployment_rate_avg,
    fill = age_group
  )) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = scales::percent_format(accuracy = 3))), vjust = 2.0))

social_vs1 <- social_vs +
  scale_y_continuous(name = "Unemployment Rate", labels = scales::percent, n.breaks = 4) +
  scale_x_discrete(name = "Age Group", labels = c(
    "25 to 34",
    "35 to 44",
    "45 to 54",
    "55 to 64")) +
  scale_fill_brewer(palette = "Set2") +
  theme_grey() +
  theme(legend.position = "none") +
  labs(title = "Social: unemployment rate by age group")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM