Re: [R] another question on shapefiles and geom_point in ggplot2

From: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
Date: Sun, 03 Apr 2011 15:37:22 -0700

Manuel:
As far as I know one needs gpclibPermit() in order to fortify see this:
Note: polygon geometry computations in maptools

        depend on the package gpclib, which has a
        restricted licence. It is disabled by default;
        to enable gpclib, type gpclibPermit() 
I am going to guess that ahmadou dicko doesn't show gpclibPermit() on his code because he loaded it with Rprofile or some other way. I tried to run his code without
gpclibPermit() and it wouldn't let me fortify, so not sure how he did it.

On the same note, your code didn't work when you were trying to join ai_biotica and ai_biotica_at_data
because you are trying to join polygons and points (just a guess). Try to use fortify instead of fortify.SpatialPolygonsDataFrame. I tested with a different shapefile and it works for me.

manuel_at_data$id = rownames(manuel_at_data)
   manuel_fort <- fortify(manuel,region="id")   manuel_merged <- join(manuel_fort,manuel_at_data, by ="id")

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
http://www.fws.gov/redbluff/rbdd_jsmp.aspx

>
>From: Manuel Spínola <mspinola10@gmail.com>
>To: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
>Cc: r-help_at_stat.math.ethz.ch; ggplot2_at_googlegroups.com
>Sent: Sun, April 3, 2011 1:51:02 PM
>Subject: Re: another question on shapefiles and geom_point in ggplot2
>
>Thank you very much Felipe,
>
>Did you see the solution from ahmadou dicko?
>He doesn´t use gpclibPermit()
>
>I have another option but I cannot get the right fill for the id.
>
>See attached map.
>
>ai_biotica = readOGR(dsn="C:/ProyectosRespacial/ICE/SIG_Biotica_PHED",
>layer="AI_BIOTICA_010411_CRTM05")
>str(ai_biotica)
>
># fortify to get the data
>
>fortify.ai_biotica <-
>fortify.SpatialPolygonsDataFrame(ai_biotica,region='Area_Influ')
>names(fortify.ai_biotica)
>str(fortify.ai_biotica)
>levels(fortify.ai_biotica$group)
>
># mapa
>
>ggplot(fortify.ai_biotica, aes(x = long, y=lat, group =  group)) +
>geom_polygon(colour = "black", fill = NA)
>
>
>geo = read.csv("riqueza_out.csv", sep = ",", header = T)
>names(geo)
>str(geo)
>summary(geo)
>
># mapa con riqueza
>
>p = ggplot(geo, aes(x, y))
>p + geom_point(aes(size = ACE, colour = ACE)) + theme_bw() + scale_size(name =
>"Número de especies", breaks = c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)) +
>scale_colour_gradientn(name = 'Número de especies', colours = heat.colors(10),
>breaks = c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+ xlab("Longitud") +
>ylab("Latitud") + opts(axis.text.x = theme_text(size = 8, vjust = 1)) +
>opts(axis.text.y = theme_text(size = 8, hjust = 1)) +
>geom_path(aes(x=long,y=lat,group=group, fill=id),data=fortify.ai_biotica)
>
>Best,
>
>Manuel
>
>On 03/04/2011 01:41 p.m., Felipe Carrillo wrote:
>Manuel:
>>I changed your variable names from x to 'long' and y to 'lat' on the
>>riqueza_out.csv file.
>>The code below should do what you want. Also, since the legend title is kind of
>>long, I broke it
>>down into three lines so you can see more plot area. I am cc'ing the other
>>groups so more people
>>use it if needed.
>>
>>library(rgdal)
>>library(ggplot2)
>>library(sp)
>>library(maptools)
>>gpclibPermit()
>>manuel <- readOGR(dsn=".", layer="AI_BIOTICA_010411_CRTM05")
>>names(manuel);dim(manuel)
>>  slotNames(manuel) # look at the slot names
>># add the 'id' variable to the shapefile and use it to merge both files
>>  manuel_at_data$id = rownames(manuel_at_data)
>># convert shapefile to dataframe
>>  manuel.df <- as.data.frame(manuel)
>># fortify to plot with ggplot2
>>  manuel_fort <- fortify(manuel,region="id")
>>  head(manuel_fort)
>># Merge shapefile and the as.dataframe shapefile
>>  manuel_merged <- join(manuel_fort,manuel.df, by ="id")
>>  head(manuel_merged)
>># Read in the csv file
>>manuel_points <- read.csv("riqueza_out.csv")
>>  head(manuel_points);dim(manuel_points)
>># fortify this one too for the points or else an error will ocurr
>> manuel_points <- fortify(manuel_points)
>> manuel_points
>> 
>># Plot the shapefile and overlayed the points over it
>>p <- ggplot(manuel_merged, aes(long,lat,group=group)) +
>>  geom_polygon(aes(data=manuel_merged,fill=Area_Influ)) +
>>  geom_path(color="white") + theme_bw() # remove this if you don't want black
>>and white background
>>  p + geom_point(data=manuel_points,aes(size=ACE,colour=ACE,group=NULL)) +
>>  scale_size(name = "Número\nde\nespecies", breaks = c(2, 4, 6, 8, 10, 12, 14,
>>16, 18, 20)) +
>> scale_colour_gradientn(name = 'Número\nde\nespecies',
>> colours = rainbow(6), breaks = c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+
>> xlab("Longitud") + ylab("Latitud") + opts(axis.text.x = theme_text(size = 8,
>>vjust = 1)) +
>>
>> opts(axis.text.y = theme_text(size = 8, hjust = 1))
>>  
>>  
>> Felipe D. Carrillo
>>Supervisory Fishery Biologist
>>Department of the Interior
>>US Fish & Wildlife Service
>>California, USA
>>http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>>
>>
>>
>>
>>>
>>>From: Manuel Spínola <mspinola10_at_gmail.com>
>>>To: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
>>>Sent: Sat, April 2, 2011 11:22:24 PM
>>>Subject: Re: another question on shapefiles and geom_point in ggplot2
>>>
>>>No problem, thank you very much Felipe.
>>>
>>>Best,
>>>
>>>Manuel
>>>
>>>On 03/04/2011 12:19 a.m., Felipe Carrillo wrote:
>>>I meant to send you this one..Let me clean up the code a little bit and
>>>>I will send it to you,,,do you mind if I send it to you in the morning?
>>>> 
>>>>Felipe D. Carrillo
>>>>Supervisory Fishery Biologist
>>>>Department of the Interior
>>>>US Fish & Wildlife Service
>>>>California, USA
>>>>http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>>>>
>>>>
>>>>
>>>>
>>>>>
>>>>>From: Manuel Spínola <mspinola10_at_gmail.com>
>>>>>To: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
>>>>>Sent: Sat, April 2, 2011 11:15:28 PM
>>>>>Subject: Re: another question on shapefiles and geom_point in ggplot2
>>>>>
>>>>>Yes Felipe.  That is the graph I was looking for.
>>>>>
>>>>>I got something closer but no like yours.  How did you do it?
>>>>>
>>>>>Manuel
>>>>>
>>>>>On 03/04/2011 12:10 a.m., Felipe Carrillo wrote:
>>>>>I was able to open them,,I am attaching a picture of the graph I created..It's
>>>>>that what
>>>>>>you had in mind?
>>>>>> 
>>>>>>Felipe D. Carrillo
>>>>>>Supervisory Fishery Biologist
>>>>>>Department of the Interior
>>>>>>US Fish & Wildlife Service
>>>>>>California, USA
>>>>>>http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>>
>>>>>>>From: Manuel Spínola <mspinola10_at_gmail.com>
>>>>>>>To: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
>>>>>>>Sent: Sat, April 2, 2011 10:35:51 PM
>>>>>>>Subject: Re: another question on shapefiles and geom_point in ggplot2
>>>>>>>
>>>>>>>It should be.  I am sending them again.
>>>>>>>
>>>>>>>Manuel
>>>>>>>
>>>>>>>On 02/04/2011 10:23 p.m., Felipe Carrillo wrote:
>>>>>>>Manuel:
>>>>>>>>I can't open the shapefile, is this the original one?
>>>>>>>>Is the csv file the one that you are trying to overlay on top of the
>>>>>>shapefile?
>>>>>>>> 
>>>>>>>>Felipe D. Carrillo
>>>>>>>>Supervisory Fishery Biologist
>>>>>>>>Department of the Interior
>>>>>>>>US Fish & Wildlife Service
>>>>>>>>California, USA
>>>>>>>>http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>>
>>>>>>>>>From: Manuel Spínola <mspinola10_at_gmail.com>
>>>>>>>>>To: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
>>>>>>>>>Sent: Sat, April 2, 2011 6:14:09 PM
>>>>>>>>>Subject: Re: another question on shapefiles and geom_point in ggplot2
>>>>>>>>>
>>>>>>>>>Files attached.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>On 02/04/2011 07:04 p.m., Felipe Carrillo wrote:
>>>>>>>>>If you want individual points overlayed on the shapefile, you need to add
>>>>>>>>>another variable to it before you fortify it.
>>>>>>>>>>After you fortify merge both the fortified dataset and the original shapefile.
>>>>>>>>>>Go ahead and post your shapefile to see if
>>>>>>>>>>I can figure it out. Do you just want the points or want text also?
>>>>>>>>>> 
>>>>>>>>>>Felipe D. Carrillo
>>>>>>>>>>Supervisory Fishery Biologist
>>>>>>>>>>Department of the Interior
>>>>>>>>>>US Fish & Wildlife Service
>>>>>>>>>>California, USA
>>>>>>>>>>http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>From: Manuel Spínola <mspinola10_at_gmail.com>
>>>>>>>>>>>To: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
>>>>>>>>>>>Sent: Sat, April 2, 2011 5:24:02 PM
>>>>>>>>>>>Subject: Re: another question on shapefiles and geom_point in ggplot2
>>>>>>>>>>>
>>>>>>>>>>>Hi Felipe,
>>>>>>>>>>>
>>>>>>>>>>>I did the same thing that I am trying know, attached is how it looks.
>>>>>>>>>>>
>>>>>>>>>>>Best,
>>>>>>>>>>>
>>>>>>>>>>>Manuel
>>>>>>>>>>>
>>>>>>>>>>>On 02/04/2011 06:09 p.m., Felipe Carrillo wrote:
>>>>>>>>>>>Manuel:
>>>>>>>>>>>>I did something similar a few weeks ago,,If you post your shapefile and describe
>>>>>>>>>>>>what you are
>>>>>>>>>>>>expecting I might be able to help..
>>>>>>>>>>>> 
>>>>>>>>>>>>Felipe D. Carrillo
>>>>>>>>>>>>Supervisory Fishery Biologist
>>>>>>>>>>>>Department of the Interior
>>>>>>>>>>>>US Fish & Wildlife Service
>>>>>>>>>>>>California, USA
>>>>>>>>>>>>http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>From: Manuel Spínola <mspinola10_at_gmail.com>
>>>>>>>>>>>>>To: "ggplot2_at_googlegroups.com" <ggplot2_at_googlegroups.com>
>>>>>>>>>>>>>Sent: Sat, April 2, 2011 12:01:56 PM
>>>>>>>>>>>>>Subject: another question on shapefiles and geom_point in ggplot2
>>>>>>>>>>>>>
>>>>>>>>>>>>>Dear list members,
>>>>>>>>>>>>>
>>>>>>>>>>>>>This is a different question from my previous post.
>>>>>>>>>>>>>I handle to read my shapefile with ggplot2 following
>>>>>>>>>>>>>https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles
>>>>>>>>>>>>>
>>>>>>>>>>>>>p = ggplot(ai_biotica.df, aes(long,lat,group=group,fill=Area_Influ)) +
>>>
>>>>>>>>>>>>>  geom_polygon() +
>>>>>>>>>>>>>  geom_path(color="white") +
>>>>>>>>>>>>>  coord_equal()
>>>>>>>>>>>>>
>>>>>>>>>>>>>I got a nice map.
>>>>>>>>>>>>>
>>>>>>>>>>>>>Now I want to plot some point with geom_point but I got an error.
>>>>>>>>>>>>>
>>>>>>>>>>>>>> p + geom_point(geo, aes(size = ACE, colour = ACE)) + scale_size(name = "Número
>>>>>>>>>>>>>>de especies", breaks = c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)) +
>>>>>>>>>>>>>>scale_colour_gradientn(name = 'Número de especies', colours = rainbow(6), breaks
>>>>>>>>>>>>>>= c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+ xlab("Longitud") + ylab("Latitud") +
>>>>>>>>>>>>>>opts(axis.text.x = theme_text(size = 8, vjust = 1)) + opts(axis.text.y =
>>>>>>>>>>>>>>theme_text(size = 8, hjust = 1))
>>>>>>>>>>>>>>
>>>>>>>>>>>>>Error: ggplot2 doesn't know how to deal with data of class uneval
>>>>>>>>>>>>>
>>>>>>>>>>>>>Best,
>>>>>>>>>>>>>
>>>>>>>>>>>>>Manuel
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>--
>>>>>>>>>>>>>Manuel Spínola, Ph.D.
>>>>>>>>>>>>>Instituto Internacional en Conservación y Manejo de Vida Silvestre
>>>>>>>>>>>>>Universidad Nacional
>>>>>>>>>>>>>Apartado 1350-3000
>>>>>>>>>>>>>Heredia
>>>>>>>>>>>>>COSTA RICA
>>>>>>>>>>>>>mspinola_at_una.ac.cr
>>>>>>>>>>>>>mspinola10_at_gmail.com
>>>>>>>>>>>>>Teléfono: (506) 2277-3598
>>>>>>>>>>>>>Fax: (506) 2237-7036
>>>>>>>>>>>>>Personal website: Lobito de río
>>>>>>>>>>>>>Institutional website: ICOMVIS --
>>>>>>>>>>>>>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 ggplot2_at_googlegroups.com
>>>>>>>>>>>>>To unsubscribe: email ggplot2+unsubscribe_at_googlegroups.com
>>>>>>>>>>>>>More options: http://groups.google.com/group/ggplot2
>>>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>--
>>>>>>>>>>Manuel Spínola, Ph.D.
>>>>>>>>>>Instituto Internacional en Conservación y Manejo de Vida Silvestre
>>>>>>>>>>Universidad Nacional
>>>>>>>>>>Apartado 1350-3000
>>>>>>>>>>Heredia
>>>>>>>>>>COSTA RICA
>>>>>>>>>>mspinola_at_una.ac.cr
>>>>>>>>>>mspinola10_at_gmail.com
>>>>>>>>>>Teléfono: (506) 2277-3598
>>>>>>>>>>Fax: (506) 2237-7036
>>>>>>>>>>Personal website: Lobito de río
>>>>>>>>>>Institutional website: ICOMVIS
>>>>>>>>
>>>>>>>>
>>>>>>>>--
>>>>>>>>Manuel Spínola, Ph.D.
>>>>>>>>Instituto Internacional en Conservación y Manejo de Vida Silvestre
>>>>>>>>Universidad Nacional
>>>>>>>>Apartado 1350-3000
>>>>>>>>Heredia
>>>>>>>>COSTA RICA
>>>>>>>>mspinola_at_una.ac.cr
>>>>>>>>mspinola10_at_gmail.com
>>>>>>>>Teléfono: (506) 2277-3598
>>>>>>>>Fax: (506) 2237-7036
>>>>>>>>Personal website: Lobito de río
>>>>>>>>Institutional website: ICOMVIS
>>>>>>
>>>>>>
>>>>>>--
>>>>>>Manuel Spínola, Ph.D.
>>>>>>Instituto Internacional en Conservación y Manejo de Vida Silvestre
>>>>>>Universidad Nacional
>>>>>>Apartado 1350-3000
>>>>>>Heredia
>>>>>>COSTA RICA
>>>>>>mspinola_at_una.ac.cr
>>>>>>mspinola10_at_gmail.com
>>>>>>Teléfono: (506) 2277-3598
>>>>>>Fax: (506) 2237-7036
>>>>>>Personal website: Lobito de río
>>>>>>Institutional website: ICOMVIS
>>>>
>>>>
>>>>--
>>>>Manuel Spínola, Ph.D.
>>>>Instituto Internacional en Conservación y Manejo de Vida Silvestre
>>>>Universidad Nacional
>>>>Apartado 1350-3000
>>>>Heredia
>>>>COSTA RICA
>>>>mspinola_at_una.ac.cr
>>>>mspinola10_at_gmail.com
>>>>Teléfono: (506) 2277-3598
>>>>Fax: (506) 2237-7036
>>>>Personal website: Lobito de río
>>>>Institutional website: ICOMVIS
>>
>>
>>--
>>Manuel Spínola, Ph.D.
>>Instituto Internacional en Conservación y Manejo de Vida Silvestre
>>Universidad Nacional
>>Apartado 1350-3000
>>Heredia
>>COSTA RICA
>>mspinola_at_una.ac.cr
>>mspinola10_at_gmail.com
>>Teléfono: (506) 2277-3598
>>Fax: (506) 2237-7036
>>Personal website: Lobito de río
>>Institutional website: ICOMVIS

-- 
Manuel Spínola, Ph.D. 
Instituto Internacional en Conservación y Manejo de Vida Silvestre 
Universidad Nacional 
Apartado 1350-3000 
Heredia 
COSTA RICA 
mspinola_at_una.ac.cr 
mspinola10_at_gmail.com 
Teléfono: (506) 2277-3598 
Fax: (506) 2237-7036 
Personal website: Lobito de río 
Institutional website: ICOMVIS -- 
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 ggplot2_at_googlegroups.com
To unsubscribe: email ggplot2+unsubscribe_at_googlegroups.com
More options: http://groups.google.com/group/ggplot2

	[[alternative HTML version deleted]]


______________________________________________ R-help_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

Received on Sun 03 Apr 2011 - 22:40:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 04:50:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive