data-lessons / NEON-R-Make-Pretty-Maps-Plots

Making pretty maps reposit
6 stars 3 forks source link

Create a map with a legend (categorical legend) in R #3

Open lwasser opened 8 years ago

lwasser commented 8 years ago

Hey @bbest @megwilliams @kwiter -- did any of you work on creating a map (or have you done this) where you have a shapefile (eg the roads_streams lines shapefile in our data ), and you create a map with the lines colored by category. IN essence "TYPE" or an attribute becomes a factor. We have the following below which seems a bit cumbersome but works. We are using the roads_streams shapefile.

I am not sure how to add a legend or to make a nice categorical plot. Is there a simple way to do and teach this?

Thanks for any ideas!

#color lines by TYPE attribute
type <- slot(object = lines, name = "data")$TYPE
#you can also write
type <- lines@data$TYPE

col <- rep(x= "black", length = length(type))
col[type == "footpath"] <- "red"

plot(x=lines, col=col,add=T)

Here is the latest version of the lesson that i'm working on

https://github.com/data-lessons/NEON-R-Spatial-Vector/blob/gh-pages/00-open-a-shapefile.Rmd

megwilliams commented 8 years ago

Hello : ) This is set to be the second map in the Vis: Section 1. I’m playing/fussing with this today. It IS tedious – I’ll pipe up when I find something definitively useful and simplified.

From: Leah Wasser [mailto:notifications@github.com] Sent: Tuesday, October 27, 2015 11:10 AM To: data-lessons/NEON-R-Make-Pretty-Maps-Plots NEON-R-Make-Pretty-Maps-Plots@noreply.github.com Cc: Williams, Meghan WilliamsMe@si.edu Subject: [NEON-R-Make-Pretty-Maps-Plots] Create a map with a legend (categorical legend) in R (#3)

Hey @bbesthttps://github.com/bbest @megwilliamshttps://github.com/megwilliams @kwiterhttps://github.com/kwiter -- did any of you work on creating a map (or have you done this) where you have a shapefile (eg the roads_streams lines shapefile in our data ), and you create a map with the lines colored by category. IN essence "TYPE" or an attribute becomes a factor. We have the following below which seems a bit cumbersome but works. We are using the roads_streams shapefile.

I am not sure how to add a legend or to make a nice categorical plot. Is there a simple way to do and teach this?

Thanks for any ideas!

color lines by TYPE attribute

type <- slot(object = lines, name = "data")$TYPE

you can also write

type <- lines@data$TYPE

col <- rep(x= "black", length = length(type))

col[type == "footpath"] <- "red"

plot(x=lines, col=col,add=T)

— Reply to this email directly or view it on GitHubhttps://github.com/data-lessons/NEON-R-Make-Pretty-Maps-Plots/issues/3.

lwasser commented 8 years ago

Thank you Meg! Are you thinking ggplot for the methods? It seems a lot of folks in the blogs are going that route. But it still is a bit confusing :)

Let me know if i can be helpful in any way :)

Leah

Leah A. Wasser, Ph.D. Supervising Scientist Education / Data Products National Ecological Observatory Network (NEON)

Boulder, Colorado

For free tutorials and resources on working with spatio-temporal data: http://www.neondataskills.orghttp://www.neondataskills.org/

From: megwilliams notifications@github.com<mailto:notifications@github.com> Reply-To: data-lessons/NEON-R-Make-Pretty-Maps-Plots reply@reply.github.com<mailto:reply@reply.github.com> Date: Tuesday, October 27, 2015 at 9:51 AM To: data-lessons/NEON-R-Make-Pretty-Maps-Plots NEON-R-Make-Pretty-Maps-Plots@noreply.github.com<mailto:NEON-R-Make-Pretty-Maps-Plots@noreply.github.com> Cc: Leah Wasser lwasser@neoninc.org<mailto:lwasser@neoninc.org> Subject: Re: [NEON-R-Make-Pretty-Maps-Plots] Create a map with a legend (categorical legend) in R (#3)

Hello : ) This is set to be the second map in the Vis: Section 1. I'm playing/fussing with this today. It IS tedious - I'll pipe up when I find something definitively useful and simplified.

From: Leah Wasser [mailto:notifications@github.com] Sent: Tuesday, October 27, 2015 11:10 AM To: data-lessons/NEON-R-Make-Pretty-Maps-Plots NEON-R-Make-Pretty-Maps-Plots@noreply.github.com<mailto:NEON-R-Make-Pretty-Maps-Plots@noreply.github.com> Cc: Williams, Meghan WilliamsMe@si.edu<mailto:WilliamsMe@si.edu> Subject: [NEON-R-Make-Pretty-Maps-Plots] Create a map with a legend (categorical legend) in R (#3)

Hey @bbesthttps://github.com/bbest @megwilliamshttps://github.com/megwilliams @kwiterhttps://github.com/kwiter -- did any of you work on creating a map (or have you done this) where you have a shapefile (eg the roads_streams lines shapefile in our data ), and you create a map with the lines colored by category. IN essence "TYPE" or an attribute becomes a factor. We have the following below which seems a bit cumbersome but works. We are using the roads_streams shapefile.

I am not sure how to add a legend or to make a nice categorical plot. Is there a simple way to do and teach this?

Thanks for any ideas!

color lines by TYPE attribute

type <- slot(object = lines, name = "data")$TYPE

you can also write

type <- lines@data$TYPE

col <- rep(x= "black", length = length(type))

col[type == "footpath"] <- "red"

plot(x=lines, col=col,add=T)

Reply to this email directly or view it on GitHubhttps://github.com/data-lessons/NEON-R-Make-Pretty-Maps-Plots/issues/3.

Reply to this email directly or view it on GitHubhttps://github.com/data-lessons/NEON-R-Make-Pretty-Maps-Plots/issues/3#issuecomment-151547856.

megwilliams commented 8 years ago

We were debating ggplot vs base R. I had started with ggplot (very easy for polys), then thought about converting code to base to make it easier to incorporate rasters later. I’m on the fence (leaning in the slightest towards ggplot’s geom_path and liking this guy’s tutorial www.kevjohnson.org/making-maps-in-r/)

If you have a preference for the overall lesson, I’m very happy go that way. Right now, I’m getting out the products I want (that’d normally be a snap in ArcMap etc.,) and then I’ll translate to whichever libraries we decide on. Again, I’m happy to learn both, so it doesn’t matter to me in end. Also fav so far below for mapping in Base R and others! https://rstudio-pubs-static.s3.amazonaws.com/79029_b56eaffe36ef44f29b8efc0a07d67208.html

From: Leah Wasser [mailto:notifications@github.com] Sent: Tuesday, October 27, 2015 1:17 PM To: data-lessons/NEON-R-Make-Pretty-Maps-Plots NEON-R-Make-Pretty-Maps-Plots@noreply.github.com Cc: Williams, Meghan WilliamsMe@si.edu Subject: Re: [NEON-R-Make-Pretty-Maps-Plots] Create a map with a legend (categorical legend) in R (#3)

Thank you Meg! Are you thinking ggplot for the methods? It seems a lot of folks in the blogs are going that route. But it still is a bit confusing :)

Let me know if i can be helpful in any way :)

Leah

Leah A. Wasser, Ph.D. Supervising Scientist Education / Data Products National Ecological Observatory Network (NEON)

Boulder, Colorado

For free tutorials and resources on working with spatio-temporal data: http://www.neondataskills.orghttp://www.neondataskills.org/http://www.neondataskills.org%3chttp:/www.neondataskills.org/

From: megwilliams notifications@github.com<mailto:notifications@github.com<mailto:notifications@github.com%3cmailto:notifications@github.com>> Reply-To: data-lessons/NEON-R-Make-Pretty-Maps-Plots reply@reply.github.com<mailto:reply@reply.github.com<mailto:reply@reply.github.com%3cmailto:reply@reply.github.com>> Date: Tuesday, October 27, 2015 at 9:51 AM To: data-lessons/NEON-R-Make-Pretty-Maps-Plots NEON-R-Make-Pretty-Maps-Plots@noreply.github.com<mailto:NEON-R-Make-Pretty-Maps-Plots@noreply.github.com<mailto:NEON-R-Make-Pretty-Maps-Plots@noreply.github.com%3cmailto:NEON-R-Make-Pretty-Maps-Plots@noreply.github.com>> Cc: Leah Wasser lwasser@neoninc.org<mailto:lwasser@neoninc.org<mailto:lwasser@neoninc.org%3cmailto:lwasser@neoninc.org>> Subject: Re: [NEON-R-Make-Pretty-Maps-Plots] Create a map with a legend (categorical legend) in R (#3)

Hello : ) This is set to be the second map in the Vis: Section 1. I'm playing/fussing with this today. It IS tedious - I'll pipe up when I find something definitively useful and simplified.

From: Leah Wasser [mailto:notifications@github.com] Sent: Tuesday, October 27, 2015 11:10 AM To: data-lessons/NEON-R-Make-Pretty-Maps-Plots NEON-R-Make-Pretty-Maps-Plots@noreply.github.com<mailto:NEON-R-Make-Pretty-Maps-Plots@noreply.github.com<mailto:NEON-R-Make-Pretty-Maps-Plots@noreply.github.com%3cmailto:NEON-R-Make-Pretty-Maps-Plots@noreply.github.com>> Cc: Williams, Meghan WilliamsMe@si.edu<mailto:WilliamsMe@si.edu<mailto:WilliamsMe@si.edu%3cmailto:WilliamsMe@si.edu>> Subject: [NEON-R-Make-Pretty-Maps-Plots] Create a map with a legend (categorical legend) in R (#3)

Hey @bbesthttps://github.com/bbest @megwilliamshttps://github.com/megwilliams @kwiterhttps://github.com/kwiter -- did any of you work on creating a map (or have you done this) where you have a shapefile (eg the roads_streams lines shapefile in our data ), and you create a map with the lines colored by category. IN essence "TYPE" or an attribute becomes a factor. We have the following below which seems a bit cumbersome but works. We are using the roads_streams shapefile.

I am not sure how to add a legend or to make a nice categorical plot. Is there a simple way to do and teach this?

Thanks for any ideas!

color lines by TYPE attribute

type <- slot(object = lines, name = "data")$TYPE

you can also write

type <- lines@data$TYPE

col <- rep(x= "black", length = length(type))

col[type == "footpath"] <- "red"

plot(x=lines, col=col,add=T)

Reply to this email directly or view it on GitHubhttps://github.com/data-lessons/NEON-R-Make-Pretty-Maps-Plots/issues/3.

Reply to this email directly or view it on GitHubhttps://github.com/data-lessons/NEON-R-Make-Pretty-Maps-Plots/issues/3#issuecomment-151547856.

— Reply to this email directly or view it on GitHubhttps://github.com/data-lessons/NEON-R-Make-Pretty-Maps-Plots/issues/3#issuecomment-151574649.

bbest commented 8 years ago

Good discussion here @megwilliams and @lwasser! I like the possibility of someday showing this vector plotting functionality for both base and ggplot2 packages. I'm gonna mostly be offline the next week but look forward to revisiting this later with y'all.

lwasser commented 8 years ago

Hi There! This is a great discussion. Thank you for taking the time to think about this Meg and Ben! :) I like that kevjohnson tutorial as well! The other one i like: http://zevross.com/blog/2014/07/16/mapping-in-r-using-the-ggplot2-package/

And there was one by good old Hadley W as well that was good but used the fortify component which i still don’t completely understand.

It seems to me that we might need to use ggplot as it seems to handle projection fairly well.

I could imagine introducing basic concepts in base R (which we do in the vector lessons) and then having a full tutorial on ggplot maps. UNLESS someone things it’s equally efficient in base r? Looping in @jduckles , @KristinaRiemer , @brymz , @jhollist (Raster group) for additional thoughts!

leah :)

brymz commented 8 years ago

If you are going to be using ggplot for the 'Pretty Maps', I would recommend going all the way with ggplot or vice versa with base graphics. The programming strategies behind ggplot and graphics are different enough that it would be challenging to introduce both well in a reasonable amount of time. That said, the Raster group uses the raster::plot() and hist() functions to visualize the data. We did this mostly to follow the previous tutorials and because we didn't know of an intuitive cognate in ggplot. If one exists we could easily integrate it into our module.

lwasser commented 8 years ago

Thanks guys - just to throw this into the mix, DC has a ggplot lesson BUT i've never taught it and can't speak to zack's concerns about it in a 2 day workshop context.

NOTE: it does have a time series element BUT nothing associated with spatial data. But as @brymz mentioned, complexity is an absolute concern. Really appreciate the wonderful input from everyone!

http://www.datacarpentry.org/R-ecology/05-visualisation-ggplot2.html