问题描述
我无法在图上创建相同的ggplot饼图,因为我需要plotly的交互式悬停功能来查看饼图段的详细信息。
由于geom_bar
(我的理解)的存在,似乎会阴谋将gpplot饼图绘制为条形图。
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)
但是在执行plot2 <- ggplotly(g2)
之后,plot2成为条形图:
如何在情节上呈现相同的饼图?
解决方法
最好使用plot_ly
来创建pie-chart
,如下所示:
# 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,showticklabels = FALSE))
然后使用subplot()
组合多个图。