Manual color scales in ggplot

456 views
Skip to first unread message

Eliot Miller

unread,
Apr 14, 2014, 3:45:19 AM4/14/14
to ggp...@googlegroups.com
Hi all,

Apologies, but I know that this is a simple question. I can't seem to get what I want to happen. I have a set of polygons, and I have assigned values to them outside of ggplot, then converted those values to r,g,b colors. I have a data frame that schematically looks something like this:

test <- data.frame(plot=c(1,1,1,1,1,2,2,2,2,2), lat=c(1,1,2,2,1,3,3,4,4,3), long=c(1,2,2,1,1,3,4,4,3,3), poly.color=c(rep("#CE3B57",5),rep("#BA5D58",5)))

It would seem to me that I should be able to plot it like this:

test.plot <- ggplot(test) +
    geom_polygon(aes(x=long, y=lat, group=plot)) +
    scale_fill_manual(breaks=plot, values=poly.color)

Or perhaps like this:

test2 <- data.frame(plot=unique(test$plot),color=unique(test$poly.color))

test.plot <- ggplot(test) +
    geom_polygon(aes(x=long, y=lat, group=plot)) +
    scale_fill_manual(breaks=test2$plot, values=test2$color)

But neither of those work. What am I not understanding here?

Thanks for any help you can offer!

Cheers,
Eliot

Honglong Wu

unread,
Apr 14, 2014, 4:07:14 AM4/14/14
to Eliot Miller, ggp...@googlegroups.com
can't understand what you want to do.
maybe you can try the following code: ggplot(test) + geom_polygon(aes(x=long, y=lat, group=plot,fill=poly.color))


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eliot Miller

unread,
Apr 14, 2014, 11:53:34 AM4/14/14
to Honglong Wu, ggp...@googlegroups.com
Hi, thank you very much for the response! This is what I want with a major caveat. Those codes I provided (e.g. "#CE3B57") are actual codes for colors. I want the polygons filled with the provided colors. You can see what I mean with this modified code:

test <- data.frame(plot=c("one","one","one","one","one","two","two","two","two","two"), lat=c(1,1,2,2,1,3,3,4,4,3), long=c(1,2,2,1,1,3,4,4,3,3), poly.color=c(rep("#CE3B57",5),rep("#BA5D58",5)))

test.plot <- ggplot(test) +
    geom_polygon(aes(x=long, y=lat, group=plot, color=factor(plot))) +
    scale_fill_manual(values=c("one"="red","two"="blue"))

The issue is that in real life, I have a color gradient I have made manually (I want to directly control each r, g, b element in the code so I don't want to give that control up to ggplot) with hundreds of levels. I do not want to manually type out "one"="#CE3B57", "eighthundredfiftyseven"="#CE3B55", etc.

Does that make sense?

Thanks!
Eliot

Eliot Miller

unread,
Apr 14, 2014, 12:06:21 PM4/14/14
to ggp...@googlegroups.com
Hi all, sorry for the multiple posts. I eventually solved this. It's pretty simple.

Make a vector of the colors you want to plot. For instance, following the original example (I haven't figured out why in this test case I need "as.character" while in my real example I don't, but it doesn't seem important):

your.colors <- as.character(test$poly.color)

Then give the elements of that vector names that correspond to the polygon elements. Then just use it like below:

names(your.colors) <- test$plot

test.plot <- ggplot(test) +
    geom_polygon(aes(x=long, y=lat, group=plot, fill=factor(plot))) +
    scale_fill_manual(values=your.colors)

Dennis Murphy

unread,
Apr 14, 2014, 2:32:53 PM4/14/14
to Eliot Miller, ggplot2
Hi:

Here are two ways you could go about this, but I would slightly
rewrite your data frame code to avoid automatic conversion of color
strings to factor.

test <- data.frame(plot=c(1,1,1,1,1,2,2,2,2,2),
lat=c(1,1,2,2,1,3,3,4,4,3),
long=c(1,2,2,1,1,3,4,4,3,3),
poly.color=rep(c("#CE3B57", "#BA5D58"), each = 5),
stringsAsFactors = FALSE)


1. Use scale_fill_manual() as you did in your later posts.


2. A less well-known feature is scale_*_identity(), where the colors
themselves are the breaks and values (and the default labels. This
also works:

ggplot(test) +
geom_polygon(aes(x=long, y=lat, fill= poly.color)) +
scale_fill_identity(guide = "legend", labels = c(1, 2)) +
labs(fill = "plot")

By default, scale_identity() does not produce a guide, so you have to
specify the type of guide you want before you can change any of the
scale's properties (in this case, the labels).

Dennis

Eliot Miller

unread,
Apr 14, 2014, 4:59:08 PM4/14/14
to Dennis Murphy, ggplot2
This is great, thanks Dennis!
Reply all
Reply to author
Forward
0 new messages