Creating plots with Plotly Package and Rmd

Part of Course: Developing Data Products (Coursera - Johns Hopkins)

In this example, we created with Plotly package. Also, I used dplyr for cleaning the data and widgetframe for rendering in blogdown correctly.

This plot shows historical water allocations for AGRICULTURE according to REPDA by CONAGUA in Yucatán Península, México.

First step: Download and data cleaning

library(plotly)
library(dplyr)
library(widgetframe)
url <-"http://201.116.60.46/DatosAbiertos/Otorgamiento_de_Concesiones.zip"

temp <- tempfile()
temp2 <- tempfile()
download.file(url, temp, mode="wb")
unzip(zipfile = temp, exdir = temp2)
data <- read.csv(file = paste0(temp2,"\\CONCESIONES","\\ANEXOS_SUBTERRANEOS.csv",sep=""),encoding = "UTF-8")
data1 <- read.csv(file = paste0(temp2,"\\CONCESIONES","\\CONCESIONES.csv",sep=""),encoding = "UTF-8")
data_all <- merge(x = data,y= data1, by = "X.U.FEFF.TITULO", all.x = TRUE)
unlink(c(temp, temp2))
#head(data_all)
df1 <- filter(data_all, NOMBRE.DE.ESTADO %in% c("CAMPECHE","QUINTANA ROO","YUCATAN"),USO.QUE.AMPARA.EL.TITULO %in% c("AGRICOLA")) #filter by State and coordinates
df1 <- data.frame(uso=c(df1$USO.QUE.AMPARA.EL.TITULO),vol=df1$VOLUMEN.ANUAL.EN.m3,date=df1$FECHA.DE.REGISTRO)

Second step: Generate plot with ploty

volume is in m3

df2 <- df1 %>%
    group_by(date,uso) %>%
    summarize(volume_m3=sum(vol))

my_plot <- plot_ly(df2, x = ~date, y =~volume_m3, type = "scatter",mode='lines+markers')

my_plot <- my_plot %>%
    layout(title = "Time Series water concession in Yucatan Peninsula",
           xaxis = list(type = 'date',tickformat = "%B %Y")

        )

my_plot
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
Edgar Rodríguez-Huerta
Edgar Rodríguez-Huerta
PhD in Sustainability Science

Researcher exploring Sustainability to raise resilience in communities

Related