-1

I'm trying to stack a bar plot using ggplot in R studio! Here is my data that I'd like to plug in:

Country      Con       Lab
England  14364350   11389497
NI           3895          0
Scotland   757949     717007
Wales      528839      71354

I'm a total beginner with R Studio so I'm sorry if this question is tedious! Ideally if you could help me stack it by the party that came second that would be helpful! (Lab is second apart from Wales)

something a bit like below would be fab!! I'm struggling on how to get started, but hopefully the picture shows what the stacked graph would look like (fingers crossed)

What I've Tried so Far:

library('ggplot2')
ggplot(elections, aes(x= Country, y= ?? (this is where I hit a wall)

Data

elections <- read.table(header = TRUE, stringsAsFactors = FALSE,
                        text = "Country       Con        Lab
                                England  14364350   11389497
                                NI           3895          0
                                Scotland   757949     717007
                                Wales      528839      71354")
Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • 1
    You should provide some info about what have you tried so far. Also might be useful to provide an example of the result that you need (either find something similar on the web or make the same plot in excel/python/whatever you have) – jim Oct 28 '17 at 21:28
  • Also check out questions others have asked: The answer by @roland on this question should get you a long way: https://stackoverflow.com/questions/21236229/stacked-bar-chart – Richard Oct 28 '17 at 23:31
  • Thank you guys for your feedback! Once I get my head around R I'll be able to adapt data from these other answers but just this once I wanted something personalised to me to start me off (if that makes sense). Thank you!! – Lauren Edensor Oct 28 '17 at 23:43
  • base r version `barplot(t(elections[, -1]), names.arg = elections$Country, las = 1, legend.text = TRUE, main = 'Chart title', col = c('dodgerblue2', 'darkorange'), border = NA)` – rawr Oct 29 '17 at 03:08

1 Answers1

2

Your table is in the wrong format to be processed within ggplot. In the case of your data, which is currently recorded in three columns Country, Labour, Conservative it instead needs to be in the format Country, Party, Number of Votes. To find out more about these requirements you can read up a bit about tidy data here: http://vita.had.co.nz/papers/tidy-data.html

The following code works. The function melt has been used to transform the data to the required format.

library(ggplot2)
library(reshape2)

elections <- data.frame(Country = c("England", "NI", "Scotland", "Wales"), Con = c(14364350, 3895, 757949, 528839), Lab = c(11389497, 0, 717007, 71354))

elections_long <- melt(elections, id = "Country")

ggplot(elections_long, aes(x = Country, y = value)) +
  geom_bar(stat="identity", aes(fill = variable))

The output plot can be seen here:

Hope that helps.

rawr
  • 20,481
  • 4
  • 44
  • 78
Michael Harper
  • 14,721
  • 2
  • 60
  • 84