create a boxplot with facet_grid

Hi,

I have diferent imputation methods and I want to use function facet grid to plot this methods by regions. I know my problem is in x axis, but I don't know how to use imputation methods (var1,var2,var3,...) from reactive function in x.

dados7 <- reactive({ 
  dataset() %>% filter(variable==input$frame) %>% 
    rename(var1 = Inicial, var2 = CasosCompletos, var3 = ImpMedia, var4 = ImpMediana, var5 = ImpLOCF, var6 = ImpNOCB, var7 = ImpMultipla)
})
globaldata$regiao2 <- globaldata$regiao

levels(globaldata$regiao2) <- c("África S.","América N. e C.","América S.","Asia Ori/Pacifico","Europa","Medio Oriente/África N.")
renderPlotly({
title3<-paste(input$frame, "por região")

p <-dados7() %>% ggplot(aes(x =~variable,fill=globaldata$regiao2))+
  geom_boxplot()+
  facet_grid(.~globaldata$regiao2)+
  theme(axis.text.x=element_text(angle=-90, vjust=0.4,hjust=1))

fig <- ggplotly(p)
fig
})

Thanks!

What does dataset() look like? Can you post the top few rows using dput(head(dataset))?

Why are renaming all those columns? You probably want to use dplyr::pivot_longer to out the method in a single column and the results in another.

In ggplot you don't use ~ in x = variable. Also you can't use another dataframe (globaldata) in the aes or in facet_grid. You need to put this information into dados7().

dataset() looks like this. I have a column for each method and the last one is the variable imputed.

If I use function dplyr::pivot_longer I don't need to rename all methods? Should I use it inside reactive function?

If your data doesn't change, you can do pivot_longer in the global section before ui, do it once is faster.

Why didn't do you dput(head(dataset)) like I said? It's easier and more useful than pasting a picture.

Maybe you should make the plot first before you try to put it in shiny. Shiny is quite hard to learn and debug.

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