[Barplotting] assigning a specific colors to specific names

I am barplotting a data frame, that has Finnish election results per district and party listed. I am trying to barplot election results per district with the ten most popular parties per district plotted. When printing out the barplot, I would like each party to have a specific color. For example, in the data SDP party are Social Democrats, so the party is associated with the color red, PS are blue etc. Thus, if the SDP are in the top 10 parties, I would like them to always be the specific color, no matter their position in the barplot. How can this be achieved in ggplot2 or in the basic barplot function?

Here is one way to correlate specific colors to values of a column. I invented party names and colors with no knowledge of Finnish politics!

library(ggplot2)
DF <- data.frame(District = rep(LETTERS[1:4], each = 4),
                 Party = c("SPD", "PS", "Pirate", "Liberal", 
                           "Freedom", "PS", "SPD", "Liberal",
                           "SPD", "PS", "Freedom", "Pirate",
                           "Pirate", "Republican", "SPD", "PS"),
                 Vote = runif(16, 20,  40))
COLORS <- c("SPD" = "red", "PS" = "blue", "Pirate" = "black", "Liberal" = "yellow",
            "Freedom" = "white", "Republican" = "orange")

ggplot(DF, aes(x = District, y = Vote, fill = Party)) + geom_col(position = "dodge") +
  scale_fill_manual(values = COLORS)

Created on 2020-06-22 by the reprex package (v0.3.0)

1 Like

I think you need to reshape the data with the pivot_longer() function from tidyr. If you can post the output of the following command and explain how you want the data plotted, someone should be able to provide a specific example.
Please run this command but replace MyData with the name of your data.

dput(head(MyData))

You can reshape the data using the reshape() function. I have not used that function in many years, so the following may not be the best way to get the desired result. You should be able to use DF2 in ggplot() as in my previous example.

DF <- structure(list(Alue = c("Helsinki", "Askola", "Espoo", "Hanko",
                        "Vantaa", "Hyvinkaa"), 
               SDP = c(13.5, 18.5, 13, 28.7, 21.4, 21.8), 
               PS = c(12.3, 23.7, 11.6, 11.4, 18.3, 24), 
               KOK = c(21.8, 11.5,31, 6.1, 18.8, 17), 
               KESK = c(2.9, 23.5, 4, 1.9, 5, 7.4), 
               VIHR = c(23.5,6.4, 18, 5.5, 14.5, 11.2), 
               VAS = c(11.1, 4.2, 3.5, 7.9, 5.9,5.5), 
               RKP = c(5.3, 2.4, 7.7, 30.1, 2.8, 1), 
               KD = c(1.9, 2.4,2.7, 2.2, 3, 3.3), 
               SIN = c(0.5, 1.6, 1.7, 0.9, 1.9, 1.7), 
               PIR = c(1.5,0.5, 1, 0.6, 1, 0.7), 
               STL = c(0.3, 0.5, 0.4, 0.7, 0.6, 0.5),
               KP = c(0, 0.1, 0, 0.2, 0.1, 0), 
               FP = c(0.9, 0, 0.2, 0.1,0.2, 0.2), 
               LIB = c(0.3, 0.2, 0.5, 0.2, 0.3, 0.2), 
               SKP = c(0.2,0.3, 0.2, 0.5, 0.3, 0.3), 
               EOP = c(0.2, 0.2, 0.2, 0.2, 0.2,0.2), 
               IP = c(0.1, 0.2, 0.1, 0.1, 0.2, 0.1), 
               SKE = c(0.1,0.1, 0.1, 0.1, 0.2, 0.1), 
               KTP = c(0, 0, 0, 0.1, 0, 0), 
               Muut = c(3.5,3.7, 4.1, 2.5, 5.4, 4.7), 
               Vaalipiiri = c("Helsinki", "Uusimaa","Uusimaa", "Uusimaa", "Uusimaa", "Uusimaa")), 
          row.names = c(NA,6L), class = "data.frame")
DF2 <- reshape(data = DF, idvar = c("Alue", "Vaalipiiri"), varying = list(2:21), 
               timevar = "Party", direction = "long", times = colnames(DF)[2:21], 
               v.names = "Value")
head(DF2, 10)
#>                           Alue Vaalipiiri Party Value
#> Helsinki.Helsinki.SDP Helsinki   Helsinki   SDP  13.5
#> Askola.Uusimaa.SDP      Askola    Uusimaa   SDP  18.5
#> Espoo.Uusimaa.SDP        Espoo    Uusimaa   SDP  13.0
#> Hanko.Uusimaa.SDP        Hanko    Uusimaa   SDP  28.7
#> Vantaa.Uusimaa.SDP      Vantaa    Uusimaa   SDP  21.4
#> Hyvinkaa.Uusimaa.SDP  Hyvinkaa    Uusimaa   SDP  21.8
#> Helsinki.Helsinki.PS  Helsinki   Helsinki    PS  12.3
#> Askola.Uusimaa.PS       Askola    Uusimaa    PS  23.7
#> Espoo.Uusimaa.PS         Espoo    Uusimaa    PS  11.6
#> Hanko.Uusimaa.PS         Hanko    Uusimaa    PS  11.4

Created on 2020-06-23 by the reprex package (v0.3.0)

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

Looks promising! in my data frame however, party names are column names, and I would like to assign x in aes to be the parties. Would I have to arbitrarily add a new column that lists party names or is there a way to work around that?

You can still post the output of dput() and get some advice.
Please also take a look at the forum's homework policy. The more specific you can make your question, the better.

Unfortunately this is a school project, that specifically states that we cannot download other packages other than ggplot2 if we need. Thus dplyr is not an option for me.

dput(head(suomi))
structure(list(Alue = c("Helsinki", "Askola", "Espoo", "Hanko",
"Vantaa", "Hyvinkää"), SDP = c(13.5, 18.5, 13, 28.7, 21.4, 21.8
), PS = c(12.3, 23.7, 11.6, 11.4, 18.3, 24), KOK = c(21.8, 11.5,
31, 6.1, 18.8, 17), KESK = c(2.9, 23.5, 4, 1.9, 5, 7.4), VIHR = c(23.5,
6.4, 18, 5.5, 14.5, 11.2), VAS = c(11.1, 4.2, 3.5, 7.9, 5.9,
5.5), RKP = c(5.3, 2.4, 7.7, 30.1, 2.8, 1), KD = c(1.9, 2.4,
2.7, 2.2, 3, 3.3), SIN = c(0.5, 1.6, 1.7, 0.9, 1.9, 1.7), PIR = c(1.5,
0.5, 1, 0.6, 1, 0.7), STL = c(0.3, 0.5, 0.4, 0.7, 0.6, 0.5),
KP = c(0, 0.1, 0, 0.2, 0.1, 0), FP = c(0.9, 0, 0.2, 0.1,
0.2, 0.2), LIB = c(0.3, 0.2, 0.5, 0.2, 0.3, 0.2), SKP = c(0.2,
0.3, 0.2, 0.5, 0.3, 0.3), EOP = c(0.2, 0.2, 0.2, 0.2, 0.2,
0.2), IP = c(0.1, 0.2, 0.1, 0.1, 0.2, 0.1), SKE = c(0.1,
0.1, 0.1, 0.1, 0.2, 0.1), KTP = c(0, 0, 0, 0.1, 0, 0), Muut = c(3.5,
3.7, 4.1, 2.5, 5.4, 4.7), Vaalipiiri = c("Helsinki", "Uusimaa",
"Uusimaa", "Uusimaa", "Uusimaa", "Uusimaa")), row.names = c(NA,
6L), class = "data.frame")