-
-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
convert static plotting from base r to ggplot2 #133
Comments
Here is a ggplot lesson BUT it's using the tidy() function to go from sp -> a df. i'm not sure actually how to cast from sf @jsta or @chris-prener do either of you guys know how far off what i wrote is from what we will want to teach sf -> ggplot friendly df. https://earthdatascience.org/courses/earth-analytics/spatial-data-r/make-maps-with-ggplot-in-R/ |
Hi Erin and Leah,
Are we using the ggmap package for static maps?
…On Thu, Apr 5, 2018 at 9:45 PM, Leah Wasser ***@***.***> wrote:
Here is a ggplot lesson BUT it's using the tidy() function to go from sp
-> a df. i'm not sure actually how to cast from sf @jsta
<https://github.com/jsta> or @chris-prener
<https://github.com/chris-prener> do either of you guys know how far off
what i wrote is from what we will want to teach sf -> ggplot friendly df.
https://earthdatascience.org/courses/earth-analytics/
spatial-data-r/make-maps-with-ggplot-in-R/
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#133 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AH1wUQ0tEzIzBQavVHjJ1Fa8oE86EHi3ks5tlkM3gaJpZM4TIvW9>
.
--
Janani Selvaraj
Research Scholar
Department of Environmental Management
Bharathidasan University
Tiruchirapalli
|
here is my 2 cents. ggmap is super cool but sometimes it doesn't load properly on people's machines. sometimes you need the dev version. SO... i think it's fine to show people ggmap but i wouldn't count on it working 100% of the time for workshops so i'd keep it optional / as a demo. i'd love to hear other people's experiences tho. i normally show students ggmap and maps just in case. |
I had an experience showing static maps using ggmap for survey data
coordinates ... Since they wanted static maps with the with the google maps
background... But as u said I could not proceed with that later because i
got some errors
…On Thu, Apr 5, 2018 at 10:26 PM, Leah Wasser ***@***.***> wrote:
here is my 2 cents. ggmap is super cool but sometimes it doesn't load
properly on people's machines. sometimes you need the dev version. SO... i
think it's fine to show people ggmap but i wouldn't count on it working
100% of the time for workshops so i'd keep it optional / as a demo. i'd
love to hear other people's experiences tho. i normally show students ggmap
and maps just in case.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#133 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AH1wUa0opowM-EF2lHyrT9a3tsyqSVT7ks5tlky6gaJpZM4TIvW9>
.
--
Janani Selvaraj
Research Scholar
Department of Environmental Management
Bharathidasan University
Tiruchirapalli
|
yes...it's really nice when it works. there are just install issues on some machines. I suggest it is an added component at the end of the plotting lesson - that can be demoed IF the instructor chooses to do so and with the risk that it won't work on some computers. :) |
Just wanted to know how far have you explored packages like highcharts and
plotly for interactive maps... And also how good is it to show packages
like shiny for interactivity in a workshop
…On Thu, Apr 5, 2018 at 10:32 PM, Leah Wasser ***@***.***> wrote:
yes...it's really nice when it works. there are just install issues on
some machines. I suggest it is an added component at the end of the
plotting lesson - that can be demoed IF the instructor chooses to do so and
with the risk that it won't work on some computers. :)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#133 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AH1wURu3RQoxCh9aJ3smBbas-G7bvpDaks5tlk48gaJpZM4TIvW9>
.
--
Janani Selvaraj
Research Scholar
Department of Environmental Management
Bharathidasan University
Tiruchirapalli
|
We will be using leaflet for interactive maps. This particular task is specific to static plots! @tyson-swetnam is working on a leaflet lesson for interactive maps. Plotly is great - but i think it's better for interactive plots vs actual maps. Please focus on the basics of static plotting for this lesson but keep all ideas on the table (we just won't address them all now so we can get the basics covered first!) |
Ok :)
…On Thu, Apr 5, 2018 at 11:40 PM, Leah Wasser ***@***.***> wrote:
We will be using leaflet for interactive maps. This particular task is
specific to static plots! @tyson-swetnam
<https://github.com/tyson-swetnam> is working on a leaflet lesson for
interactive maps. Plotly is great - but i think it's better for interactive
plots vs actual maps. Please focus on the basics of static plotting for
this lesson but keep all ideas on the table (we just won't address them all
now so we can get the basics covered first!)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#133 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AH1wUdhjZ8YaZiUijjIADeqM3ixEPu5bks5tll4NgaJpZM4TIvW9>
.
--
Janani Selvaraj
Research Scholar
Department of Environmental Management
Bharathidasan University
Tiruchirapalli
|
so appreciative of your work on this and the thoughts @janu123 thank you. :) |
Sorry if I was exaggerating ... I was just curious:)
…On Thu, Apr 5, 2018 at 11:43 PM, Leah Wasser ***@***.***> wrote:
so appreciative of your work on this and the thoughts @janu123
<https://github.com/janu123> thank you. :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#133 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AH1wURGQS_Q0J23GFvf-16RUkK0i2mjWks5tll6_gaJpZM4TIvW9>
.
--
Janani Selvaraj
Research Scholar
Department of Environmental Management
Bharathidasan University
Tiruchirapalli
|
Thanks @janu123 and @lwasser for the conversation above. I'm elaborating a bit on the CAC discussion to see if I can help clarify the goals here. The CAC had an in-depth discussion about the various options for both static and interactive plotting of geospatial data. The options that were considered for static plotting were base R / ggplot / and tmap. For ggplot, there was a detailed conversation about using the stable version of ggplot (which doesn't currently support geospatial data objects) or the development version (which does, but is often difficult to install and has some serious bugs with geom_sf). There is more detail about the CAC's discussions of these issues The decision at this point was to use the stable version of ggplot (on CRAN) for static plotting, and to convert from an sf object to a df. I don't know the specifics of sf object formatting, so don't know how this works, but @jsta and @chris-prener I believe have both taught this way. Could one of you please clarify what the function is to do this conversion? |
@ErinBecker I think the easiest way is |
I am very familiar using My guess is we need to have a three-step process: |
@juanfung that approach does make a lot of sense given an sf obj is essentially already a df. or can someone else try it? |
@lwasser The result is an object of class |
I think that will get very messy with polygon and line objects right? My feeling is we want to avoid manual tidying of complex coordinates in sf list columns. |
totally. i agree we want to avoid manual stuff. i was thinking it would create a nice clean geojson col like geopandas :) but it sounds like that may not be the case! gosh i wish geom_sf was just on the ggplot cran. maybe the workflow about is best then @jsta ? @juanfung we do want to plot and keep attributes for colors. |
@jsta Yes you are absolutely correct! That approach won't work very well for non-points objects. What you suggested earlier ( Is it absolutely necessary that learners use Note that it fails for some |
hey @lwasser just getting caught up on this. I've tested this a little with the polygon data built into library(broom)
library(ggplot2)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(sp)
nc_sf <- st_read(system.file("shape/nc.shp", package="sf"), stringsAsFactors = FALSE)
#> Reading layer `nc' from data source `/Users/chris/Library/R/3.4/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
nc_sp <- as(nc_sf, "Spatial")
nc_df <- tidy(nc_sp)
#> Regions defined for each Polygons
ggplot() +
geom_polygon(data = nc_df, aes(x = long, y = lat, group = group)) +
coord_equal() I agree this is super clunky, and I wish |
wrapper packages like
but at the expense of making it clear how spatial data is translated into a plottable form. I think that's an important concept to grasp. |
hey @obrl-soil I was thinking of clunky more from a teaching perspective - my workflow above uses three different data frame types that would all have to be introduced. |
This is great discussion @chris-prener @obrl-soil !! Here is my 2 cents :) with the hopes in 6 months it's no longer in dev! i think that might be a good approach for the time being if you guys agree too!! i think its valuable for students to understand ggplot plays nicely with df's in general! :) |
Hi @lwasser @chris-prener @obrl-soil it would be great to come up with a one example that many of us agree with so that we can post that here as an example and then create a todo list of the other graphs to update. We can then hopefully use some contributors from the bug BBQ to help us go through and make the updates. I'm not a ggplot / mapping guru otherwise i would propose one myself ;) But if I get a sense of what we want I'd be happy to use my European time zone difference (advantage?) to get started on making some changes tomorrow morning or pick up from where we get to over the next 12 hours |
@lachlandeer THANK YOU for helping us all get organized on this!! And for helping improve the lessons!! I think the example that @chris-prener provided earlier could be good:
in this example you are plotting polygons |
@lwasser - yes that looks like something pretty understandable! Do you know if we can do all if this in a pipe? i.e would the following (plus modifications most likely) work?
|
try it! it could work just fine @lachlandeer i haven't tested it but know people pipe sf objects a lot! i think it would be good to show the non pipes syntax in a breakout just in case it's cognative overload for students to absorb pipes... @chris-prener what has been your experience with pipes AND spatial data? i've not used them with spatial stuff but my students LOVE pipes once they understand it. |
I agree about the non-pipes syntax option. |
Yes - both are defs useful in the R-intro lesson as it stands I have left the dplyr stuff and there are some examples at the end (that I want to simplify by removing facet wraps) that do the "data-manipulation piped into ggplot" style so that learners have a reference to look at with a "simpler" rectangular dataset |
Hi all, just wanted to add my two cents. It seems to me (not a geospatial person), that the "basic" workflow we're looking at here is
Since the second step is turning out to be more complex than we perhaps at first expected, my proposal is to make the dataframe outside of the lesson and provide it to the learners (i.e. not require the learners to understand the code that generated the dataframe). This would enable the learners to focus on learning plotting rather than data restructuring. Is this something that would be possible? We've done a similar thing in the Genomics lesson where we created a data structure "behind the scenes" and presented it to the learners as a done deal. |
hey @ErinBecker !! this is a good thought... but i don't think it's an ideal approach.
so you can use the tidy function which allows for ggplot plotting. This is the crux of spatial data in R and it's important for them to understand that ggplot doesn't natively plot spatial data. you need to do something i think it's a good idea to explain what's happening here. i don't suggest altering the format to a df which is not a standard thing a user will encounter. the real goal of these lessons is to support users working with real data! |
It seems most of the discussion of moving from base to ggplot was based on sf objects. For rasters, another advantage of using base plot (or specifically, the 'plot' method overloaded by the raster package) is that the whole data doesn't need to fit in R memory. |
ok got yah @lwasser - I've never used base for plotting spatial data so that is where the real limit is! It seems like, based on what you and @pmarchand1 have said, that one option is to teach |
so after beating my head against the wall if we do the hillshade with
which is slowly moving towards how the version from baseplot looked, and probbaly will be better with a finer grid on the grey hillside Should I pursue this further? |
Hey, I don't know if this is solves the issue, but the library(ggplot2)
library(data.table)
library(metR)
ggplot(melt(volcano), aes(Var1, Var2)) +
geom_relief(aes(z = value)) +
geom_tile(aes(fill = value), alpha = 0.5) +
scale_fill_viridis_c() |
A more general answer is to use library(data.table)
library(ggplot2)
data(volcano)
volcano <- as.data.table(melt(volcano, varnames = c("x", "y"),
value.name = "h"))
First, you can make one plot with it's own gradient. (shade <- ggplot(volcano, aes(x, y)) +
geom_raster(aes(fill = h), alpha = 0.5, interpolate = TRUE) +
scale_fill_gradient(low = "black", high = "white")) Then, you take the important part of the plot and add it to a second plot. grob.shade <- ggplotGrob(shade)
grob.shade <- grob.shade$grobs[[6]]$children[[3]]
ggplot(volcano, aes(x, y)) +
geom_raster(aes(fill = h), alpha = 0.5, interpolate = TRUE) +
annotation_custom(grob = grob.shade) +
scale_fill_gradient(low = "blue", high = "red") +
coord_fixed() +
theme_void() Of course, this example is meaningless because is the same data both times. The tricky part is the |
The problem when using |
There is an |
you annotation raster and the RStoolbox is the only way i've gotten this to work as well @paleolimbot it avoids the issue with needing 2 diffferent colormaps which ggmap doesn't natively support from what i can tell. |
I should mention that both need a 3 or 4 band raster, RGBA |
@datacarpentry/geospatial-maintainers - Hi all, as we get closer to our target publication date (mid-July), I'm wondering how we can best move forward on the rest of the base plot --> ggplot conversions. @lachlandeer, @jsta, and @justinmillar did a great job on converting the first four episodes, but we have episodes 5 - 14 that still need converting. I'm unable to tackle this myself, as I'm not familiar with plotting geospatial data, but I'm happy to help coordinate efforts, resolve merge conflicts, and offer any pedagogical guidance that folks request. If you're able to help by converting one or two of the remaining episodes, could you please edit the list below to add your name to the episodes you would like to tackle? Many thanks!
@lwasser @jsta @tyson-swetnam @obrl-soil @janu123 @lachlandeer @chris-prener @juanfung @r4space |
@ErinBecker I can work on episodes 9 and 10. Just to clarify, we are using ggplot with sf? |
@juanfung thanks! I'll put your name down on the list for those episodes. Yes, we are using ggplot with sf. The new release of ggplot2 on CRAN includes sf support, so learners will no longer need to install the development version of ggplot2!! |
Closes with #228 |
Closed with Addressed with #228 |
According to the CAC conversation:
For this we will be reading the data in as an sf object, recasting it to a dataframe, and using the stable ggplot on CRAN.
See up to date list of which episodes remain to be converted at the end of this thread.
The text was updated successfully, but these errors were encountered: