1

I have a dataset with two variables: 1) Country; 2) Coalition government or not over time (binary).

I want to use ggplot to plot a bar plot in which I have country on the X-axis and the percentage of years in which the country had a coalition government (Y=1). Each of the countries should sum to 100 pct. leading to them having the same size.

It is basically the same plot which is asked for in this question (Create stacked barplot where each stack is scaled to sum to 100%), except that I have only two outcomes (coalition government or not) and not five.

Although I follow the instructions in the answer to the question, I get the attached unsuccesful plot, using this code:

ggplot(data,aes(x = countryname, y = alliance,fill = alliance)) + 
  geom_bar(position = "fill",stat = "identity") + 
  scale_y_discrete(labels = percent_format())

I don't know what I am doing wrong, and I have tried so many different things now. Can anyone help me?

enter image description here

Community
  • 1
  • 1
champlos
  • 51
  • 1
  • 4
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Apr 23 '16 at 22:03
  • The problem is in your data frame. Re-read the original answer and look more carefully at the solution. Note it used "y=value" (the value column created by the melt), not "y=ind". – patrickmdnet Apr 23 '16 at 22:09
  • Thank you. I changed a few things according to the link. But now I get the following error: "Error: Aesthetics must be either length 1 or the same as the data (29160): x, y, fill" The code I used is: dat.m <- melt(data,id.vars = "countryname") ggplot(dat.m, aes(x = countryname, y = value,fill=alliance)) + geom_bar(stat='identity') "Alliance" is a binary variable (0 or 1 depending on whether the country in the given year formed a coalition government or not. – champlos Apr 24 '16 at 19:45

1 Answers1

2

I'll try and answer by generating some random data to match your description. Hopefully you can re-purpose this example for your needs.

# Sample data set
year <- 1990:2016

n <- length(year)
country <- rep(c("US", "Canada", "England", "France", "Germany"), each = n)
govt <- sample(c(1, 0), size = length(country), replace = T)

df <- data.frame(country, year = rep(year, times = 5), govt)
head(df)

# Create summary
library(ggplot2)
library(dplyr)
library(reshape2)

df.plot <- df %>% 
  group_by(country) %>% 
  summarize(coalition = sum(govt)/n(),
            non.coalition = 1-coalition)

# Check
rowSums(df.plot[,-1])

# Now plot
df.plot %>% 
  melt() %>% 
  ggplot(aes(x = country, y = value, fill = variable)) + geom_bar(stat = "identity", position = "stack") + 
  xlab("Country") + 
  ylab("Percent Coalition / Non Coalition") +
  scale_fill_discrete(guide = guide_legend(title = "Type of Govt."))
royr2
  • 2,239
  • 15
  • 20