0
votes

I am having trouble trying create the same ggplot pie chart in plotly, as I need plotly's interactive on-hover capability to view the details of a pie chart segment.

It appears plotly will render a gpplot piechart as a bar chart due to the presence of geom_bar(my understanding).

I have the following code:

g2 <- ggplot( data = DT, aes( x = 1, y = percentage, fill = as.factor( operationtype ) ) ) +
  geom_bar( width = 1, stat = "identity" ) +
  facet_wrap( ~deviceid, ncol = 1 ) +
  coord_polar(theta = "y", start=0) +
  theme(axis.title.x = element_blank(),
        axis.text.x  = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y  = element_blank(),
        axis.ticks.y = element_blank(),) + 
  labs( fill = "status" )
plot2 <- ggplotly(g2)

Where g2 is the ggplot piechart: enter image description here

But after performing plot2 <- ggplotly(g2) , plot2 becomes a bar chart: enter image description here

How can render the same pie chart in plotly?

1
This is a known issue: github.com/ropensci/plotly/issues/878 As a workaround you may create the plot with plotly instead of ggplot2: plotly.com/r/pie-chartstamtam

1 Answers

1
votes

It is best to use plot_ly to create the pie-chart as shown below:

# Create fake data
  df <- data.frame(
    genre=c("Pop", "HipHop", "Latin", "Jazz"), 
    values = c(33,22,25,20)
  )
    
  plot_ly(data=df,labels=~genre, values=~values, type="pie") %>% 
    layout(title = 'United States Music Genre Prevalent in 1999',
           xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
           yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

output

Then use subplot() to combine multiple plots.