[R] another question on shapefiles and geom_point in ggplot2

Manuel Spínola mspinola10 at gmail.com
Sun Apr 3 22:51:02 CEST 2011


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 <mailto:manuel at data$id> = rownames(manuel at data 
> <mailto: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
>>>>>>                         <https://sites.google.com/site/lobitoderio/>
>>>>>>                         Institutional website: ICOMVIS
>>>>>>                         <http://www.icomvis.una.ac.cr/>
>>>>>>                         -- 
>>>>>>                         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
>>>>>                     <https://sites.google.com/site/lobitoderio/>
>>>>>                     Institutional website: ICOMVIS
>>>>>                     <http://www.icomvis.una.ac.cr/>
>>>>>
>>>>
>>>>
>>>>                 -- 
>>>>                 *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
>>>>                 <https://sites.google.com/site/lobitoderio/>
>>>>                 Institutional website: ICOMVIS
>>>>                 <http://www.icomvis.una.ac.cr/>
>>>>
>>>
>>>
>>>             -- 
>>>             *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
>>>             <https://sites.google.com/site/lobitoderio/>
>>>             Institutional website: ICOMVIS
>>>             <http://www.icomvis.una.ac.cr/>
>>>
>>
>>
>>         -- 
>>         *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
>>         <https://sites.google.com/site/lobitoderio/>
>>         Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
>>
>
>
>     -- 
>     *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
>     <https://sites.google.com/site/lobitoderio/>
>     Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
>


-- 
*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 
<https://sites.google.com/site/lobitoderio/>
Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: riqueza.png
Type: image/png
Size: 14825 bytes
Desc: riqueza.png
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110403/105d1def/attachment.png>


More information about the R-help mailing list