我有一些实时的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
但是当我试图在饼图中将sentiment_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))
如何使用R中的sentiment_type
包在饼图中将plotly
列绘制为百分比,请建议我。
答案 0 :(得分:2)
你在找这样的东西吗?
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