Reputation: 87
I am trying to achieve something similar to this with a dataframe i have but i can't get it to work for me.
My dataframe is as follows;
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
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?
Upvotes: 1
Views: 1373
Reputation: 146050
Maybe this is what you are looking for?
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")
Or this?
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")
Upvotes: 4