Is there anyway to plot a bar like this?

Hi guys,

I am wondering if there is a way to plot a bar indicating the distribution and the cutoff value like below.


pic from Biden vs Trump: US presidential election 2020 results

I have got

attitude <- c('solid_blue', 'leaning_blue', 'toss_up', 'leaning_red', 'solid_red')
n_votes <- c(190, 108, 121, 39, 80)
group <- c(1,1,1,1,1)
df <- rbind(attitude, n_votes, group)
df <- as.data.frame(t(df))

ggplot(data = df) + 
  geom_bar(stat = 'identity', mapping = aes (x = group, y = n_votes, fill = attitude)) + coord_flip()

The n_votes does not seem to accumulate correctly, how can I correct this?

Cheers,

Danny

when you transpose you coerce the n_votes numbers to be characters.
add the following dplyr code to get back the n_votes as numbers.

 df <- df %>% mutate(n_votes=as.numeric(n_votes))

Thanks, I thought there must be something wrong with the dataframe so I created a tibble. It worked.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.