by Ryan Garner
Senior Data Scientist, Revolution Analytics
I love creating spatial data visualizations in R. With the ggmap package, I can easily download satellite imagery which serves as a base layer for the data I want to represent. In the code below, I show you how to visualize sampled soil attributes among 16 different rice fields in Uruguay.
library(ggmap) library(plyr) library(gridExtra) temp <- tempfile() download.file("http://www.plantsciences.ucdavis.edu/plant/data.zip", temp) connection <- unz(temp, "Data/Set3/Set3data.csv") rice <- read.csv(connection) names(rice) <- tolower(names(rice)) # Create a custom soil attribute plot # @param df Data frame containing data for a field # @param attribute Soil attribute # @return Custom soil attribute plot create_plot <- function(df, attribute) { map <- get_map(location = c(median(df$longitude), median(df$latitude)), maptype = "satellite", source = "google", crop = FALSE, zoom = 15) plot <- ggmap(map) + geom_point(aes_string(x = "longitude", y = "latitude", color = attribute), size = 5, data = df) plot <- plot + ggtitle(paste("Farmer", df$farmer, "/ Field", df$field)) plot <- plot + scale_color_gradient(low = "darkorange", high = "darkorchid4") return(plot) } ph_plot <- dlply(rice, "field", create_plot, attribute = "ph") ph_plots <- do.call(arrangeGrob, ph_plot)
First, I download data that is used in "Spatial Data Analysis in Ecology and Agriculture using R" by Dr. Richard Plant. (This is an excellent book to get your feet wet working with spatial data in R.) After the data has been downloaded, I create a function that builds a custom soil attribute plot for each unique field found in the rice yield data. Then, I customized the output to include larger spatial points and a custom gradient that goes from dark orange to dark purple for clarity.
Finally, once all the plots are generated, I arrange them into a single plot.
The plot shows the ph intensity of the soil in 16 fields belonging to 9 different farmers. The second to the last plot, field 15 of farmer L, appears to have higher ph concentrations than the rest.
Please ignore my previous question. I was missing "RgoogleMaps" and "mapproj" packages
Posted by: Ima | January 06, 2015 at 22:29
Thanks for this post - this is really interesting stuff!
But I think your conlusion ("appears to have higher ph concentrations than the rest.") is wrong.
Field 14 has much higher values - each plot has its own color range.
Another thing I noticed: for Fields 1:4, 6 and 16, the layout of point matches the field borders in sattelite images well. What about all the other plots, where it seems like the sat images are from the wrong location?
Posted by: Berry Boessenkool | January 07, 2015 at 23:55
Excellent observations Berry! You are correct -- field 14 along with field 15 have higher ph values than field 16.
Your other observation is interesting too. By visualizing this dataset, we not only see how the range of ph values vary from field to field, but the location of the sample points seem to fall into a field boundary for some fields and appear somewhat random on other fields.
Posted by: Ryan Garner | January 10, 2015 at 11:32
Nice example. Wish the help page for scale_color_gradient was better. Doesn't explain the 'distiller' function at all and the examples could be a lot better (e.g. avoid gray points on gray background).
Posted by: Kevin Wright | January 11, 2015 at 19:44