r - 将时间线上的时间跨度可视化为水平直方图/条形图

标签 r data-visualization histogram

我有一个包含 6 个事件及其发生时间跨度的 csv 表。我的变量是开始日期、结束日期和事件 ID。我打算创建一个水平直方图/条形图可视化来显示时间范围,即某些类型的事件持续了多长时间。 X 轴应该有多年的日期,Y 轴应该有不同种类的事件 ID。理想情况下,我想要时间跨度长度的水平条。一个事件有多个时间跨度。事件开始,然后结束。几个月后它开始,持续一段时间然后再次结束。我有大约 6 个事件 ID,它们有很多不同的事件。我只想在时间轴上查看发生的事件以进行概述...

编辑: 表格看起来像这样:

Begin      End          EventID
01.01.2000 01.05.2000   Chicago
03.04.1998 03.09.1999   New York
12.03.2014 16.07.2014   Los Angeles
12.12.2003 03.06.2004   Amsterdam
21.06.1993 14.12.1993   Paris
27.02.1995 15.03.1995   London
14.06.2002 15.06.2002   Madrid

我尝试了以下代码:

cities <- read.table(textConnection("Begin End EventID
01.01.2000 01.05.2000   Chicago
03.04.1998 03.09.1999   New York
12.03.2014 16.07.2014   Los Angeles
12.12.2003 03.06.2004   Amsterdam
21.06.1993 14.12.1993   Paris
27.02.1995 15.03.1995   London
14.06.2002 15.06.2002   Madrid
"), sep=" ", header=TRUE)

cities$Begin<- as.Date(cities$Begin, "%d.%m.%Y")
cities$End<- as.Date(cities$End, "%d.%m.%Y")
cities$EventID<- as.factor(cities$EvenID)
cities$Sep <- as.factor(1:length(cities$Begin))

library(ggplot2)

p <- ggplot(data=cities) + geom_segment(aes(x=Begin, xend=End, y=EventID, yend=EventID, 
group=Sep), size=12)

对于:

cities$EventID<- as.factor(cities$EvenID) 

我收到一条错误消息,因为 EventID 不包含整数。

Error in `$<-.data.frame`(`*tmp*`, "EventID", value = integer(0)) : 
Replacement has 0 rows. Data has 75

我是否必须将 EventID 中的数据转换为其他内容?如果是,它是什么?

最佳答案

这是我认为您想要使用 ggplot2 包的示例。语法使这个图表在某种程度上更容易构建(复制数据需要更多代码!)

Lines <- read.table(textConnection("Begin End EventID
01.01.2000 01.05.2000 1
03.04.1998 03.09.1999 1
12.03.2014 16.07.2014 2
12.12.2003 03.06.2004 3
21.06.1993 14.12.1993 2
27.02.1995 15.03.1995 3
14.06.2002 15.06.2002 2
"), sep=" ", header=TRUE)

Lines$Begin <- as.Date(Lines$Begin, "%d.%m.%Y")
Lines$End <- as.Date(Lines$End, "%d.%m.%Y")
Lines$EventID <- as.factor(Lines$EventID)
Lines$Sep <- as.factor(1:length(Lines$Begin))

library(ggplot2)

p <- ggplot(data=Lines) + 
     geom_segment(aes(x = Begin, xend = End, y = EventID, yend = EventID, group=Sep)
     ,size =12)
p

enter image description here

你对间隔直方图的描述让我想起了 this ,但这似乎不是您要问的问题。


使用更新后的代码,导致问题的只是拼写错误。这是您的新数据的示例(修改了 read.table 以使其正常工作,并将 EventID 转换为一个因子时出现了拼写错误)。

这里我还根据顶部的最早日期对情节进行了排序。请注意,在此示例中您实际上不需要 Sep 分组变量,因为同一城市没有多个时间跨度。

cities <- read.table(textConnection("Begin End EventID
01.01.2000 01.05.2000 Chicago
03.04.1998 03.09.1999 New_York
12.03.2014 16.07.2014 Los_Angeles
12.12.2003 03.06.2004 Amsterdam
21.06.1993 14.12.1993 Paris
27.02.1995 15.03.1995 London
14.06.2002 15.06.2002 Madrid
"), sep=" ", header=TRUE)

cities$Begin <- as.Date(cities$Begin, "%d.%m.%Y")
cities$End <- as.Date(cities$End, "%d.%m.%Y")
cities$EventID <- gsub("_"," ",cities$EventID)
cities$EventID <- as.factor(cities$EventID)
cities$Sep <- as.factor(1:length(cities$Begin))

#sorting levels so earliest is at top of graph
cities <- transform(cities, EventID=reorder(EventID, -rank(Begin)))

p <- ggplot(data=cities) + 
     geom_segment(aes(x=Begin, xend=End, y=EventID, yend=EventID, group=Sep), size=12)
p 

enter image description here

关于r - 将时间线上的时间跨度可视化为水平直方图/条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26945868/

相关文章:

python - 使用 rpy2 从 Python 调用自定义函数

python - 如何区分点散布matplotlib on pick

python - 在seaborn barplot之前隐藏文本

r - R中的直方图

c++ - R dyn.load "Symbol not found"错误,即使 C++ 代码构建良好

r - 计算 R 中多列的唯一值

r - 无法加载包 'rJava'

Javascript D3 直方图 : thresholds producing wrong number of bins

Python:如何使用 Plotly 堆叠或叠加直方图

r - ggplot2:有条件的直方图