1

我目前正在尝试制作自己的图形时间线,就像本页底部的时间线一样。我使用 rvest 包从该链接中刮掉了表格并将其清理干净。

这是我的代码:

library(tidyverse)
library(rvest)
library(ggthemes)
library(lubridate)

URL <- "https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States"

justices <- URL %>% 
  read_html %>%  
  html_node("table.wikitable") %>% 
  html_table(fill = TRUE) %>% 
  data.frame()

# Removes weird row at bottom of the table
n <- nrow(justices)
justices <- justices[1:(n - 1), ]

# Separating the information I want
justices <- justices %>% 
  separate(Justice.2, into = c("name","year"), sep = "\\(") %>% 
  separate(Tenure, into = c("start", "end"), sep = "\n–") %>% 
  separate(end, into = c("end", "reason"), sep = "\\(") %>% 
  select(name, start, end) 

# Removes wikipedia tags in start column
justices$start <- gsub('\\[e\\]$|\\[m\\]|\\[j\\]$$','', justices$start)

justices$start <- mdy(justices$start)

# This will replace incumbencies with NA
justices$end <- mdy(justices$end)

# Incumbent judges are still around! 
justices[is.na(justices)] <- today()

justices$start = as.Date(justices$start, format = "%m/%d%/Y")
justices$end = as.Date(justices$end, format = "%m/%d%/Y")

justices %>% 
  ggplot(aes(reorder(x = name, X = start))) +
  geom_segment(aes(xend = name,
                   yend = start,
                   y = end)) +
  coord_flip() + 
  scale_y_date(date_breaks = "20 years", date_labels = "%Y") +
  theme(axis.title = element_blank()) +
  theme_fivethirtyeight() +
  NULL

这是 ggplot 的输出(我不担心美学,但我知道它看起来很糟糕!): 这是 ggplot 的输出(我不担心美学,但我知道它看起来很糟糕!):

此图的目标是从开始日期开始按时间顺序排列评委,因此开始日期最早的评委应该在底部,而最近开始的评委应该在顶部。如您所见,有多个实例违反了此规则。

它不是按时间顺序排序,而是简单地将评委按照他们在数据框中出现的顺序列出,这也是维基百科的顺序。因此,另一个线段上方的线段应始终比其下方的线段更靠右

我对重新排序的理解是,它将采用 X = 从 geom_segment 开始并对其进行排序并按该顺序列出名称。

我能找到解决这个问题的唯一帮助是考虑日期,然后以这种方式订购它们,但是我得到了错误

错误:无效输入:date_trans 仅适用于 Date 类的对象。

感谢您的帮助!

4

2 回答 2

1

我会对此发表评论,但我无法适应它。

这是我放弃的尝试。看起来它确实解决了问题,但它破坏了格式的其他几个方面,我已经没有时间修复它了。

justices <- justices[order(justices$start, decreasing = TRUE),]
any(diff(justices$start) > 0) # FALSE, i.e. it works

justices$id <- nrow(justices):1


ggplot(data=justices, mapping=aes(x = start, y=id)) + #,color=name, color = 
  scale_x_date(date_breaks = "20 years", date_labels = "%Y") +
  scale_y_discrete(breaks=justices$id, labels = justices$name) +
  geom_segment(aes(xend = end, y = justices$id, yend = justices$id), size = 5) +
  theme(axis.title = element_blank()) +
  theme_fivethirtyeight() 

另请参阅线程。GL!

于 2018-06-29T22:36:00.133 回答
1

您可以使该name列成为一个因素,并用于forcats::fct_reorder根据开始日期重新排序名称。fct_reorder可以采用用于订购的功能start;您可以使用min()每个法官的最早开始日期订购。这样,具有多个开始日期的裁判将根据最早的日期进行排序。只有两行更改:mutate在管道的开头添加 a ,并删除reorderinside aes

justices %>% 
  mutate(name = as.factor(name) %>% fct_reorder(start, min)) %>%
  ggplot(aes(x = name)) +
  geom_segment(aes(xend = name,
                   yend = start,
                   y = end)) +
  coord_flip() + 
  scale_y_date(date_breaks = "20 years", date_labels = "%Y") +
  theme(axis.title = element_blank()) +
  theme_fivethirtyeight()

reprex 包(v0.2.0)于 2018 年 6 月 29 日创建。

于 2018-06-30T00:04:14.923 回答