我想创建一个条形图,显示条形图y轴上某个日期的日期条目数和图形x轴上的日期。与此同时,我想将条形分为是和否部分。
Date,Hungry
Friday, March 24, 2017 2:33:46 PM,Yes
Friday, March 24, 2017 3:39:46 PM,No
Friday, March 24, 2017 4:39:46 PM,Yes
Friday, March 24, 2017 5:39:46 PM,No
Friday, March 24, 2017 6:39:46 PM,Yes
Friday, March 24, 2017 7:39:46 PM,Yes
Saturday, March 25, 2017 9:41:00 AM,Yes
Saturday, March 25, 2017 10:41:00 AM,Yes
Saturday, March 25, 2017 11:41:00 AM,No
Saturday, March 25, 2017 13:41:00 AM,No
Saturday, March 25, 2017 14:41:00 AM,No
Saturday, March 25, 2017 15:41:00 AM,Yes
Saturday, March 25, 2017 16:41:00 AM,Yes
总共有3月25日的6个日期条目(3Yes / 3No)和3月26日的7个日期条目(4Yes / 3No)。得到如下图:
Hungry
^
|
|
|
|
9|
| _
| _ |n|
| |n| |o|
5| |o| |_|
| |_| |y|
| |y| |e|
| |e| |s|
1|__|s|________|_|__________>
Date
2 2
4 5
M M
A A
R R
C C
H H
很抱歉提出这个看似简单的问题。
EDIT。 Adam Quek让我用dput
提供一些数据。以下是几行:
dat <- structure(list(Date = structure(1:4, .Label = c("Friday, March 24, 2017 4:39:46 PM",
"Friday, March 24, 2017 7:21:33 PM", "Friday, March 24, 2017 7:21:51 PM",
"Friday, March 24, 2017 7:22:00 PM", "Friday, March 24, 2017 7:22:14 PM",
"Saturday, March 25, 2017 8:00:10 AM", "Saturday, March 25, 2017 8:00:25 AM",
"Saturday, March 25, 2017 8:00:46 AM", "Saturday, March 25, 2017 8:01:02 AM",
"Saturday, March 25, 2017 8:01:20 AM", "Saturday, March 25, 2017 8:01:37 AM",
"Saturday, March 25, 2017 8:01:50 AM"), class = "factor"), Hungry = structure(c(2L,
1L, 2L, 1L), .Label = c("No", "Yes"), class = "factor")), .Names = c("Date",
"Hungry"), row.names = c(NA, 4L), class = "data.frame")
答案 0 :(得分:1)
这是一种快速绘制所需内容的方法。
library(ggplot2)
ggplot(dat, aes(date, fill=as.factor(hungry))) +
geom_bar()
我将数据重新制作为:
dat <- structure(list(date = structure(c(17249, 17249, 17249, 17249,
17249, 17249, 17250, 17250, 17250, 17250, 17250, 17250, 17250
), class = "Date"), hungry = structure(c(2L, 1L, 2L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L), .Label = c("No", "Yes"), class = "factor")), class = "data.frame", .Names = c("date",
"hungry"), row.names = c(NA, -13L))
编辑:
使用新示例数据的附加输入:
(i)将日期更改为R中的可读日期格式。在日期/时间格式上查看strptime
的R帮助文件
dat$date <- as.Date(dat$Date, "%A, %B %d, %Y %X")
(ii)与上述类似的ggplot的情节。
ggplot(dat, aes(date, fill=Hungry)) +
geom_bar()