5

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.

5
  • 1
    we cld rly use a dput(tweets_frame) (or part of it) in the example Commented Aug 3, 2015 at 14:04
  • The data frame has a created column, that contains a posix timestamp, and then columns for the text of the tweet, tweet-id, screenname of author, etc... I can upload a sample later, but the question is general and not specific to the data. Commented Aug 3, 2015 at 14:17
  • 2
    I agree with hrbrmstr. It'd be a lot better if you provided sample data and showed what you have tried. Are you looking for something like this: ggplot(data=mtcars, aes(x=hp)) + geom_line(aes(fill=..count..), stat="bin", binwidth=10)? Commented Aug 3, 2015 at 14:56
  • @Jota ok, the missing part in my solution was stat="bin" , and I also found that By default, geom_bar uses stat="bin" and geom_line does not. That's why I did not think about it. Do you want to add your comment as an answer, @Jota. Commented Aug 3, 2015 at 20:23
  • I updated the question to give more detailed information. Commented Aug 4, 2015 at 8:24

1 Answer 1

5

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).
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.