【发布时间】:2018-08-27 12:43:31
【问题描述】:
我正在处理一个 5k 完成时间的数据集,看起来有点像这样:
"15:34"
"14:23"
"17:34"
等等,有很多,但它们都是这样格式化的。我能够将它们全部转换为 POSIXct,并将它们存储在数据框中,以便更轻松地使用 ggplot2,但对于我的生活,我无法让 ggplot 更改颜色。填充命令不起作用,图形只是保持灰色。
我尝试仅引用我创建的 POSIXct 对象,但 ggplot 抛出错误并告诉我它不适用于 POSIXct。我能够显示直方图的唯一方法是将其存储在数据框中。
我目前使用的代码如下:
#make the data frame
df <- data.frame(
finish_times = times_list)
#set the limits on the x axis as datetime objects
lim <- as.POSIXct(strptime(c('2018-3-18 14:15', '2018-3-18 20:00'), format = "%Y-%m-%d %M:%S"))
#making the plot
ggplot(data = df, aes(x = finish_times)) +
geom_histogram(fill = 'red') + #this just doesn't work
stat_bin(bins = 30) +
scale_x_datetime(labels = date_format("%M:%S"),
breaks = date_breaks("1 min"),
limits = lim) +
labs(title = "2017 5k finishers",
x='Finish Times',
y= 'Counts')
我浏览了很多 ggplot 和 R 文档,但我不确定我缺少什么,感谢所有帮助,谢谢
【问题讨论】:
-
你确定 ggplot 代码没有抛出错误,只是留下了你最后的情节吗?
fill = "red"'应该可以工作,例如试试:ggplot() + geom_histogram(aes(x=rnorm(100)), fill = 'red') -
绘图包有很多不同的部分,仅从文档中很难将它们放在一起。如果您是 ggplot 新手,一个不错的起点是 r4ds.had.co.nz