plotting xyz bathymetry file with geom_raster

500 views
Skip to first unread message

Aurelie Cosandey Godin

unread,
Apr 23, 2012, 8:13:35 PM4/23/12
to ggplot2
Hi all,

I'm trying to use geom_raster to plot the bathymetry of my area of
interest.
I downloaded an xyz txt file from http://topex.ucsd.edu and uploaded
the data into R as follow:

SF.bath <- read.table("SF_bath.txt",
header=F,col.names=c("long","lat","z"))#

> head(SF.bath)
lon lat z
1 292.0083 50.9968 757
2 292.0250 50.9968 693
3 292.0417 50.9968 673
4 292.0583 50.9968 769
5 292.0750 50.9968 761
6 292.0916 50.9968 733

I can plot my data as a raster image using the following code:

P<- ggplot(SF.bath, aes(lon, lat, fill = z)) +
geom_tile(colour = "white") + scale_fill_gradient(low="darkblue",
high="black")
P

However, when I use geom_raster I get an error message as follow:
newp<-qplot(lon, lat, data = SF.bath, fill = z, geom = "raster")
> newp
Using alpha as value column: use value_var to override.

I'm not sure what I need to do to fix this.

Thank you very much in advance,
Aurelie

Ben Bond-Lamberty

unread,
Apr 23, 2012, 8:43:59 PM4/23/12
to ggplot2
Aurelie,

Your example works fine for me* (using R 2.14.1, ggplot 0.9), with
both the snippet you posted and a file from that website. Perhaps
check "summary(SF.bath)" and make sure all three numbers are correctly
numeric? Just a guess.

Ben

*Note however there's an inconsistency in what you posted--you're
assigning the column name "long" to the first column of the data in
read.table, but asking ggplot to plot "lon".

Aurelie Cosandey Godin

unread,
Apr 23, 2012, 9:02:55 PM4/23/12
to Ben Bond-Lamberty, ggplot2
Thanks Ben,

Yes, I noticed my typo on the long vs lon, but this wasn't the problem (my mistake when I typed the question). I just tried again and verified if all my data were indeed numerical. They were, but I still get this message "Using alpha as value column: use value_var to override." and get an empty plot with the correct legend.

I was able to plot the contour line using the following code, but again the colours do not show on the map... I get a white map with my contour lines in black and the correct legend (see plot attached). I also get the same message  "Using alpha as value column: use value_var to override." 

v <- ggplot(SF.bath, aes(lon, lat,z=z))
print(v + geom_raster(aes(fill=z)) + stat_contour(size=0.2) + scale_fill_gradient("z"))

Not sure where I need to specify value_var and how??
Thank you!



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


Ben Bond-Lamberty

unread,
Apr 23, 2012, 9:28:56 PM4/23/12
to ggplot2
>v <- ggplot(SF.bath, aes(lon, lat,z=z))
>print(v + geom_raster(aes(fill=z)) + stat_contour(size=0.2) + scale_fill_gradient("z"))

Hmm, this example also works perfectly for me. It looks like the error
is coming from the reshape2 package...my guess would be that there are
multiple values for some x-y, and that's screwing things up. Try first
summarizing your data frame so that there's only one unique z for each
x/y combinatione.g.:

SF.bath.ag <- aggregate(z ~ long+lat, data=SF.bath,FUN=mean)

...and then plot that.

Ben

R version 2.14.1 (2011-12-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] ggplot2_0.9.0

loaded via a namespace (and not attached):
 [1] colorspace_1.1-0   dichromat_1.2-4    digest_0.5.1
grid_2.14.1        MASS_7.3-16        memoise_0.1        munsell_0.3
 [8] plyr_1.6           proto_0.3-9.2      RColorBrewer_1.0-5
reshape2_1.2.1     scales_0.2.0       stringr_0.6

Aurelie Cosandey Godin

unread,
Apr 23, 2012, 10:15:42 PM4/23/12
to Ben Bond-Lamberty, ggplot2
Thanks again Ben,

This is weird... I've tried your suggestion but I still get the same result.
I've also looked at the following example and get the same problem: "Using alpha as value column: use value_var to override."

library("reshape2")
volcano3d <- melt(volcano)
names(volcano3d) <- c("lon", "lat", "z")
v <- ggplot(volcano3d, aes(lon, lat,z=z))


print(v + geom_raster(aes(fill=z)) + stat_contour(size=0.2) + scale_fill_gradient("z"))

I still get this white plot with my contour line... :(

-Aurelie

Sam Albers

unread,
Apr 23, 2012, 10:30:09 PM4/23/12
to Aurelie Cosandey Godin, Ben Bond-Lamberty, ggplot2
Very weird. For what it is worth I also do not receive the same error
message that Aurelle is getting. Could it be that all your package
aren't up to date? Have you posted your session info yet? Here's mine
which is similiar (except I am using linux) to Ben's.

Sam

> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_CA.UTF-8 LC_COLLATE=en_CA.UTF-8
[5] LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_CA.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] ggplot2_0.9.0 reshape2_1.2.1

loaded via a namespace (and not attached):

[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 grid_2.14.1
[5] MASS_7.3-17 memoise_0.1 munsell_0.3 plyr_1.7.1
[9] proto_0.3-9.2 RColorBrewer_1.0-5 scales_0.2.0 stringr _0.6

Aurelie Cosandey Godin

unread,
Apr 23, 2012, 10:38:47 PM4/23/12
to Sam Albers, Ben Bond-Lamberty, ggplot2
Heres' my sessionInfo()
Looks like some of my packages aren't up-to-date... I'll see if that fix anything.
-Aurelie

> sessionInfo()
R version 2.14.2 (2012-02-29)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C/en_US.UTF-8/C/C/C/C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] reshape2_1.1 ggplot2_0.9.0

loaded via a namespace (and not attached):

[1] MASS_7.3-17 RColorBrewer_1.0-5 colorspace_1.1-0
[4] dichromat_1.2-4 digest_0.5.1 grid_2.14.2
[7] memoise_0.1 munsell_0.3 plyr_1.6
[10] proto_0.3-9.2 scales_0.2.0 stringr_0.5
[13] tools_2.14.2

Aurelie Cosandey Godin

unread,
Apr 23, 2012, 11:03:09 PM4/23/12
to Sam Albers, Ben Bond-Lamberty, ggplot2
Yeaah! Thank you very much Sam & Ben!
Turns out that it was an easy problem to fix! Must of been my reshape package that wasn't up-to-date! grrrr
Cheers,
-Aurelie
Reply all
Reply to author
Forward
0 new messages