我有一个对象,其中包含国会各成员的参与数据,按年份分组并按政党和办公室分类。我从中提取了每年的最高参与次数,并使用下面的代码进行相应的排序。
test <- OfficeRaw %>%
filter(!is.na(id)) %>%
count(`Year`,`MoC Name`,`Party Label`, `Congressional Entity`) %>%
filter(`Party Label` != "Indipendent") %>%
slice_max(order_by = n,by=Year, n=10) %>%
ungroup() %>%
mutate(`MoC Name`=reorder_within(`MoC Name`, n, Year))
这创建了我在这里提供的数据集。
structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), levels = c("2019",
"2020", "2021", "2022", "2023"), class = "factor"), `MoC Name` = structure(c(56L,
52L, 44L, 38L, 47L, 48L, 49L, 18L, 19L, 24L, 25L, 25L, 44L, 29L,
32L, 35L, 65L, 64L, 61L, 53L, 57L, 37L, 20L, 53L, 26L, 31L, 63L,
58L, 55L, 50L, 51L, 39L, 42L, 45L, 46L, 30L, 33L, 34L, 36L, 60L,
62L, 59L, 54L, 40L, 41L, 21L, 22L, 23L, 27L, 28L, 43L, 12L, 13L,
14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L), levels = c("Debbie Stabenow___2023", "Dick Durbin___2023",
"Jake LaTurner___2023", "Jim McGovern___2023", "Joni Ernst___2023",
"Marco Rubio___2023", "Mitch McConnell___2023", "Raphael Warnock___2023",
"Robert Aderholt___2023", "Sherrod Brown___2023", "Tom Kean___2023",
"Colin Allred___2023", "Cory Booker___2023", "Earl Blumenauer___2023",
"John Boozman___2023", "Mike Braun___2023", "Sara Jacobs___2023",
"Cory Booker___2019", "Dean Phillips___2019", "Dean Phillips___2020",
"Gregory Meeks___2022", "Hal Rogers___2022", "Jackie Walorski___2022",
"Jeff Merkley___2019", "Jim Costa___2019", "Jim McGovern___2020",
"Jim Risch___2022", "Joaquin Castro___2022", "K. Michael Conaway___2019",
"Karen Bass___2021", "Lois Frankel___2020", "Marco Rubio___2019",
"Marco Rubio___2021", "Michael McCaul___2021", "Roger Marshall___2019",
"Sara Jacobs___2021", "Betty McCollum___2020", "Chrissy Houlahan___2019",
"Darin LaHood___2021", "Darin LaHood___2022", "Earl Blumenauer___2022",
"Gregory Meeks___2021", "Jeff Merkley___2023", "Jim McGovern___2019",
"Jim McGovern___2021", "Joaquin Castro___2021", "Pat Roberts___2019",
"Ted Lieu___2019", "Todd Young___2019", "Betty McCollum___2021",
"Cory Booker___2021", "Ilhan Omar___2019", "Jackie Walorski___2020",
"Bob Casey___2022", "Earl Blumenauer___2021", "John Boozman___2019",
"Xochitl Torres Small___2020", "Grace Meng___2021", "Sara Jacobs___2022",
"Betty McCollum___2022", "Darin LaHood___2020", "Grace Meng___2022",
"Jackie Walorski___2021", "Bob Casey___2020", "Earl Blumenauer___2020"
), class = "factor", scores = structure(c(`Betty McCollum___2020` = 6,
`Betty McCollum___2021` = 7, `Betty McCollum___2022` = 10, `Bob Casey___2020` = 11,
`Bob Casey___2022` = 8, `Chrissy Houlahan___2019` = 6, `Colin Allred___2023` = 4,
`Cory Booker___2019` = 5, `Cory Booker___2021` = 7, `Cory Booker___2023` = 4,
`Darin LaHood___2020` = 10, `Darin LaHood___2021` = 6, `Darin LaHood___2022` = 6,
`Dean Phillips___2019` = 5, `Dean Phillips___2020` = 5, `Debbie Stabenow___2023` = 3,
`Dick Durbin___2023` = 3, `Earl Blumenauer___2020` = 13, `Earl Blumenauer___2021` = 8,
`Earl Blumenauer___2022` = 6, `Earl Blumenauer___2023` = 4, `Grace Meng___2021` = 9,
`Grace Meng___2022` = 10, `Gregory Meeks___2021` = 6, `Gregory Meeks___2022` = 5,
`Hal Rogers___2022` = 5, `Ilhan Omar___2019` = 7, `Jackie Walorski___2020` = 7.5,
`Jackie Walorski___2021` = 10, `Jackie Walorski___2022` = 5,
`Jake LaTurner___2023` = 3, `Jeff Merkley___2019` = 5, `Jeff Merkley___2023` = 6,
`Jim Costa___2019` = 5, `Jim McGovern___2019` = 6, `Jim McGovern___2020` = 5,
`Jim McGovern___2021` = 6, `Jim McGovern___2023` = 3, `Jim Risch___2022` = 5,
`Joaquin Castro___2021` = 6, `Joaquin Castro___2022` = 5, `John Boozman___2019` = 8,
`John Boozman___2023` = 4, `Joni Ernst___2023` = 3, `K. Michael Conaway___2019` = 5,
`Karen Bass___2021` = 5, `Lois Frankel___2020` = 5, `Marco Rubio___2019` = 5,
`Marco Rubio___2021` = 5, `Marco Rubio___2023` = 3, `Michael McCaul___2021` = 5,
`Mike Braun___2023` = 4, `Mitch McConnell___2023` = 3, `Pat Roberts___2019` = 6,
`Raphael Warnock___2023` = 3, `Robert Aderholt___2023` = 3, `Roger Marshall___2019` = 5,
`Sara Jacobs___2021` = 5, `Sara Jacobs___2022` = 9, `Sara Jacobs___2023` = 4,
`Sherrod Brown___2023` = 3, `Ted Lieu___2019` = 6, `Todd Young___2019` = 6,
`Tom Kean___2023` = 3, `Xochitl Torres Small___2020` = 8), dim = 65L, dimnames = list(
c("Betty McCollum___2020", "Betty McCollum___2021", "Betty McCollum___2022",
"Bob Casey___2020", "Bob Casey___2022", "Chrissy Houlahan___2019",
"Colin Allred___2023", "Cory Booker___2019", "Cory Booker___2021",
"Cory Booker___2023", "Darin LaHood___2020", "Darin LaHood___2021",
"Darin LaHood___2022", "Dean Phillips___2019", "Dean Phillips___2020",
"Debbie Stabenow___2023", "Dick Durbin___2023", "Earl Blumenauer___2020",
"Earl Blumenauer___2021", "Earl Blumenauer___2022", "Earl Blumenauer___2023",
"Grace Meng___2021", "Grace Meng___2022", "Gregory Meeks___2021",
"Gregory Meeks___2022", "Hal Rogers___2022", "Ilhan Omar___2019",
"Jackie Walorski___2020", "Jackie Walorski___2021", "Jackie Walorski___2022",
"Jake LaTurner___2023", "Jeff Merkley___2019", "Jeff Merkley___2023",
"Jim Costa___2019", "Jim McGovern___2019", "Jim McGovern___2020",
"Jim McGovern___2021", "Jim McGovern___2023", "Jim Risch___2022",
"Joaquin Castro___2021", "Joaquin Castro___2022", "John Boozman___2019",
"John Boozman___2023", "Joni Ernst___2023", "K. Michael Conaway___2019",
"Karen Bass___2021", "Lois Frankel___2020", "Marco Rubio___2019",
"Marco Rubio___2021", "Marco Rubio___2023", "Michael McCaul___2021",
"Mike Braun___2023", "Mitch McConnell___2023", "Pat Roberts___2019",
"Raphael Warnock___2023", "Robert Aderholt___2023", "Roger Marshall___2019",
"Sara Jacobs___2021", "Sara Jacobs___2022", "Sara Jacobs___2023",
"Sherrod Brown___2023", "Ted Lieu___2019", "Todd Young___2019",
"Tom Kean___2023", "Xochitl Torres Small___2020")))), `Party Label` = c("Republican",
"Democrat", "Democrat", "Democrat", "Republican", "Democrat",
"Republican", "Democrat", "Democrat", "Democrat", "Democrat",
"Democrat", "Democrat", "Republican", "Republican", "Republican",
"Democrat", "Democrat", "Republican", "Republican", "Democrat",
"Democrat", "Democrat", "Republican", "Democrat", "Democrat",
"Republican", "Democrat", "Democrat", "Democrat", "Democrat",
"Republican", "Democrat", "Democrat", "Democrat", "Democrat",
"Republican", "Republican", "Democrat", "Democrat", "Democrat",
"Democrat", "Democrat", "Republican", "Democrat", "Democrat",
"Republican", "Republican", "Republican", "Democrat", "Democrat",
"Democrat", "Democrat", "Democrat", "Republican", "Republican",
"Democrat", "Democrat", "Democrat", "Republican", "Democrat",
"Republican", "Republican", "Republican", "Democrat", "Republican",
"Democrat", "Republican"), `Congressional Entity` = c("Senate",
"House", "House", "House", "Senate", "House", "Senate", "Senate",
"House", "Senate", "House", "Senate", "Senate", "Senate", "Senate",
"Senate", "House", "Senate", "House", "House", "House", "Senate",
"Senate", "Senate", "Senate", "Senate", "House", "House", "House",
"House", "Senate", "House", "House", "House", "House", "House",
"Senate", "House", "House", "House", "House", "House", "Senate",
"House", "House", "House", "House", "House", "Senate", "House",
"Senate", "House", "Senate", "House", "Senate", "Senate", "House",
"Senate", "Senate", "House", "House", "Senate", "Senate", "Senate",
"Senate", "House", "Senate", "House"), n = c(8L, 7L, 7L, 6L,
6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 13L, 11L, 10L,
10L, 8L, 6L, 5L, 5L, 5L, 5L, 10L, 9L, 8L, 7L, 7L, 6L, 6L, 6L,
6L, 5L, 5L, 5L, 5L, 10L, 10L, 9L, 8L, 6L, 6L, 5L, 5L, 5L, 5L,
5L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L)), row.names = c(NA, -68L), class = c("tbl_df", "tbl",
"data.frame"))
我一直在尝试创建一个降序水平图,使用下面的代码按年份对前十名活动的列表进行排序:
test %>%
ggplot(aes(n,`MoC Name`, fill = `Party Label`), stat = "identity") +
geom_col() +
facet_wrap(~Year, scales = 'free_y') +
scale_y_reordered()
结果如下:
图表乱序,我尝试对这些奇怪的列重新排序一段时间,但无济于事,使用 fct_inorder,更改 reorder_within 的位置,甚至回退到旧的重新排序函数。我不确定目前如何解决这个问题。
非常感谢任何帮助或重定向到相关信息!
问题似乎可能在于每个方面中
MoC Name
变量的排序。您可以使用 forcats 包中的 fct_reorder
函数来显式指定每个方面中 MoC Name
的顺序。此外,您还可以使用 coord_flip
创建水平条形图。
尝试修改后的代码,看看它是否产生所需的输出:
library(ggplot2)
library(forcats)
test %>%
mutate(`MoC Name` = fct_reorder(`MoC Name`, n)) %>%
ggplot(aes(n, `MoC Name`, fill = `Party Label`), stat = "identity") +
geom_col() +
facet_wrap(~Year, scales = 'free_y') +
scale_y_reordered() +
coord_flip()
在此代码中,
fct_reorder
用于根据每个方面内的MoC Name
变量对n
重新排序。 coord_flip
函数用于创建水平条形图。这应该会生成一个图,其中每个方面内的条形根据 n
变量进行排序。