I have a donut chart that I want to make interactive in my R Shiny app using plotly.
When it is rendered as a plot, everything is fine:
Using ggplot--looks right, but not interactive
library(shiny)
library(dplyr)
library(ggplot2)
library(Cairo)
library(ggrepel)
library(plotly)
options(shiny.usecairo=T) #used to get the plot to look crisp in Shiny
my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
fluidRow(
plotOutput("count_by_person")
))
))
server <- function(input, output) {
output$count_by_person <- renderPlot({
data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
n = c(10, 30, 59, 1),
prop = c(10, 30, 59, 1)) %>%
dplyr::arrange(desc(name)) %>%
dplyr::mutate(text_y = cumsum(prop)-prop/2)
ggplot(data, aes(x = 2, y = prop, fill = name)) +
geom_bar(stat = "identity", color = "white") +
coord_polar(theta = "y", start = 0)+
geom_label_repel(aes(y = text_y, label = paste0(n, "\n", prop, "%")), force_pull = 100, nudge_x = 1) +
scale_fill_manual(values = my_colors) +
theme_void() +
xlim(.5, 2.5)
})
}
shinyApp(ui, server)
But, when I try to use plotly, the format gets messed up in an incredible way:
Using ggplotly--interactive, but looks unexplainably wrong
library(shiny)
library(dplyr)
library(ggplot2)
library(Cairo)
library(ggrepel)
library(plotly)
options(shiny.usecairo=T) #used to get the plot to look crisp in Shiny
my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
fluidRow(
plotlyOutput("count_by_person")
))
))
server <- function(input, output) {
output$count_by_person <- renderPlotly({
data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
n = c(10, 30, 59, 1),
prop = c(10, 30, 59, 1)) %>%
dplyr::arrange(desc(name)) %>%
dplyr::mutate(text_y = cumsum(prop)-prop/2)
plot <- ggplot(data, aes(x = 2, y = prop, fill = name)) +
geom_bar(stat = "identity", color = "white") +
coord_polar(theta = "y", start = 0)+
geom_label_repel(aes(y = text_y, label = paste0(n, "\n", prop, "%")), force_pull = 100, nudge_x = 1) +
scale_fill_manual(values = my_colors) +
theme_void() +
xlim(.5, 2.5)
ggplotly(plot)
})
}
shinyApp(ui, server)
How do I get my donut plot to still look how it does in the first example but be interactive like the second one?
You can use the ggiraph package with ggplot
library(shiny)
library(dplyr)
library(ggplot2)
library(Cairo)
library(ggiraph) #Need to intall this package
options(shiny.usecairo=T) #used to get the plot to look crisp in Shiny
my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
fluidRow(
ggiraphOutput("count_by_person") #need to update from plotOutput
))
))
server <- function(input, output) {
output$count_by_person <- renderggiraph({ #need to update from renderPlot
data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
n = c(10, 30, 59, 1),
prop = c(10, 30, 59, 1)) %>%
dplyr::arrange(desc(name)) %>%
dplyr::mutate(text_y = cumsum(prop)-prop/2)
p <- ggplot() + #assign as object in environment
geom_bar_interactive(data = data, stat = "identity", color = "white", aes(x = 2, y = prop, fill = name, tooltip = paste0(n, "\n", prop, "%"))) + #Add tooltip to aes()
coord_polar(theta = "y", start = 0)+
scale_fill_manual(values = my_colors) +
theme_void() +
xlim(.5, 2.5)
ggiraph(code = print(p)) #call object p
})
}
shinyApp(ui, server)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.