#
console
R

Sentiment Heatmap in R – EDIT: Discretise

Here is a small edit to the previous Sentiment Heatmap in R post – Discretise the sentiment score for a more adequate colour scale.

We can base ourselves on the previous code. We discretise the sentiment scores by forming ranges using ‘cut’. Then run the same ggplot snippet only changing the colour scale from scale_fill_gradient2 to scale_fill_brewer since it is not continuous but discrete.

#copy our previous data.frame
sent.dc <- senti

#Inspect continuous scores (x)
head(sent.dc)
        date  time          x
1 2014-10-25 00:00  1.0000000
2 2014-10-26 00:00 -0.3333333
3 2014-10-27 00:00 -1.0000000
4 2014-10-28 00:00  0.0000000
5 2014-10-29 00:00 -1.0000000
6 2014-10-30 00:00  0.0000000 

#discretise scores - form ranges
sent.dc$x <- cut(sent.dc$x, breaks = c(-Inf,(min(sent.dc$x)-1):max(sent.dc$x),Inf))

#inspect discrete scores - ranges
        date  time       x
1 2014-10-25 00:00   (0,1]
2 2014-10-26 00:00  (-1,0]
3 2014-10-27 00:00 (-2,-1]
4 2014-10-28 00:00  (-1,0]
5 2014-10-29 00:00 (-2,-1]
6 2014-10-30 00:00  (-1,0]

#heatmap
ggplot(data = s, aes(x = date, y = time)) +
  geom_tile(aes(fill=x),colour="grey5") +
  scale_fill_brewer(palette = "RdYlGn") +
  theme(axis.text.y = element_text(colour="white"),
        axis.text.x = element_text(colour="white"),
        axis.ticks.y = element_line(colour="grey5"),
        axis.ticks.x = element_line(colour="grey5"),
        axis.title.x = element_text(colour="white"),
        axis.text.y = element_text(colour="white"),
        plot.background=element_rect(fill="grey5"),
        panel.background=element_rect(fill="grey5"),
        panel.border=element_rect(fill=NA,colour="grey5"),
        panel.grid.minor.x = element_line(colour="grey5"),
        panel.grid.major.x = element_line(colour="grey5"),
        panel.grid.minor.y = element_line(colour="grey5"),
        panel.grid.major.y = element_line(colour="grey5"),
        legend.background = element_rect(fill = "grey5"),
        legend.text = element_text(colour="white"),
        legend.title = element_text(colour="white"))

disc_hm

Advertisements

Discussion

Trackbacks/Pingbacks

  1. Pingback: Sentiment Heatmap in R | SocialFunction() - November 19, 2014

  2. Pingback: Sentiment Heatmap in R - Jabber Cruncher - May 24, 2015

reply()

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: