I want to create a plot using ggplot2 showing the percentage of vote for political parties in a particular region relative to the number of voters in the region not in the country.
This is my data frame:
data <- read.table(header=TRUE, text='
REGION Q99
Tunis Nahdha
Tunis Jabha
Tunis NidaaTounes
Tunis Nepasvoter
Tunis Nepasvoter
Tunis Nepasvoter
Tunis NidaaTounes
Tunis Nepasvoter
Tunis Nahdha
Tunis NidaaTounes
Tunis CPR
Tunis Nahdha
Tunis Autres
Tunis Nepasvoter
Tunis Nepasvoter
Tunis Nahdha
Tunis Jabha
Tunis Nepasvoter
Tunis Nahdha
Tunis Nahdha
Tunis JabhaChaabia
Tunis Autres
Tunis Nepasvoter
Tunis NidaaTounes
Tunis Nepasvoter
Tunis NidaaTounes
Tunis Nepasvoter
Tunis NidaaTounes
Tunis Jabha
Tunis NidaaTounes
Tunis Autres
Tunis Nahdha
Tunis Nahdha
Tunis Autres
Tunis Jabha
Tunis Nepasvoter
Tunis Nepasvoter
Tunis CPR
Tunis Nahdha
Tunis Nepasvoter
Tunis Nepasvoter
Tunis Nepasvoter
Tunis Nahdha
Tunis NidaaTounes
Tunis CPR
Tunis CPR
Tunis Nepasvoter
Tunis Autres
Tunis Nahdha
Tunis NidaaTounes
Tunis Nahdha
Tunis Autres
Tunis Nepasvoter
Ariana Nahdha
Ariana CPR
Ariana Nahdha
Ariana Nepasvoter
Ariana NidaaTounes
Ariana CPR
Ariana Nepasvoter
Ariana Nahdha
Ariana Nepasvoter
Ariana NidaaTounes
Ariana CPR
Ariana NidaaTounes
Ariana NidaaTounes
Ariana NidaaTounes
Ariana CPR
Ariana Nahdha
Ariana CPR
Ariana Nahdha
Ariana Nahdha
Ariana CPR
Ariana Nahdha
Ariana Nepasvoter
Ariana NidaaTounes
Ariana NidaaTounes
Ariana Nahdha ')
First I used this code:
g <- ggplot(data, aes(x = REGION, y =(..count..)/(nrow(data[data$REGION=="Ariana",])), fill = Q99))
g <- g + geom_bar(position = "stack")
But I got this error:
Error in nrow(data[data$REGION == "Ariana", ]) : object 'data' not found
then I tried to use environment= environment()
scale = nrow(data[data$REGION=="Ariana",])
g <- ggplot(data, aes(x = REGION, y = (..count..)/scale, fill = Q99), environment = environment())
g <- g + geom_bar(position = "stack")
then I got this error:
Error in (count)/scale : non-numeric argument to binary operator