我正在使用5k完成时间的数据集,看起来像这样:
"15:34"
"14:23"
"17:34"
等等,有很多,但是它们都是这样格式化的。我能够将它们全部转换为POSIXct,并将它们存储在数据帧中,从而使使用ggplot2更加容易,但是为了我的生命,我无法让ggplot更改颜色。fill命令不起作用,图形仅保持灰色。
我尝试仅引用我创建的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文档,但不确定自己缺少什么,感谢所有帮助,谢谢
stat_bin(bins = 30)
正在覆盖您设置的任何内容geom_histogram()
。通常,每个几何都有一个关联的默认统计信息,您可以使用两者之一来绘制对象,但是当您尝试同时使用两者时,可能会遇到问题。有几种解决方案。这是一个例子。
ggplot(diamonds, aes(x = carat)) + geom_histogram(fill = "red") + stat_bin(bins = 30)
产生带有灰色填充的图
ggplot(diamonds, aes(x = carat)) + geom_histogram(fill = "red", bins = 30)
产生带有红色填充的图
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句