Want Geom bars side by side

42 views Asked by At

I am working on a graph whose x axis is state name and y axis is vote share, but on the x-axis I want both general election and state election data side by side per each state. However, in my output I can only see one bar and the other one is mixed inside it.

ggplot(BJP.voteshare.2019) +
  geom_bar(aes(x = State_Name, y = voteshare.bjp.x), fill = "blue", stat = "identity", position = "dodge") +
  geom_bar(aes(x = State_Name, y = voteshare.bjp.y), fill = "red", stat = "identity", position = "dodge") +
  geom_text(aes(x = State_Name, y = voteshare.bjp.x, label = round(voteshare.bjp.x, 2)), vjust = -0.5, size = 3, color = "black", position = position_dodge(width = 0.8)) +
  geom_text(aes(x = State_Name, y = voteshare.bjp.y, label = round(voteshare.bjp.y, 2)), vjust = -0.5, size = 3, color = "black", position = position_dodge(width = 0.8)) +
  labs(title = "BJP Vote Share Comparison: State vs General Elections",
       x = "State", y = "Vote Share (%)") +
  scale_fill_manual(values = c("blue", "red"), labels = c("State Elections", "General Elections")) +
  theme_minimal()

Bar chart showing vote share by state with only a single bar per state

1

There are 1 answers

0
Nir Graham On

I'll invent some simple example data to simulate what you probably had (conclusion I dont see how it would all have been red ... ).

solution wise, I would say that ggplot2 is part of tidyverse, and so is opinionated on how the data should be arranged; it should be tidy. Therefore I tidy it, and show the more streamlined code, that also results in likely the desired chart. Hope it helps you .

1)

(BJP.voteshare.2019 <- data.frame(State_Name=LETTERS[1:3],
                                  voteshare.bjp.x=4:6,
                                  voteshare.bjp.y=1:3
                                  ))
library(tidyverse)

ggplot(BJP.voteshare.2019) +
  geom_bar(aes(x = State_Name, y = voteshare.bjp.x), fill = "blue", stat = "identity", position = "dodge") +
  geom_bar(aes(x = State_Name, y = voteshare.bjp.y), fill = "red", stat = "identity", position = "dodge") +
  geom_text(aes(x = State_Name, y = voteshare.bjp.x, label = round(voteshare.bjp.x, 2)), vjust = -0.5, size = 3, color = "black", position = position_dodge(width = 0.8)) +
  geom_text(aes(x = State_Name, y = voteshare.bjp.y, label = round(voteshare.bjp.y, 2)), vjust = -0.5, size = 3, color = "black", position = position_dodge(width = 0.8)) +
  labs(title = "BJP Vote Share Comparison: State vs General Elections",
       x = "State", y = "Vote Share (%)") +
  scale_fill_manual(values = c("blue", "red"), labels = c("State Elections", "General Elections")) +
  theme_minimal()

enter image description here 2)

# make tidy data

(BJP.voteshare.2019_tidy <- pivot_longer(BJP.voteshare.2019,
                                        cols = starts_with("voteshare.bjp")))

ggplot(BJP.voteshare.2019_tidy) +
  aes(x = State_Name, y = value,group=name)+
  geom_col(aes(fill=name),   position = "dodge") +
  geom_text(aes(label = round(value, 2)), vjust = -0.5, size = 3, color = "black", position = position_dodge(width=.8)) +
   labs(title = "BJP Vote Share Comparison: State vs General Elections",
       x = "State", y = "Vote Share (%)") +
  scale_fill_manual(values = c("voteshare.bjp.x"="blue", "voteshare.bjp.y" = "red"), labels = c("State Elections", "General Elections")) +
  theme_minimal()

enter image description here