带负值的t条形图 [英] t bar graph with negative values

查看:112
本文介绍了带负值的t条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现类似于

I am trying to achieve something similar to this with a dataframe i have but i can't get it to work for me.

我的数据框如下;

Sentiment   word    freq
Negative    obik    52
Negative    ride    7
Negative    just    6
Negative    bad     6
Negative    like    6
Negative    bike    5
Negative    cycl    5
Negative    home    5
Negative    park    5
Negative    get     4
Positive    obik    71
Positive    ride    26
Positive    free    13
Positive    just    11
Positive    cycl    9
Positive    start   8
Positive    come    7
Positive    park    7
Positive    healthi 7
Positive    bike    7

我想要的是一张图,看起来像下面的图像,具有积极情绪的单词以各自的频率出现在x轴上方,而具有消极情绪的单词以各自的频率出现在x轴下方. y轴是单词的频率

What i want is a graph that looks like the image below where words that have a positive sentiment appear above the x-axis with their respective frequency and word with a negative sentiment appear below the x-axis with their respective frequency. The y-axis is the frequency of the word

经过多次失败的尝试,我回到了没有达到我想要的目标的简单代码;

After many failed attempts, i am back with the simple code that does not achieve what i want;

ggplot(AsiaSentiment10,aes(word,freq,label=""))+
geom_bar(stat="identity",position="identity")

我需要做些什么来制作图形?

What do i need to do to make my graph?

推荐答案

也许这就是您想要的?

df$sign_freq = df$freq * ifelse(df$Sentiment == "Positive", 1, -1)
df$word = reorder(df$word, df$sign_freq, sum)

ggplot(df, aes(x = word, y = sign_freq, fill = Sentiment)) +
    geom_bar(position = 'identity', stat = "identity")

还是这个?

df_agg = aggregate(sign_freq ~ word, FUN = sum, data = df)

ggplot(df_agg, aes(x = word, y = sign_freq, fill = factor(sign(sign_freq)))) +
    geom_bar(position = 'identity', stat = "identity") +
    guides(fill = FALSE) +
    labs(y = "diff")

这篇关于带负值的t条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆