Plot points into DEM: "Error: Discrete value supplied to continuous scale"

821 views
Skip to first unread message

Diego

unread,
Dec 5, 2012, 5:19:08 PM12/5/12
to ggp...@googlegroups.com
Hi! I'm trying to plot some points into DEM with ggplot2 but I have the error : "Error: Discrete value supplied to continuous scale". I attach the shapefiles. This the code.

library("raster")

library("plyr")
library("rgdal") 
library("maptools")
library("ggplot2")
library("rgeos")
gpclibPermit()

Cuenca_Anya <- readOGR(dsn="C:/Users/Diego/Desktop/Afiche_Data/Shapefiles", layer="Cuenca_Anya")
Cuenca_Anya@data$id= rownames(Cuenca_Anya@data)
Cuenca_Anya.points = fortify(Cuenca_Anya, region="id")
Cuenca_Anya.df=join(Cuenca_Anya.points,Cuenca_Anya@data,by="id")

DEM_Anya <- readGDAL("C:/Users/Diego/Desktop/Afiche_Data/Shapefiles/dem_anya.tif") 
projection(DEM_Anya) <- projection(Cuenca_Anya)
DEM_Anya <- data.frame(DEM_Anya)
colnames(DEM_Anya) <- c("Altitud","long","lat")

Estaciones_Anya <- read.table("C:/Users/Diego/Desktop/Afiche_Data/Shapefiles/Estaciones_Anya.txt",header=TRUE)
Estaciones_Anya = fortify(Estaciones_Anya, region="id")
names(Estaciones_Anya)[which(names(Estaciones_Anya) == 'Longitud')] <- 'long' 
names(Estaciones_Anya)[which(names(Estaciones_Anya) == 'Latitud')] <- 'lat'

qw <- ggplot(data=DEM_Anya) + geom_tile(aes(long, lat, fill=Altitud)) + coord_equal()
qw <- qw + geom_point(data=Estaciones_Anya, aes(x=long,y=lat,fill=Estacion,group=NULL),size=4,shape=21,colour="black",size=3)
qw

I could not find in the web some link with the solution (and I don't know how to transform the points to continuous scale that I think is the solution, or something like that). Thanks.

Regards,

Diego

New WinRAR ZIP archive.zip

Ben Bond-Lamberty

unread,
Dec 7, 2012, 2:03:53 PM12/7/12
to ggp...@googlegroups.com
Diego,

I didn't run your code, but am pretty sure the problem is that you're
already defined a continuous fill (Altitud) in the geom_tile call, and
then are trying to have a separate discrete fill (Estacion) in
geom_point. This can't be done as far as I know, see e.g.:
https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/lDvsd4yJ0AE

What about just setting color to Estacion in geom_point? (And
obviously removing the color="black" setting.) Would this give the
effect you want? Otherwise, perhaps re-think this plot and what you're
trying to show.

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

Diego

unread,
Dec 7, 2012, 6:32:34 PM12/7/12
to ggp...@googlegroups.com
Thank you very much, I changed that part of the code like that:
qw <- qw + geom_point(data=Estaciones_Anya, aes(x=long,y=lat,group=NULL),colour=c("red","orange","yellow"),size=4,size=3) 
qw <- qw + geom_point(data=Estaciones_Anya, aes(x=long,y=lat,group=NULL),colour=c("black","black","black"),size=4,size=3,shape=1) 

But I do not know how to add the legend with the names of  "Estaciones". How can I do that?

Thanks!

Diego

Ben Bond-Lamberty

unread,
Dec 7, 2012, 7:59:47 PM12/7/12
to ggp...@googlegroups.com
I was thinking for doing something more like:

qw <- qw + geom_point(data=Estaciones_Anya,
> aes(x=long,y=lat,color=Estacion),size=4,size=3,shape=1)

That will give you a legend automatically.

Ben

Diego

unread,
Dec 8, 2012, 12:17:14 AM12/8/12
to ggp...@googlegroups.com
Thank you very much Ben!

Regards,

Diego 
Reply all
Reply to author
Forward
0 new messages