Order in stacked barplot

Hello. Trying to work with this df to make a stacked barplot

party <- data.frame(
  stringsAsFactors = FALSE,
        party_name = c("Ex-Left","Ex-Left","Ex-Left",
                       "Ex-Left","Ex-Left","Ex-Left","Ex-Left","Ex-Left",
                       "Ex-Left","Ex-Left","Ex-Left","Left","Left","Left",
                       "Left","Left","Left","Left","Left","Left","Left",
                       "Left","Conservative","Conservative","Right National",
                       "Right National","Right National","Right National",
                       "Right National","Right National","Right National",
                       "Right National","Right National","Liberal","Liberal",
                       "Liberal","Liberal","Liberal","Liberal","Liberal",
                       "FarRight","FarRight","FarRight","FarRight","FarRight",
                       "FarRight","FarRight","FarRight","FarRight","FarRight",
                       "FarRight"),
             value = c(924,1.209,1.321,2.398,1.45,
                       1.477,1.749,1.7,2.552,773,2.552,2.307,16.647,
                       12.514,10.542,11.226,9.766,12.058,10.426,8.444,6.766,
                       10.407,2.522,6.013,2.989,2.896,3.493,2.39,2.471,
                       1.671,1.713,1.642,1.466,2.67,2.998,900,1.032,906,
                       1.688,2.374,6.787,7.537,8.429,13.934,18.076,
                       17.747,17.216,13.482,7.533,7.436,5.719),
          election = c(1983L,1987L,1991L,1995L,
                       1999L,2003L,2007L,2011L,2015L,2019L,2015L,2019L,
                       1983L,1987L,1991L,1995L,1999L,2003L,2007L,2011L,2015L,
                       2019L,2019L,2015L,2019L,2015L,1987L,1991L,1995L,
                       1999L,2003L,2007L,2011L,2019L,2015L,2003L,2007L,
                       2011L,2015L,2019L,1983L,1987L,1991L,1995L,1999L,
                       2003L,2007L,2011L,2015L,2019L,2011L)

I'm using this code to plot:

ggplot(party,
       aes(y = value, x = as.factor(election), fill = party_name)) +
  geom_bar(position = "fill", stat = "identity", width = 0.8, colour = F) +
  coord_flip() 

But, ggplot does not respect the order:

I want to keep the ideological spectrum, i.e.: Ex-Left, Left, Conservative, Right National, Liberal, FarRight.

Thanks.

Like this?

party <- data.frame(
  stringsAsFactors = FALSE,
  party_name = c("Ex-Left","Ex-Left","Ex-Left",
                 "Ex-Left","Ex-Left","Ex-Left","Ex-Left","Ex-Left",
                 "Ex-Left","Ex-Left","Ex-Left","Left","Left","Left",
                 "Left","Left","Left","Left","Left","Left","Left",
                 "Left","Conservative","Conservative","Right National",
                 "Right National","Right National","Right National",
                 "Right National","Right National","Right National",
                 "Right National","Right National","Liberal","Liberal",
                 "Liberal","Liberal","Liberal","Liberal","Liberal",
                 "FarRight","FarRight","FarRight","FarRight","FarRight",
                 "FarRight","FarRight","FarRight","FarRight","FarRight",
                 "FarRight"),
  value = c(924,1.209,1.321,2.398,1.45,
            1.477,1.749,1.7,2.552,773,2.552,2.307,16.647,
            12.514,10.542,11.226,9.766,12.058,10.426,8.444,6.766,
            10.407,2.522,6.013,2.989,2.896,3.493,2.39,2.471,
            1.671,1.713,1.642,1.466,2.67,2.998,900,1.032,906,
            1.688,2.374,6.787,7.537,8.429,13.934,18.076,
            17.747,17.216,13.482,7.533,7.436,5.719),
  election = c(1983L,1987L,1991L,1995L,
               1999L,2003L,2007L,2011L,2015L,2019L,2015L,2019L,
               1983L,1987L,1991L,1995L,1999L,2003L,2007L,2011L,2015L,
               2019L,2019L,2015L,2019L,2015L,1987L,1991L,1995L,
               1999L,2003L,2007L,2011L,2019L,2015L,2003L,2007L,
               2011L,2015L,2019L,1983L,1987L,1991L,1995L,1999L,
               2003L,2007L,2011L,2015L,2019L,2011L))
party$party_name <- factor(party$party_name,
                           levels = c("FarRight","Liberal","Right National",
                                      "Conservative","Left","Ex-Left"))  

library(ggplot2)
ggplot(party,
       aes(y = value, x = as.factor(election), fill = party_name)) +
  geom_bar(position = "fill", stat = "identity", width = 0.8, colour = F) +
  coord_flip() 

Created on 2022-01-24 by the reprex package (v2.0.1)

4 Likes

Works fine, but, is it possible to change the order of the legend?: Ex-Left as first item, Left second, and so on?

Thanks a lot FJCC

I try with fill = forcats::fct_rev(party_name), but then graphic also changes and don't keeps the order...

Ok I found it :sweat_smile:

scale_fill_discrete(guide = guide_legend(reverse=TRUE))

This code does the job.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.