-1

I have the following data frame:

   from variable               value
1   ASM cent.degree         0.208333333
2   AUS cent.degree         0.958333333
3   COK cent.degree         0.166666667
4   FJI cent.degree         0.916666667
5   FSM cent.degree         0.208333333
6   GUM cent.degree         0.208333333
26  ASM cent.betweenness    0.000000000
27  AUS cent.betweenness    0.588500000
28  COK cent.betweenness    0.000000000
29  FJI cent.betweenness    0.509333333
30  FSM cent.betweenness    0.001666667
31  GUM cent.betweenness    0.001666667

And the following code:

  geom_bar(stat='identity', position='dodge') +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  theme(legend.position="bottom") +
  ggtitle("Központiság mutatók") +
  #scale_y_continuous(trans = "log2") +
  scale_x_discrete(name = "Származás") +
  scale_fill_discrete(name = NULL, labels = c("Degree centrality (Népszerűség)", 
                                              "Closeness centrality (Befolyás)",
                                              "Betweenness centrality (Alkuerő)",
                                              "Sajátvektor központiság (Státusz)"))

It provides a side-by-side bar chart of four variables in total, differentiating the colour based on which variable it is. The thing I'm having trouble with is changing the colours (to four new colors). I tried adding scale_fill_manual(values = oc.color[1:4]) + (where oc.color is a vector of hexacodes), but it says "Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.", and continues to use the default ggplot colors. Here's the chart I get

Thanks for any help.

massisenergy
  • 1,764
  • 3
  • 14
  • 25

2 Answers2

0

You probably added the scale_fill_manual before scale_fill_discrete. This way scale_fill_discrete will replace your colors with default colors. Best and the recommended way is to use only one scale per aesthetic. Try this:

library(ggplot2)

oc.color <- c("red", "blue", "green", "pink")

ggplot(df, aes(from, value, fill = variable)) +
  geom_bar(stat='identity', position='dodge') +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  theme(legend.position="bottom") +
  ggtitle("Központiság mutatók") +
  scale_x_discrete(name = "Származás") +
  scale_fill_manual(name = NULL, labels = c("Degree centrality (Nepszerűség)", 
                                            "Closeness centrality (Befolyás)",
                                            "Betweenness centrality (Alkuerő)",
                                            "Sajátvektor központiság (Státusz)"), values = oc.color[1:4])

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • You are welcome. If you want do me a favor: Mark the question as answered. Besides giving me some credit it shows others with a similar problem that the solution worked and removes the question from the queue of questions still waiting for an answer. – stefan Apr 19 '20 at 12:27
0

enter image description hereThis is working in my system:

oc.color <- c("#999999", "#E69F00", "#56B4E9","#56B0E9");

df %>% ggplot(aes(from, value)) + 
    geom_bar(stat='identity', position='dodge', aes(fill = variable)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    theme(legend.position="bottom") +
    ggtitle("Központiság mutatók") +
    #scale_y_continuous(trans = "log2") +
    scale_x_discrete(name = "Származás") +
    scale_fill_discrete(name = NULL, labels = c("Degree centrality (Népszerűség)", 
                                                "Closeness centrality (Befolyás)",
                                                "Betweenness centrality (Alkuerő)",
                                                "Sajátvektor központiság (Státusz)")) + 
    scale_fill_manual(values = oc.color[1:4]);
massisenergy
  • 1,764
  • 3
  • 14
  • 25