plotly: ¿How to make graphics for each department / state?

#Data download: https://wetransfer.com/downloads/4fbc963a827c38ce5c809d4cd0fbd32a20190812215454/fd9861

library(haven)
enaho <- as.data.frame(read_dta("H:/Clases para Diana/bases/ingresos&gastos.dta"))
#visualizar la base de datos
View(enaho)


###estadisticas descriptivas
#install.packages("pastecs")
library(pastecs)
stat.desc(enaho)
stat.desc(enaho[,c("Mieperho","Ipcr_0","Ipcr_1")])

stat.desc(enaho[,c("Mieperho","Ipcr_0","Ipcr_1")],
          basic=TRUE, desc=TRUE, norm=TRUE, p=0.95)
stat.desc(enaho[4:8], basic=TRUE, desc=TRUE,
          norm=TRUE, p=0.95)


#Grafico enaho
library(plotly)
#https://plot.ly/r/font/
t <- list(
  family = "sans serif",
  size = 14,
  color = '#777777')
#https://plot.ly/r/axes/
ay <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE
)

#keep if dpto==1
#https://blog.exploratory.io/filter-data-with-dplyr-76cf5f1a258e
#enaho <-filter(enaho,dpto==1)

grafico2 = plot_ly(data=enaho ,x =~año , y=~Ipcr_0,
                   marker = list(color = '#E21B24'), type="bar",
                   transforms = list(list( 
                     type = 'filter',
                     target = ~dpto,
                     operation = '=',
                     value = unique(enaho$dpto)[1]))) %>%
  layout(title='<b>INGRESO REAL PROMEDIO PER CÁPITA MENSUAL</b><br><i> (Soles constantes base=2018 a precios de Lima Metropolitana)<br>',font=t,
         xaxis = list(title = " "),
         yaxis = ay,
         updatemenus = list(
           list(
             type = 'dropdown',
             active = 0,
             buttons = apply(as.data.frame(unique(enaho$dpto_abrv)), 1, 
                             function(x) list(method = 'restyle',args = list('transforms[1].value',x),label = x)))))
grafico2

#Atte: Andrés Talavera Cuya
#Saludos desde Lima, Perú

Greetings fellow Peruvian, welcome to the community!

You are just making a little mistake, you have to match the target in transforms with the values for updatemenus, so far you are mixin dpto with dpto_abrv, if you choose just one it will work, see this example

plot_ly(data=enaho ,x =~año , y=~Ipcr_0,
                   marker = list(color = '#E21B24'), type="bar",
                   transforms = list(list( 
                       type = 'filter',
                       target = ~dpto_abrv,
                       operation = '=',
                       value = unique(enaho$dpto_abrv)[1]))) %>%
    layout(title='<b>INGRESO REAL PROMEDIO PER CÁPITA MENSUAL</b><br><i> (Soles constantes base=2018 a precios de Lima Metropolitana)<br>',font=t,
           xaxis = list(title = " "),
           yaxis = ay,
           updatemenus = list(
               list(
                   type = 'dropdown',
                   active = 0,
                   buttons = apply(as.data.frame(unique(enaho$dpto_abrv)), 1, 
                                   function(x) list(method = 'restyle',args = list('transforms[0].value',x),label = x)))))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.