0

I am currently working on a dataframe and I want to make a barplot in which every cloumn is related to a variable, and I want to assign to each column (each bar of the barplot) an specific color, different than the predefined color assigned by ggplot.

This is my dataframe

    Abstencion    PP    PSOE    IU    UPyD   Ahora.madrid Ciudadanos Year
1      40.86     27.79  20.20  5.72   0.00         0.00       0.00  a91
2      28.87     37.35  19.62 11.02   0.00         0.00       0.00  a95
3      39.94     29.54  21.49  5.17   0.00         0.00       0.00  a99
4      31.07     35.21  25.18  4.95   0.00         0.00       0.00  a03
5      34.09     36.48  20.28  5.69   0.00         0.00       0.00  a07
6      32.78     32.79  15.79  7.09   5.18         0.00       0.00  a11
7      31.15     23.62  10.44  1.16   1.25        21.76       7.81  a15

The columns of the dataset are actually political parties in my country, so I want to produce a barplot depending on the year, in which each column should be plotted in the colour of the political party.

I already designed the color vector:

col <- c("black","dodgerblue","firebrick1","chartreuse3",
         "deeppink","darkorchid3","darkorange1")

And I developed this code to do the barplots (suppose my dataframe is X)

library(ggplot2)
library(reshape2)
a <- X[1,]
a.molten <- melt(a, value.name="Votes", variable.name="Party")
#Eliminar los partidos politicos que sacaron 0 votos
a.molten <- a.molten[-which(a.molten$Votes==0),] 
ggplot(a.molten, aes(x=Party, y=Votes, fill=Party)) + geom_bar(stat="identity")

changing the parameter from a <- X[i,] will produce the barplots for each year, but now I am stuck on assigning each party its color from the vector "col."

lmo
  • 37,904
  • 9
  • 56
  • 69
  • 3
    `?scale_colour_manual`, `?scale_fill_manual` – Ben Bolker Jun 07 '16 at 12:11
  • I feel like this must be answered somewhere on SO, but so far I have only found *related* questions - nothing that answers precisely this question, but several that show in passing how to use `scale_*_manual`. This might help: http://stackoverflow.com/questions/20099594/customise-colors-of-graphs-in-ggplot/20104120#20104120 – Ben Bolker Jun 07 '16 at 12:42

0 Answers0