使用 R 进行数据可视化:情绪分析

Data visualization with R: sentiment analysis

我的数据框如下所示,

text                 class.negative        class.positive       class
<fctr>                    <dbl>              <dbl>              <dbl>

firmly believe...       11                   24                   3
when i thought...       3                    3                    4
fans of david...        11                   24                   12
just watched...         3                    5                    9
i was so looking...     16                   9                    10

我想可视化结果并开始学习如何使用 ggplot,并显示 "positive"、"negative" 和 "total scores"。但显然我不能简单地把它们写成

ggplot(data=..., aes(x=..., y=..., fill=...)) + geom_bar(stat="identity",position = 'stack') + ggtitle('Sentimental Analysis')

我想知道如何创建类似这样的图表 http://joxi.ru/vAWvKx5HeXp72W,非常感谢任何 tips/advice!

您需要先重塑数据,然后使用 tidyr 中的 geom_line from ggplot. I will use gather 重塑数据。我叫起始data_framedff.

dff %>% gather(opinion, values, -text) %>%
    ggplot(data = .) + 
    geom_line(aes(x = text, y = values, group = opinion, color = opinion))

这应该产生:

您还可以使用 geom_bar 通过以下方式可视化数据:

dff %>% 
    gather(opinion, values, -text) %>%
    ggplot() +
    geom_bar(aes(y = values, x = text, fill = opinion), stat = "identity")

它应该产生如下内容:

希望对您有所帮助。