marinebon / infographiqR

R functions for creation of interactive intelligent infographics, ie infographiq
https://marinebon.github.io/infographiqR
MIT License
11 stars 2 forks source link

support multiple plot types? #21

Closed 7yl4r closed 6 years ago

7yl4r commented 7 years ago

include plotting_function_call column in indicators.csv:

plot_series(title="my title")

raw_html_passthrough or img_wrapper could be functions that could be called too.

7yl4r commented 7 years ago

will need to modify modal_plot template

7yl4r commented 7 years ago

after further discussion with Ben, I believe we do want to expand support for multiple different ways of generating plots and we feel that the best way is to embed R code directly in the csv column. Default behavior will be to use plot_timeseries, so new users can leave the plot_function_call column empty, advanced users can use this column to override the plot_timeseries call.

reikopm commented 6 years ago

That would work great for me. Jen Brown (MBNMS) wanted a taxonomic structure plot which I implemented in R using a bar graph on seasonal averages over 16 taxonomic groups.

This is what the code looks like:

urlstr='https://raw.githubusercontent.com/reikopm/info-mb/master/prep/data/phytoplankton/efm_taxa_structure_means.csv' dat <- read.csv(url(urlstr))

library(plotly)

t <- list( family = "helvetica", size = 24, color = 'black')

p1 <- plot_ly(dat, x = ~Season, y = ~asyn, type = 'bar', name = 'Synechococcus',width=2040, height=1080) %>% add_trace(y = ~arfp, name = 'Red Fluorescing Picoplankton') %>% add_trace(y = ~aprym, name = 'Haptophyte') %>% add_trace(y = ~aaflag, name = 'Autotrophic Flagellate') %>% add_trace(y = ~aadino, name = 'Autotrophic Dinoflagellate') %>% add_trace(y = ~acryp, name = 'Cryptomonad') %>% add_trace(y = ~aprasy, name = 'Prasinophyte') %>% add_trace(y = ~aphaeo, name = 'Phaeocystsis') %>% add_trace(y = ~apen, name = 'Pennate Diatom') %>% add_trace(y = ~acen, name = 'Centric Diatom') %>% add_trace(y = ~ahdino, name = 'Heterotrophic Dinoflagellate') %>% add_trace(y = ~ahflag, name = 'Heterotrophic Flagellate') %>% add_trace(y = ~achoano, name = 'Choanoflagellate') %>% add_trace(y = ~ahcryp, name = 'Leucocryptos') %>% add_trace(y = ~ahcil, name = 'Heterotrophic Ciliate') %>% add_trace(y = ~aacil, name = 'Autotrophic Ciliate') %>% layout(font=t,yaxis = list(title = 'mg C m-3'),barmode = 'stack') %>% layout(font=t,title = "Q10:Taxonomic Structure", margin = 1)

bbest commented 6 years ago

This looks great @reikopm !

image

I'll try to work this into the framework as a slightly more generic function. Along with seascape plot and multiple time series (eg in/out MPAs) a la:

image

Side note:

reikopm commented 6 years ago

I just noticed the seasons in the taxonomic bar plot are not ordered. Added this code: dat$Season <- factor(dat$Season, levels = c("Spring", "Summer", "Fall", "Winter"))

mb_seasonal_taxonomic_structure2

7yl4r commented 6 years ago

It looks like we need this functionality to show the seascapes streamgraphs as well.

@bbest : do you have a specific plan for this or would you like me to take a swing at it?

bbest commented 6 years ago

Hey @reikopm,

Sorry, been focused on the Explorer app. Am discussing this now with @7yl4r and we're considering putting whole functions in the csv, which could then be evaluated. For instance:

txt = "urlstr='https://raw.githubusercontent.com/reikopm/info-mb/master/prep/data/phytoplankton/efm_taxa_structure_means.csv'
dat <- read.csv(url(urlstr))

library(plotly)

t <- list(
family = 'helvetica',
size = 24,
color = 'black')

p1 <- plot_ly(dat, x = ~Season, y = ~asyn, type = 'bar', name = 'Synechococcus',width=2040, height=1080) %>%
add_trace(y = ~arfp, name = 'Red Fluorescing Picoplankton') %>%
add_trace(y = ~aprym, name = 'Haptophyte') %>%
add_trace(y = ~aaflag, name = 'Autotrophic Flagellate') %>%
add_trace(y = ~aadino, name = 'Autotrophic Dinoflagellate') %>%
add_trace(y = ~acryp, name = 'Cryptomonad') %>%
add_trace(y = ~aprasy, name = 'Prasinophyte') %>%
add_trace(y = ~aphaeo, name = 'Phaeocystsis') %>%
add_trace(y = ~apen, name = 'Pennate Diatom') %>%
add_trace(y = ~acen, name = 'Centric Diatom') %>%
add_trace(y = ~ahdino, name = 'Heterotrophic Dinoflagellate') %>%
add_trace(y = ~ahflag, name = 'Heterotrophic Flagellate') %>%
add_trace(y = ~achoano, name = 'Choanoflagellate') %>%
add_trace(y = ~ahcryp, name = 'Leucocryptos') %>%
add_trace(y = ~ahcil, name = 'Heterotrophic Ciliate') %>%
add_trace(y = ~aacil, name = 'Autotrophic Ciliate') %>%
layout(font=t,yaxis = list(title = 'mg C m-3'),barmode = 'stack') %>%
layout(font=t,title = 'Q10:Taxonomic Structure', margin = 1)
print(p1)"

eval(parse(text=txt))

Similarly you could include static images that have a URL like so:

txt = "knitr::include_graphics('./fish_diversity.png')"

eval(parse(text=txt))

Or leaflet marker cluster:

txt = "library(tidyverse)
library(leaflet)

leaflet(quakes) %>% 
  addTiles() %>% 
  addMarkers(
    clusterOptions = markerClusterOptions())"

eval(parse(text=txt))

I am admittedly nervous about any major architectural changes like this though before we do live demos. @7yl4r is going to play with this in a sandbox (ie github branch) to not affect anything else.

7yl4r commented 6 years ago

planned options for new column plot_function in plot_indicators.csv:

  1. leave blank to default to plot_timerseries
  2. put your R code directly in the cell. used for short calls to plotting functions. Possible future functions:
    1. png insert plot function using knitter::include_graphics
    2. html insert plot function
    3. streamgraph plotter for eDNA data (above)
    4. maps using leaflet cluster markers or ddgridR
    5. no_plot function for #30 (edit: nevermind, there is a better way to fix that)
  3. file path to R script that creates plot (for longer custom plotters)
7yl4r commented 6 years ago

I have implemented 1, 2.i, 2.ii, 2.v, and 3 above; opened new issues for 2.iii (#36) and 2.iv (#35) since they are a bit bigger.

Initially I kept these commits on the multiplots branch, but I tested building info-demo, info-fk, and info-mb and ran into no issues so I have merged the changes into master.

For now the best place to look to figure out how to use all this functionality is at the marinebon/info-demo readme - specifically the lobster section.