简体   繁体   中英

Adjust geom_bar plot with large label count

I want to display this data in a geom_bar() plot.

This is what I came up with so far:

ggplot(fill_names, aes( x = row_mean, y = count/unique(variable) %>% length, fill = variable   ))+
  geom_bar(position = position_stack(),aes(colour=pol_dir),stat = 'identity' )+
  scale_colour_manual(breaks = c('Right','Left'),values = c('Red','Blue'))+
  geom_text(aes(label = ifelse(value>0,str_c(round(value*100,2),'%'),'')),
            position = position_stack(vjust = 0.5), size = 2,col="firebrick", show.legend = T,colour= 'black')+  
    scale_x_continuous(breaks = fill_names$row_mean)+
  scale_fill_brewer(palette="Paired")+
  coord_flip()+
  ylab('Count of users with the same row mean')+
  xlab('Row mean of users')

which results in this:

Improvments I'm looking forward to:

Q1 : How to change the color of geom_text() to eg 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?

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", ""))

在此处输入图像描述

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