简体   繁体   English

R:在ggplot2中使用条形图中的日期

[英]R: using dates in bar graph with ggplot2

I would like to create a bar plot that displays the number of Date entries for a certain date in the y axis of a bar graph and the date on the x axis of a graph. 我想创建一个条形图,在条形图的y轴上显示某个日期的日期条目数,在图的x轴上显示日期。 At the same time, I would like to split the bar into a Yes and No section. 同时,我想将栏分为“是”和“否”部分。

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

In total, there are 6 Date entries on the 25 of March (3Yes/3No) and 7 Date entries on the 26 of March (4Yes/3No). 总共3月25日有6个日期条目(3Yes / 3No),3月26日有7个日期条目(4Yes / 3No)。 Resulting in a graph like this: 结果如下图所示:

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

Sorry for asking this seemingly simple question. 很抱歉问这个看似简单的问题。

EDIT. 编辑。 Adam Quek ask me to provide some data with dput . 亚当郭问我提供一些数据和dput Here are a few rows: 这是几行:

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")

Here's a quick way to plot what you need. 这是绘制所需内容的快速方法。

library(ggplot2)
ggplot(dat, aes(date, fill=as.factor(hungry))) + 
      geom_bar()

在此处输入图片说明

I remade the data as: 我将数据重现为:

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))

EDIT: 编辑:

Additional input with the new example data: 新示例数据的附加输入:

(i) Change the Date into a readable date format in R. Check out R helpfile for strptime on the date/time format (i)将“日期”更改为R中可读的日期格式。在R帮助文件中查看日期/时间格式的strptime

dat$date <- as.Date(dat$Date, "%A, %B %d, %Y %X")

(ii) plot with similar ggplot as above. (ii)使用与上述类似的ggplot进行绘图。

ggplot(dat, aes(date, fill=Hungry)) + 
      geom_bar()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM