Home > database >  Adjust geom_bar plot with large label count
Adjust geom_bar plot with large label count

Time:11-17

I want to display

Improvments I'm looking forward to:

Q1: How to change the color of geom_text()to e.g. black, while scale_colour_manual() is already in use?

Q2: How can one only display those values of the column variables in the bars that share a non-zero value in column value?

Q3: How can one get some kind of "magnifier" effect above thoses bars with a low amount of counts but a large amount of labels?

CodePudding user response:

From the description of the data set, a more aesthetic and legible plot could be something like this:

fill_names %>%
  filter(value != 0) %>%
  mutate(variable = substr(variable, 5, 50),
         value = ifelse(pol_dir == "Left", -value, value)) %>%
  mutate(variable = case_when(
    variable == "abort" ~ "Abortion",
    variable == "anti_racism" ~ "Anti-Racism",
    variable == "climate_change" ~ "Climate Change",
    variable == "feminism" ~ "Feminism",
    variable == "Gender" ~ "Gender Rights",
    variable == "gun" ~ "Gun Rights",
    variable == "left_wing" ~ "Left-wing rights",
    variable == "LGBTQ_Gender" ~ "LGBTQ Gender rights",
    variable == "right_wing" ~ "Right-wing rights")) %>%
  select(-pol_dir) %>%
  ggplot(aes(y = row_mean, x = value, fill = value > 0, alpha = count))  
  geom_point(alpha = 0)  
  geom_hline(yintercept = 0, alpha = 0.1)  
  geom_vline(xintercept = 0, alpha = 0.1)  
  geom_col(data = . %>% filter(value < 0), orientation = "y")  
  geom_col(data = . %>% filter(value >= 0), orientation = "y")  
  guides(fill = guide_none(), alpha = guide_none())  
  facet_wrap(.~variable)  
  scale_fill_manual(values = c("#0015BC", "#E9141D"))  
  scale_y_continuous(limits = c(-0.35, 0.35), name = "Mean score")  
  labs(title = "Importance of issues according to mean score on all questions",
       subtitle = paste("Bar size shows mean score, density indicates",
                        "number of respondents at each mean score"))  
  theme_minimal(base_size = 16)  
  theme(panel.grid = element_blank(), panel.spacing.y = unit(8, "mm"),
        strip.text = element_text(face = 2))  
  scale_x_continuous(limits = c(-0.6, 0.6), name = "Views on issue",
                     labels = c("", "Left wing", "", "Right wing", ""))

enter image description here

  • Related