geom_raster - factor values

224 views
Skip to first unread message

Tim_Haering

unread,
Mar 30, 2012, 5:25:18 AM3/30/12
to ggplot2
Hi all,

I have problems plotting a raster with factor values. Is there anyone
who has a solution?

library(ggplot2)
library(raster)

### load raster
f <- system.file("external/test.grd", package="raster")
r <- raster(f)

### extract coordinates and values
val <- getValues(r)
xy <- as.data.frame(xyFromCell(r,1:ncell(r)))
xy <- cbind(xy,val)

# plot - OK
ggplot(xy, aes(x=x, y=y, fill=val)) + geom_raster() + coord_equal()

### now, I reclass the raster
r <- reclass(r, c(0,500,1, 500,2000,2))

val <- getValues(r)
xy <- as.data.frame(xyFromCell(r,1:ncell(r)))
xy <- cbind(xy,val)

# plot classified raster - OK
# but legend is continuous
ggplot(na.omit(xy), aes(x=x, y=y, fill=val)) + geom_raster() +
coord_equal()

# if I plot the values as factor, the map becomes wrong
ggplot(na.omit(xy), aes(x=x, y=y, fill=factor(val))) + geom_raster() +
coord_equal()

Thanks for every hint!

TIM

Kohske Takahashi

unread,
Apr 2, 2012, 1:46:52 AM4/2/12
to Tim_Haering, ggplot2
Here is a workaround:

ggplot(na.omit(xy), aes(x=x, y=y, fill=factor(val), group = 1)) +
geom_raster() +
+ coord_equal()

definitely this is a bug and will be fixed in the future version.

2012年3月30日18:25 Tim_Haering <tim.h...@basf.com>:


> val <- getValues(r)
> xy <- as.data.frame(xyFromCell(r,1:ncell(r)))
> xy <- cbind(xy,val)

--
--
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

Hadley Wickham

unread,
Apr 11, 2012, 4:28:25 PM4/11/12
to Kohske Takahashi, Tim_Haering, ggplot2
You filled a bug report for this right?
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/

Kohske Takahashi

unread,
Apr 15, 2012, 10:45:29 PM4/15/12
to Hadley Wickham, Tim_Haering, ggplot2
> You filled a bug report for this right?

Not sure... but probably no.

Tim_Haering

unread,
Apr 16, 2012, 7:32:30 AM4/16/12
to ggplot2
> > You filled a bug report for this right?
>
> Not sure... but probably no.

No, I haven`t filled a but report. I can do - but don`t know where.
Could you please give me a hint?

TIM

Jean-Olivier Irisson

unread,
Apr 16, 2012, 7:50:32 AM4/16/12
to Tim_Haering, ggplot2
On 2012-Apr-16, at 13:32 , Tim_Haering wrote:
>
> No, I haven`t filled a but report. I can do - but don`t know where.
> Could you please give me a hint?

https://github.com/hadley/ggplot2/issues/

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/

Kohske Takahashi

unread,
Apr 16, 2012, 7:54:33 AM4/16/12
to Jean-Olivier Irisson, Tim_Haering, ggplot2
I found the issue, and this is fixed.

https://github.com/hadley/ggplot2/issues/463

I issued this...
Sorry for confusion, this mail was in my `starred` box...


2012年4月16日20:50 Jean-Olivier Irisson <iri...@normalesup.org>:

> --
> 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

--

Reply all
Reply to author
Forward
0 new messages