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

From: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
Date: Sun, 03 Apr 2011 12:41:01 -0700

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 
	[[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 - 20:24:31 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 Sun 03 Apr 2011 - 22:20:28 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