1
votes

I am working on RMarkdown report with Shiny elements, using ggplot2 and transforming its charts, using ggplotly. In a regular RMarkdown report everything works perfect. ggplotly does not allow you to put a horizontal legend in the bottom, but I still managed to do it, using this answer.

The plot now looks great

enter image description here

The code for this plot is

semiformal_savings_chart <- ggplot(semiformal_savings, aes(x = Country, y = 
Percent, fill = Category)) + 
geom_bar(stat = 'identity', width = 0.7) +      
theme_tufte() +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
ylim(0, 80) + 
theme(legend.text = element_text(size = 12)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 12)) +
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm")) 

ggplotly(semiformal_savings_chart) %>% config(displayModeBar = F)  %>%
layout(legend = list(orientation = "h", x = 0.4, y = -0.2))

When I put it into Shiny format, I am forced to drop the last two lines of code with ggplotly, which disables the Plotly panel. Just connecting it to ggplot with pipe does not work and I get an error.

Error: no applicable method for 'layout' applied to an object of class "c('theme', 'gg')"

Reactivity works perfect but in Shiny my now my plot has the Plotly panel + legend in on a right, which I do not like.

enter image description here

The Shiny code looks like this

semiformal_savings_data <- reactive({
filter(semiformal_savings,
       Country %in% input$Country)

selectInput(inputId = "Country", label = "Please select a 
country/countries", choices = unique(semiformal_savings$Country), 
            selected = unique(semiformal_savings$Country), multiple = TRUE)
plotlyOutput("semiformalsavingsPlot")

output$semiformalsavingsPlot <- renderPlotly({
ggplot(semiformal_savings_data(), aes(x = Country, y = Percent, fill = 
Category)) + geom_bar(stat = 'identity', width = 0.7) +      
theme_tufte() +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
ylim(0, 80) + 
theme(legend.text = element_text(size = 12)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 12)) +
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm")) 
})

But this part is missing now

%>% config(displayModeBar = F) %>% layout(legend = list(orientation = "h", 
x = 0.4, y = -0.2))

How to attach this functionality to my almost perfect plot?

Thanks!

1
Please provide the data to reproduce these plots.Sada93

1 Answers

3
votes

Resolved personally

The changes were minor, I still used ggplotly()

g1 <- ggplot(ownership_data(), aes(x = as.character(Year), y = Percent, fill = 
Category)) + 
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Country) +
theme_tufte() +
ylim(0, 100) +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(legend.text = element_text(size = 13)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 13)) +
theme(strip.text.x = element_text(size = 12)) +
theme(legend.position = "bottom") +
theme(legend.title = element_blank()) 

return(ggplotly(g1, tooltip = c("y", "text")) %>% config(displayModeBar = F)  %>%
layout(legend = list(orientation = "h", x = 0.4, y = -0.2)))

enter image description here