[英]shrink the x-axis with ggplot2
我使用ggplot2軟件包獲得了該圖
使用代碼:
ggplot(count(c4l.tweets, "by.hour"), aes(x=by.hour, y=freq)) + geom_bar(stat="identity") + xlab("Number") + ylab("Date") + labs(title="#c4l13 tweets by the hour")
我只想在第一個網格上縮小時間軸。 我試過了:
ggplot(count(subset(c4l.tweets, time > as.POSIXct("2013-02-12 09:00:00", tz="CST") & time < as.POSIXct("2013-04-14 12:00:00", tz="CST"), "by.hour"), aes(x=by.hour, y=freq)) + geom_bar(stat="identity") + xlab("Number") + ylab("Date") + labs(title="#c4l13 tweets by the hour")
但它不起作用,它給了我一個空白圖表...
還有,如果我想使其看起來像時間序列圖? 我應該使用哪種美學(即連接每個點)?
非常感謝您,文森佐
將您在鏈接中提供的表作為數據幀c41.tweets
,可以按如下設置限制。
df <- data.frame(time=c41.tweets$time)
df$time <- as.POSIXct(df$time,format="%d/%m/%Y %H:%M:%S")
df$hour <- as.POSIXct(cut(df$time, breaks="hour"))
freq <- data.frame(table(df$hour, dnn="hour"))
freq$hour <- as.POSIXct(freq$hour)
start <- as.POSIXct("2013-02-01 09:00:00")
end <- as.POSIXct("2013-03-01 12:00:00")
ggp <- ggplot(freq)
ggp <- ggp + geom_bar(aes(x=hour,y=Freq),stat="identity")
ggp <- ggp + labs(title="c4l13 Tweets by the Hour [Feb-01 to Mar-01]")
ggp <- ggp + labs(y="Count", x="2013")
ggp <- ggp + theme(plot.title=element_text(face="bold"))
ggp <- ggp + xlim(start,end)
ggp
關於第二個問題,這是您的初衷嗎?
ggp <- ggplot(freq)
ggp <- ggp + geom_line(aes(x=hour,y=Freq))
ggp <- ggp + labs(title="c4l13 Tweets by the Hour [Feb-01 to Mar-01]")
ggp <- ggp + labs(y="Count", x="2013")
ggp <- ggp + theme(plot.title=element_text(face="bold"))
ggp <- ggp + xlim(start,end)
ggp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.