我有一个带有推文的数据框(包含时间码、推文 ID、文本等),并且想要可视化每小时的推文数量。它适用于条形图:
我使用以下代码生成条形图(created以 POSIX 格式存储推文的时间码):
ggplot(data=tweets_frame, aes(x=created)) +
geom_bar(aes(fill=..count..), binwidth=3600) +
scale_x_datetime("Time") +
scale_y_continuous("Tweets")
Run Code Online (Sandbox Code Playgroud)
我想生成相同的图形,但作为折线图而不是条形图。
我试图只替换geom_bar为geom_line:
ggplot(data=tweets_frame, aes(x=created)) +
geom_line(aes(fill=..count..), binwidth=3600) +
scale_x_datetime("Time") +
scale_y_continuous("Tweets")
Run Code Online (Sandbox Code Playgroud)
这导致此错误消息:
eval(expr,envir,enclos)中的错误:找不到对象“计数”
我无法弄清楚如何..count..在折线图中指定。
您可以从stat="identity"默认设置切换到,geom_line到stat="bin",这允许使用..count..。我用了mtcars这个例子的数据,我随意设置binwidth为10。
ggplot(data=mtcars, aes(x=hp)) + geom_line(aes(fill=..count..), stat="bin", binwidth=10).
Run Code Online (Sandbox Code Playgroud)