0
votes

I have some real time twitter data and I am using these data to find the sentiment analysis. I write a code for sentiment analysis and got the accurate result, like these:

    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

But when i am trying to plot sentiment_type column data as a percentage in pie chart i got an error. Here is my code:

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))

How can I plot sentiment_type column as a percentage in pie chart using plotly package in R, please suggest me.

1
Does it answer your question?Prradep

1 Answers

2
votes

Are you looking for something like this?

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 

enter image description here