Dear List

I want to present a scatter plot (on time) of soil sample data for each block in a tree crop, using r. I have a csv file with the value of the selected attribute, the block name, the x and y, and the dates for 9 locations, making 34 observations in total. I wish to present it as a leafpop::popupGraph on a mapview map. The user will be click on the point(a block centroid) within the block and see the history/plot for that location only.

The leafpop package manual provides the svg example using the meuse data set. Using it as the basis, I have a map with the points, and the popup. I have not been able to supply only the plot for the specific block to each point instance within it. My plot is a faceted ggplot with one facet for each block. Onclick on the map, each of my 9 locations shows the same full faceted plot (all 9). I want just the plot for that location.

The meuse example shows rep iteration through one plot, changing the colour of the value in that plot to highlight data point for the geographic location selected. While elegant, it is no guide to achieving my target.

Tim Salabim kindly pointed me a step forward suggesting lapply, but I do have not yet the depth of skill to successfully implement it. Below is an extract of the code, to show the steps I have taken so far.

I am not succeeding at getting the plot images. It also appears that the process loops through all 34 observations to make plots for each.

I am hoping the members can guide me a few steps further forward.

Here is the code:
block.graph <- function(select_organic_matter_by_blockname_with_xy_range, na.rm = TRUE){
    
     # create list of blocks in data to loop over
     block_list <- unique(som_xy$block_name)
    
     # create for loop to produce ggplot2 graphs
     for (i in seq_along(block_list)) {
        
         # create plot for each block in the dataframe
         soil_om <-
             ggplot(subset(som_xy, som_xy$block_name==block_list[i]),
                    aes(x= date, y = organic_matter)) +
             theme_light() +
             geom_point() +
             ylim(0, 10) +
             geom_hline(data = "" linetype="dotted", colour="#ff710c",alpha = 0.5, aes(yintercept = range_min)) +
             geom_hline(data = "" linetype="dotted", colour="#ff710c",alpha = 0.5, aes(yintercept = range_max))
         p <- (soil_om +
                   theme(
                       axis.text.x=element_text(angle=90,hjust=1, vjust=1),
                       axis.title.x = element_blank(),
                       axis.title.y = element_text(angle=90, hjust=0.5),
                       legend.title = element_blank(),
                       plot.title = element_text(hjust = 0.5)
                   )+
                   ggtitle("Organic Matter Time Series")+
                   ylab("%"))
     }}
 individ_plot <- lapply(som_xy$block_name, block.graph)
 
 mapview(collins_farm,
         data = "">          zcol = "block_name",
         popup= leafpop::popupImage(individ_plot))

Thanks in advance
David

Land and Water Management logo

David Hine
Land and Water Management PL
Level 7, 127 Creek St
Brisbane, Qld 4000
Australia

m: 0429 886 146 +61 429 886 146
t: (07) 4015 3470 +61 7 4015 3470

GeoPortal with example presentations of spatial data for horticulture users and others.
Land and Water Management PL


_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to