4

It's been a while since this was asked, but I know for a fact that I will be pulling new data soon and I would like to figure out how to plot it with this technique. It looks like the people in the comments and answers had an idea of how this could be don, but I couldn't quite figure it out with what was given to me. Anyone else want to take a crack at it?

I am trying to plot multiple bar graphs with factor variables that have many levels.

First, I generate a data.frame and create a factor which I order descending by a numeric value (so that the plot will move from highest to lowest). Then, I want to generate one ggplot2 object, which in this case can be gg1. This will automatically assign a color to each of the levels and build a bar chart. I then create gg2, which is the essentially the same, but with a different ordering. However, ggplot2 automatically assigns the colors based on the value of val. I need the colors to map to the character string name in a way that can be re-used in multiple ggplot objects.

The problem in a nutshell is that, in the example below, only the axis tickmark values change, as opposed to the colors as well. I tried using the method here to no avail. Any ideas?

library(ggplot2)
test1 <- data.frame(name = c('a', 'b', 'c'),
                    val = 1:3)
test1$name <- factor(test1$name,
                     levels = as.character(test1$name[order(-test1$val)]))
gg1 <- ggplot(data = test1) + geom_bar(aes(x = name, y = val, fill = val))
gg1
test2 <- data.frame(name = c('a', 'b', 'c'),
                    val = c(3, 1, 2))
test2$name <- factor(test2$name,
                     levels = as.character(test2$name[order(-test2$val)]))
gg2 <- ggplot(data = test2) + geom_bar(aes(x = name, y = val, fill = val))
gg2

This is how I tried to incorporate the method suggested in the linked solution:

g <- ggplot_build(gg1)
myPalette <- unique(g$data[[1]]["fill"])[, 1]
gg3 <- ggplot(data = test2) + geom_bar(aes(x = name, y = val, fill = val)) + 
       scale_fill_manual(values = myPalette)
gg3
Community
  • 1
  • 1
zap2008
  • 684
  • 1
  • 9
  • 24
  • 1
    You say that "ggplot2 automatically assigns the colors based on the value of val", but in your example, you've manually set the colours to be based on `val` by specifying `fill = val`. I do understand your question, about making sure `a` maps to the same colour across datasets, but at the moment your example code doesn't get it across as well as it should. – Marius Dec 17 '13 at 04:23
  • This is very true that it was either worded poorly or I didn't provide the proper code. The code I have here is just a dumbed down example of the approach I'm taking in the larger dataset. When I say "automatically assigns the colors," I am trying to say that it picks the actual color mappings for me out of the default `fill` palette; I didn't provide my own palette mapping. I am not quite sure how to re-phrase my question though... – zap2008 Dec 17 '13 at 05:31
  • Just set the limits on the scales, exactly as you would for the axes. – hadley Dec 17 '13 at 14:00
  • How would setting the limits help? I would think that this is an issue with the `values` rather than the `limit`... – zap2008 Jan 03 '14 at 16:42

1 Answers1

4

Try this

test2 <- data.frame(name = c('a', 'b', 'c'),
                    val = c(3, 1, 2,3,2,1,1,2,3))

add labels to the fill val

test2$val <- factor(test2$val,
                    levels = c(1,2,3),
                    labels = c("One", "Two","Three")) 
ggplot(data = test2) + geom_bar(aes(x = name,fill=val)) +
  scale_fill_manual(values = c('One' = "red", 'Two' = "orange", 'Three' = "blue"))
Keniajin
  • 1,649
  • 2
  • 20
  • 43
  • To use the colors in different plots, you can define values as a variable – Keniajin Dec 17 '13 at 09:41
  • Hmm I'm not sure how this solves my particular problem, though defining `values` as text in the way you did might be a step in the right direction. – zap2008 Jan 03 '14 at 16:39