Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualize ..count.. in a line graph with ggplot2

Tags:

r

ggplot2

I have a data frame with tweets (containing a timecode, tweet-id, text, etc.) and want to visualise the amount of tweets per hour. It works fine with a bar graph:

Bar graph of tweets per hour

I use the following code to produce the bar graph (created stores the timecode of a tweet in POSIX format):

  ggplot(data=tweets_frame, aes(x=created)) + 
     geom_bar(aes(fill=..count..), binwidth=3600) + 
     scale_x_datetime("Time") + 
     scale_y_continuous("Tweets")

I want to produce the same graph, but as line graph instead of as bar graph.

I tried to just replace geom_bar with geom_line:

  ggplot(data=tweets_frame, aes(x=created)) + 
     geom_line(aes(fill=..count..), binwidth=3600) + 
     scale_x_datetime("Time") + 
     scale_y_continuous("Tweets")

Which resulted in this error message:

Error in eval(expr, envir, enclos) : object 'count' not found

I cannot figure out how to specify the ..count.. in a line graph.

like image 425
MartinW Avatar asked Aug 03 '15 13:08

MartinW


1 Answers

You can switch from stat="identity", the default setting with geom_line, to stat="bin", which allows the use of ..count... I used the mtcars data for this example, and I arbitrarily set binwidth to 10.

ggplot(data=mtcars, aes(x=hp)) + geom_line(aes(fill=..count..), stat="bin", binwidth=10).
like image 74
Jota Avatar answered Oct 23 '22 16:10

Jota