r - Negative valued factors in stacked barplot -
i trying figure out way of introducing negative values of factors in stacked barplot in ggplot2
. data level of support basic income among finnish mps. @ bottom of post.
i can plot 1 want (minus negatively valued factors) following code:
library(forcats) library(ggplot2) support.plot <- ggplot(mpsupport.df, aes(fct_infreq(party))) + geom_bar (aes(fill=support)) + coord_flip() + theme(legend.position = "bottom")+ ylab("party") + xlab("number of mps")
this gives following:
what graph centred on green-turquoise border, support basic income right, while opposition left. make sense?
data:
> dput(mpsupport.df) structure(list(party = structure(c(1l, 2l, 2l, 2l, 2l, 2l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 6l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 5l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 7l, 7l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 5l, 5l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 7l, 7l, 7l, 7l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 3l, 3l, 3l, 3l, 3l, 3l, 5l, 6l, 7l, 7l, 7l), .label = c("national coalition", "centre party", "social democratic party", "left alliance", "christian democrats", "true finns", "swedish people's party", "greens"), class = "factor"), support = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l), .label = c("fully.agree", "partially.agree", "partially.disagree", "fully.disagree"), class = "factor")), .names = c("party", "support"), row.names = c("1", "2", "2.1", "2.2", "2.3", "2.4", "4", "4.1", "4.2", "4.3", "4.4", "4.5", "4.6", "4.7", "6", "8", "8.1", "8.2", "8.3", "8.4", "8.5", "8.6", "8.7", "8.8", "8.9", "8.10", "8.11", "8.12", "8.13", "8.14", "9", "9.1", "9.2", "9.3", "9.4", "9.5", "9.6", "9.7", "10", "10.1", "10.2", "10.3", "10.4", "10.5", "10.6", "10.7", "10.8", "10.9", "10.10", "10.11", "10.12", "10.13", "10.14", "10.15", "10.16", "10.17", "10.18", "10.19", "10.20", "10.21", "10.22", "10.23", "10.24", "10.25", "10.26", "10.27", "10.28", "10.29", "10.30", "10.31", "10.32", "10.33", "11", "11.1", "11.2", "11.3", "12", "12.1", "12.2", "12.3", "13", "14", "14.1", "14.2", "14.3", "14.4", "14.5", "14.6", "14.7", "14.8", "14.9", "14.10", "14.11", "14.12", "14.13", "14.14", "14.15", "14.16", "14.17", "14.18", "14.19", "14.20", "15", "15.1", "17", "17.1", "17.2", "17.3", "17.4", "17.5", "17.6", "17.7", "17.8", "17.9", "17.10", "17.11", "17.12", "17.13", "17.14", "17.15", "17.16", "17.17", "17.18", "17.19", "18", "18.1", "18.2", "18.3", "18.4", "18.5", "18.6", "18.7", "19", "19.1", "19.2", "19.3", "19.4", "19.5", "19.6", "19.7", "19.8", "19.9", "19.10", "19.11", "19.12", "19.13", "19.14", "19.15", "19.16", "19.17", "19.18", "19.19", "19.20", "19.21", "19.22", "19.23", "21", "21.1", "22", "22.1", "22.2", "22.3", "22.4", "22.5", "22.6", "22.7", "22.8", "22.9", "22.10", "22.11", "22.12", "23", "23.1", "23.2", "23.3", "25", "25.1", "25.2", "25.3", "25.4", "25.5", "25.6", "27", "27.1", "27.2", "27.3", "27.4", "27.5", "29", "30", "31", "31.1", "31.2"), class = "data.frame")
try along these lines:
library(ggplot) library(forcats) mpsupport.df$dummy = ifelse(mpsupport.df$support %in% c("fully.agree", "partially.agree"), 1, -1) agg = aggregate(dummy ~ support + party, data = mpsupport.df, fun = sum) ggplot(data = agg)+ geom_bar (aes(y = dummy, x= fct_infreq(party), fill = factor(support, levels = c("fully.agree", "partially.agree", "fully.disagree" ,"partially.disagree"))), stat= "identity") + coord_flip()+ theme(legend.position = "bottom", legend.title = element_blank())
Comments
Post a Comment