library(ggplot2)
print(ggplot(dfComb,aes(x=hrmin,y=count,fill = dfComb$word)) +geom_bar(alpha = 0.5,stat='identity')+
xlab("Minute of Day") +
ylab("Count") +
ggtitle(paste("Frequencies of Tweets by minute from tweetsORC1")) +
scale_fill_hue() +
scale_colour_hue() +
# labs(fill="Word"))
scale_fill_discrete("Word",
breaks=c("A","B"),
labels=c("yes","no" ))+
scale_x_continuous(breaks=seq(00:00, 23:59, 60))
)
如您所见,我已尝试使用
限制标签(根据this)scale_x_continuous(breaks=seq(00:00, 23:59, 60))
但这不起作用,因为我的数据是以下形式:
dput(head(dfComb))
structure(list(hrmin = c("0:00", "0:03", "0:06", "0:08", "0:18",
"0:20"), count = c(1, 1, 1, 1, 1, 1), word = c("B", "B", "B",
"B", "B", "B")), .Names = c("hrmin", "count", "word"), row.names = c(NA,
6L), class = "data.frame")
其中hrmin
的格式为HH:MM,即从0:00到23:59的时间。我想要的标签只指定每小时凌晨1点到午夜,总共只有24个标签。关于如何处理这类数据的任何指示都非常感谢。
答案 0 :(得分:1)
也许你可以尝试转换" hrmin"在aes()函数中变量为一小时?
使用lubridate包,我怀疑它看起来像这样:
install.packages("lubridate")
library(lubridate)
print(ggplot(dfComb,aes(x=hour(hrmin),y=count,fill = dfComb$word)) +geom_bar(alpha = 0.5,stat='identity')+
xlab("Hour of Day") + ....
答案 1 :(得分:1)
未经测试,因为没有示例数据集,但可能以下内容可行。
# Get the hour part, prior to calling 'ggplot'
hr_lab <- as.integer(strftime(paste(Sys.Date(), dfComb$hrmin), format = "%H"))
# then use
scale_x_continuous(breaks = seq(hr_lab[1], hr_lab[length(hr_lab)]))
如果这不起作用,请提前抱歉。只是这样说,我就删除它。
答案 2 :(得分:0)
对于任何在00:00至23:00之后到达的人:
library(dplyr)
0:23 %>% { ifelse(nchar(.) == 1, paste0("0", .), .) } %>% paste0(., ":00")
给出
"00:00" "01:00" "02:00" "03:00" "04:00" "05:00" "06:00" "07:00" "08:00" "09:00"
"10:00" "11:00" "12:00" "13:00" "14:00" "15:00" "16:00" "17:00" "18:00" "19:00"
"20:00" "21:00" "22:00" "23:00"