使用 R 和 vistime-package 在时间轴中绘制历史时期,BC 是不可能的

问题描述 投票:0回答:2

对于一个研究项目,我想绘制一条历史时间表,其中包含古代王朝的统治时期;分为男性和女性统治者。

我正在使用 R 和

vistime
包(如here建议)。一般来说它是有效的,但问题是我不知道如何使用日期 BC;这意味着负数年。

我的代码的一个简单示例如下:

# devtools::install_github("edgararuiz/gregorian")
# library(gregorian)
library(ggplot2)
library(vistime)

counter.chronology <- data.frame(
  namen = c("Ptolemaios V.","Ptolemaios VI.","Kleopatra I.","Kleopatra II.","Kleopatra III."),
  geschlecht = c("Männliche Regenten","Männliche Regenten","Weibliche Regentinnen","Weibliche Regentinnen","Weibliche Regentinnen"),
  antritt = as.Date(c("0197-01-01","0180-01-01","0194-01-01","0175-01-01","0141-01-01")),
  ende = as.Date(c("0180-01-01","0164-01-01","0176-01-01","0164-01-01","0130-01-01")),
  stringsAsFactors = FALSE
)

vistime(counter.chronology, col.event = "namen", col.group = "geschlecht", col.start = "antritt", col.end = "ende")

这工作正常并产生如下图:

plot with wrong (positive) dates

但是,如果我将日期格式更改为负年份 - 例如

"-0175-01-01"
- 绘图不起作用,我会收到错误消息:

    Errore in charToDate(x) : 
    character string is not in a standard unambiguous format

我尝试了

gregorian
包并将
as.Date
替换为
as_gregorian
,但这似乎与
vistime
不兼容。

有人知道这个问题的简单解决方案吗?如果不可能出现负日期,则有助于扭转绘图,即沿 x 轴从最高年份到最低年份进行倒计时。

一个不太重要的问题,但如果解决了也很好:使用年份作为开始和结束日期就足够了。几个月和几天是不必要的。但是,如果我仅输入,例如,

"-0175
或肯定版本
"0175"
,则会出现与上述相同的错误消息。因此,我使用
01-01
来表示月份和日期。无论如何它都是有效的,因为时间表不是那么详细。解决这个问题固然很好,但这不是必须的。

感谢您的所有回复和解答!

最好的,

弗洛

艾伦回复后编辑:

由于我的简短代码示例,发生了另一个您无法意识到的问题。有时统治时期会重叠。如果我现在进入另一个与其他两个时期重叠的女性统治——例如,“Arsinoe”——,它看起来很尴尬:

counter.chronology <- data.frame(
  namen = c("Ptolemaios V.", "Ptolemaios VI.",
            "Kleopatra I.","Arsinoe", "Kleopatra II.","Kleopatra III."),
  geschlecht = rep(c("Männliche Regenten", "Weibliche Regentinnen"), 
                   times = c(2, 4)),
  antritt = -c(197, 180, 194, 200, 175, 141),
  ende = -c(180, 164, 176, 150, 164, 130)
)

Overlapping periods

是否可以让

ggplot
自动将这个重叠条放置在其他条之上一级?

r plot date-formatting vistime
2个回答
1
投票

如果直接在 ggplot 中执行此操作,则可以直接使用负年份作为连续刻度上的整数值:

counter.chronology <- data.frame(
  namen = c("Ptolemaios V.", "Ptolemaios VI.",
            "Kleopatra I.", "Kleopatra II.","Kleopatra III."),
  geschlecht = rep(c("Männliche Regenten", "Weibliche Regentinnen"), 
                   times = c(2, 3)),
  antritt = -c(197, 180, 194, 175, 141),
  ende = -c(180, 164, 176, 164, 130)
)

library(geomtextpath)

ggplot(counter.chronology, aes(antritt, geschlecht)) +
  geom_textsegment(aes(xend = ende, yend = geschlecht, label = namen,
                       colour = namen), gap = FALSE,
                   linewidth = 30, textcolour = 'black') +
  scale_x_continuous(labels = abs) +
  scale_color_brewer(palette = 'Pastel1', guide = 'none') +
  theme_minimal(base_size = 16) 


0
投票

使用

timevis
包找到了另一种方法。到目前为止效果很好,也有重叠的时期:

library(timevis)

counter.chronology <- data.frame(
  content = c("Ptolemaios V.","Ptolemaios VI.","Kleopatra I.","Arsinoe","Kleopatra II.","Kleopatra III."),
  start = c("-000197-01-01","-000180-01-01","-000194-01-01","-000190-01-01","-000175-01-01","-000141-01-01"),
  end = c("-000180-01-01","-000164-01-01","-000160-01-01","-000145-01-01","-000164-01-01","-000130-01-01"),
  group = c(1,1,2,2,2,2),
  style = c("background-color: #f1d9a4; border-color: black;", 
            "background-color: #ceaf7a; border-color: black;",
            "background-color: #ca9865; border-color: black;")
)

timevis(
  counter.chronology, 
  groups = data.frame(
    id = 1:2, 
    content = c("Regenten", "Regentinnen"),
    width = 900
    )
)

Working solution

从风格角度来看,我更喜欢 ggplot 版本,但没有找到任何解决重叠条问题的解决方案。使用

gg_vistime
将是我的首选解决方案 - 因为它结合了
vistime
ggplot
的工具。不幸的是,就像在
vistime
-Github-page 上看到的那样,除了复杂的解决方法之外,似乎没有可行的解决方案来使用 BC-dates:https://github.com/shosaco/vistime/issues/6

目前,我采用

timevis
解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.