簡體   English   中英

在R中使用Plotly繪制餅圖時出錯

[英]Error while plotting pie-chart using Plotly in R

我有一些實時的Twitter數據,並且正在使用這些數據來查找情緒分析。 我編寫了用於情感分析的代碼,並獲得了准確的結果,如下所示:

    senti_value Sentiment_type
1          0.00        Neutral
2         -0.75       Negative
3          0.00        Neutral
4         -0.25       Negative
5         -3.25       Negative
6         -0.35       Negative
7          0.35        Positve
8          1.75        Positve
9         -2.40       Negative

但是,當我嘗試將sendiment_type列數據繪制為餅圖中的百分比時,出現了錯誤。 這是我的代碼:

p <- plot_ly(data, labels = ~Sentiment_type, values = ~as.character(Sentiment_type), type = 'pie') %>%
  layout(title = 'Sentiment Analysis',
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

我怎樣才能繪制sentiment_type列使用餅圖的百分比plotly中的R包,請給我建議。

您是否正在尋找這樣的東西?

data <- read.table(text="    senti_value Sentiment_type
1          0.00        Neutral
2         -0.75       Negative
3          0.00        Neutral
4         -0.25       Negative
5         -3.25       Negative
6         -0.35       Negative
7          0.35        Positve
8          1.75        Positve
9         -2.40       Negative", header=T)
data

p <- plot_ly(data %>% group_by(Sentiment_type) %>% 
               summarise(n=n()) %>% mutate(percent=n/sum(n)), 
             labels = ~Sentiment_type, values = ~percent, type = 'pie') %>%
  layout(title = 'Sentiment Analysis',
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
p 

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM