Plotly无法使用数据帧的子集渲染正确的绘图

时间:2017-07-04 08:02:50

标签: r ggplot2 plotly

我正在尝试对数据框进行分组,并使用plotly创建条形图:

resort_pos %>% subset(resort + pos > 5000)%>% 
mutate(pos_percentage = (pos*100)/(resort+pos)) %>% 
mutate(Source = as.factor(src)) %>% 
plot_ly(x= ~Source,y= ~pos_percentage, type = "bar")

以下是resort_pos的头部和resort_pos的子集

>>head(resort_pos)
src resort positive
1 ABL  45686     7352
2 AGR     96        0
3 AMD  15857    21077
4 BAM   3806   141275
5 BBI    416       54
6 BDQ      5        0

>>head(resort_pos %>% subset(resort + pos > 5000)%>% 
    mutate(pos_percentage = (pos*100)/(resort+pos)) %>% 
    mutate(Source = as.factor(src)))

 src resort positive pos_percentage Source
1 ABL  45686     7352       13.86176    ABL
2 AMD  15857    21077       57.06666    AMD
3 BAM   3806   141275       97.37664    BAM
4 BIL  73847   142660       65.89163    BIL
5 BLR  52711   192061       78.46527    BLR
6 BNO  44069    71089       61.73171    BNO

这是输出:

enter image description here

x获取所有不同的值,但由于y在子集化数据框中不可用,因此它显示空白。我怎么能绕过这个?

1 个答案:

答案 0 :(得分:0)

对结果data.frame执行droplevels()