Take colour value from data frame

39 views
Skip to first unread message

july

unread,
Mar 19, 2011, 9:01:47 AM3/19/11
to ggplot2
Hello,

I have a data frame which already contains colour values for each data
point and I'd like to display the data points with the given colour.
But how???

Antje

Kohske Takahashi

unread,
Mar 19, 2011, 9:22:24 AM3/19/11
to july, ggplot2
Hi,

here is a minimal example:

d <- data.frame(x=1:3, y=1:3, co=c("green", "pink", "green"))
ggplot(d, aes(x,y,colour=co)) + geom_point() +
scale_colour_manual(value=d$co, breaks=d$co)

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

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

Antje Niederlein

unread,
Mar 19, 2011, 11:07:19 AM3/19/11
to Kohske Takahashi, ggplot2
The problem is that I have continuous data points and
scale_colour_manual complains about it.
I would like to apply certain colours for certain intervals.

Antje

--
Antje Niederlein, Dipl. Informatikerin (FH)
Research Assistant, Image Analysis
HT-Technology Development Studio (TDS)

Max Planck Institute of Molecular Cell Biology and Genetics (MPI-CBG)

Pfotenhauerstrasse 108
D-01307 Dresden
Phone: ++49 351 210 - 2724
Fax: ++49 351 210 - 1349
E-mail: nied...@mpi-cbg.de
Internet: http://mpi-cbg.de/

Antje Niederlein

unread,
Mar 19, 2011, 11:15:22 AM3/19/11
to Kohske Takahashi, ggplot2
It works if I use the interval-level as value to apply the colour on
but it gets incredibly slow (I have 284 colours) and I end up with a
long legend. Is there any way to produce scale bars? And is there
anything I can do to make it faster?

Antje

On 19 March 2011 16:07, Antje Niederlein

Kohske Takahashi

unread,
Mar 19, 2011, 8:09:20 PM3/19/11
to Antje Niederlein, ggplot2
Hi,

could you please provide a minimum example?

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Antje Niederlein

unread,
Mar 21, 2011, 3:58:10 AM3/21/11
to Kohske Takahashi, ggplot2
Hi,

here is a little example

library(ggplot2)

row <- 1:10
col <- 1:10
myDF <- expand.grid(row = row, col = col)
myDF$value <- 1:100
myDF$colIv <- cut(myDF$value, breaks = seq(0,100,2))

my.colors <- c("blue",rainbow(48),"green")

p <- ggplot(myDF, aes(x = col, y = row, fill = colIv)) + geom_tile()
p <- p + scale_fill_manual(values = my.colors)
p

The legend I get is pretty long and using my real data it slows down
the plot. If I don't plot the legend it's fine. A colour scale bar
would be nice in this case (something like this:
http://www.mathworks.com/matlabcentral/fx_files/2558/2/colorscales.png
)

Hadley Wickham

unread,
Mar 21, 2011, 9:36:19 AM3/21/11
to july, ggplot2
+ scale_colour_identity()

Hadley

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

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Antje Niederlein

unread,
Mar 22, 2011, 4:20:30 AM3/22/11
to Kohske Takahashi, Hadley Wickham, ggplot2
Nice feature :-) I would like to see it implemented in the future...

I just went for the discrete scale because I could not find any way to
apply customized colours on a continuous scale (exactly defining
colors for given intervals). In the end, color mapping is nothing else
than mapping discrete values ( colours) to intervals of continuous
values, no?
I don't use Matlab at all, I just sent this link to make sure that you
know what I'm looking for.

For now, I use a simple solution - creating a colorbar as additional plot:

library(ggplot2)

row <- 1:10
col <- 1:10

my.colors <- c("blue",rainbow(48),"green")

myDF <- expand.grid(row = row, col = col)
myDF$value <- 1:100
myDF$colIv <- my.colors[unclass(cut(myDF$value, breaks = seq(0,100,2)))]
myDF$meta <- "data"

scaleDF <- data.frame(col = 1, row = 1:50, colIv = my.colors, meta =
"scalebar", value = NA)

myDF <- rbind(myDF,scaleDF)

p <- ggplot(myDF, aes(x = col, y = row, fill = colIv)) + geom_tile()

p <- p + scale_fill_identity()
p <- p + facet_wrap( ~ meta , scales = "free_y")
p <- p + opts(legend.position = "none")
p


Antje

On 22 March 2011 03:05, Kohske Takahashi <takahash...@gmail.com> wrote:
> Hi,
>

> yes, scale_*_identity does this.
>
> As for the matlab type colormap (colorbar), now I'm just on the way to
> implement that for my own use.
> Generally, colormap cannot work with discrete scale, so maybe it is
> not available for your case (even in matlab? i don't know matlab is
> sensitive to if the variable is discrete or continuous).
>
> I attached the example code and output, but the current state is very
> very ad-hoc and hence is even far from alpha, and I'm not sure this
> feature is merged in the future release.
> If many people want this feature, and Hadley are considering to merge
> this kind of feature, then maybe I will try to make a progress on this
> feature.
>
>
> # note that this code cannot work with current version of ggplot2
> d <- data.frame(x=1:100, y=runif(100), fi=runif(100))
> ggplot(d, aes(x,y,colour=fi, size=fi)) + geom_point() +
> scale_colour_continuous(colorbar=TRUE)
>
> colorbar=TRUE works for scale_fill or scale_color (maybe also scale_alpha?)


>
> --
> Kohske Takahashi <takahash...@gmail.com>
>
> Research Center for Advanced Science and Technology,
> The University of  Tokyo, Japan.
> http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
>
>
>

--

Kohske Takahashi

unread,
Mar 21, 2011, 10:05:00 PM3/21/11
to Hadley Wickham, july, ggplot2
Hi,

yes, scale_*_identity does this.

As for the matlab type colormap (colorbar), now I'm just on the way to
implement that for my own use.
Generally, colormap cannot work with discrete scale, so maybe it is
not available for your case (even in matlab? i don't know matlab is
sensitive to if the variable is discrete or continuous).

I attached the example code and output, but the current state is very
very ad-hoc and hence is even far from alpha, and I'm not sure this
feature is merged in the future release.
If many people want this feature, and Hadley are considering to merge
this kind of feature, then maybe I will try to make a progress on this
feature.


# note that this code cannot work with current version of ggplot2
d <- data.frame(x=1:100, y=runif(100), fi=runif(100))
ggplot(d, aes(x,y,colour=fi, size=fi)) + geom_point() +
scale_colour_continuous(colorbar=TRUE)

colorbar=TRUE works for scale_fill or scale_color (maybe also scale_alpha?)

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

On Mon, Mar 21, 2011 at 10:36 PM, Hadley Wickham <had...@rice.edu> wrote:

screen_shot.png

Brandon Hurr

unread,
Mar 22, 2011, 7:15:05 AM3/22/11
to Antje Niederlein, Kohske Takahashi, Hadley Wickham, ggplot2
I agree that this is a nice feature and would also like to see it implemented in a future ggplot2 release. 

Brandon

Hadley Wickham

unread,
Mar 22, 2011, 8:34:23 AM3/22/11
to Kohske Takahashi, july, ggplot2
> I attached the example code and output, but the current state is very
> very ad-hoc and hence is even far from alpha, and I'm not sure this
> feature is merged in the future release.
> If many people want this feature, and Hadley are considering to merge
> this kind of feature, then maybe I will try to make a progress on this
> feature.

This would be cool - for integration into ggplot2, it would great if
you could make sure that it was implemented as it's own grid grob.
This makes use in other places easier.

Hadley

Osmo Salomaa

unread,
Mar 23, 2011, 4:41:27 AM3/23/11
to ggplot2
On Mar 22, 4:05 am, Kohske Takahashi <takahashi.koh...@gmail.com>
wrote:
> If many people want this feature, and Hadley are considering to merge
> this kind of feature, then maybe I will try to make a progress on this
> feature.

I would definitely find this useful. I often make choropleth maps
without classification because I don't want to butcher the data. The
current legends (with e.g. scale_color_gradientn) sort of imply
classification and some people find it confusing. A colorbar legend
would better communicate the continuous nature of the scale.
Reply all
Reply to author
Forward
0 new messages