0

I am trying to create a graph, but for some reason the data labels are the breakdown of every data point on the graph. Not sure what is going on here. here's the code I'm using. I would appreciate any help with this.

incentive_select %>%
  count(invest_reward = factor(invest_reward)) %>% 
  mutate(pct = n/sum(n)) %>%
  ggplot(aes( x = invest_reward, y= pct,
              label = scales::percent(pct)))+
  geom_col(fill = "steelblue") +
  geom_text(vjust = -1,
            size = 3) +
  scale_y_continuous(labels = scales::percent) +
  labs(
    x = "How much do you agree with the following statements about solar 
    incentives and net metering? \n Only people who are investing in rooftop 
    solar panels should be rewarded for their contribution to clean
    energy generation."
  ) +
  theme(axis.title.y=element_blank(),
        axis.text.x = element_text(vjust = 0.5, hjust=1))

incentive_select %>% select(invest_reward,
                            more_support,
                            pay_fee) %>%
  count(invest_reward = factor(invest_reward),
        more_support = factor(more_support),
        pay_fee = factor(pay_fee)) %>% 
  mutate(pct = n/sum(n)) %>%
  rename("Only people who are investing in rooftop solar panels 
should be rewarded for their contribution to clean energy generation" = "invest_reward",
         "More support should be provided to those who don’t have
access to rooftop solar" = "more_support",
         "Incentives are too high for rooftop solar panels owners.
They should pay a fee for sending power to the grid" = "pay_fee") %>% 
  pivot_longer(cols = c("Only people who are investing in rooftop solar panels \nshould be rewarded for their contribution to clean energy generation",
                        "More support should be provided to those who don’t have\naccess to rooftop solar",
                        "Incentives are too high for rooftop solar panels owners.\nThey should pay a fee for sending power to the grid")) %>%
  group_by(name) %>% 
  ggplot(aes(x = value, y= pct, fill = name,
             label = scales::percent(pct)))+
  geom_col() +
  facet_wrap(~name, ncol = 1) +
  geom_text(aes(label = pct), position = position_stack(vjust = 0.5),
            size = 3, color = "white", check_overlap = TRUE) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = label_wrap_gen(width = 25, multi_line = TRUE)) +
  labs(y = "Percentage",
       title = "How strongly do you agree with the proposed rooftop solar
    incentive reforms in your local jurisdiction?") +
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(vjust = 0.5, hjust=1),
        legend.position = "bottom")

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
H B
  • 1
  • 4
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data via `dput()`. – stefan Sep 13 '22 at 18:47
  • This said: From your code I would guess that you did the data prep steps in the wrong order, i.e. reshape your data before computing the counts and the percentages. Try `incentive_select %>% select(invest_reward, more_support, pay_fee) %>% pivot_longer(everything()) %>% group_by(name, value) %>% summarise(n = n()) %>% mutate(pct = n / sum(n))` – stefan Sep 13 '22 at 18:54
  • A bit of an aside, but to plot ratings/Likert scales, you probably want to use something like this: https://www.r-bloggers.com/2013/07/plotting-likert-scales-net-stacked-distributions-with-ggplot-rstats/ – Ottie Sep 13 '22 at 19:39
  • thank you both for your help. these resources are great. and @stefan you're right it was in issue with the order. your suggested code worked perfectly. thanks!! – H B Sep 13 '22 at 20:26

0 Answers0