[英]Filtering (dplyr) factor levels does not filter all rows that it should
I have a dataset of political parties. 我有一个政党数据集。 I want to plot the elecotral results of some of these as a line_geom. 我想将其中一些的电声结果绘制为line_geom。 I only want some parties to be displayed. 我只想显示一些聚会。 So I am filtering the factor levels of the parties I want. 因此,我正在过滤我想要的各方的因子水平。 As can be seen by table() below, for the party PSOE, for instance, I have values for every election year, but these are not filtered out correctly (eg 1993, 1996, 2004), nor plotted afterward. 从下面的表()中可以看出,例如,对于PSOE政党,我每个选举年都有一个值,但是这些值没有被正确滤除(例如1993、1996、2004),也没有随后进行绘制。 I cannot understand why this is happening. 我不明白为什么会这样。
I though it may have to do with the fact that some parties did not participate in elections in all the years, so I am only plotting for PSOE and PP. 尽管我可能与以下事实有关:有些政党多年来一直没有参加选举,所以我只是在谋求PSOE和PP。 But I would in fact like to filter and have displayed also those parties on the respective filter line after the hashtag. 但实际上,我想进行过滤,并在主题标签之后的相应过滤行中也显示了这些参与方。
Please help: 请帮忙:
trial <- structure(list(year = c(1977L, 1977L, 1977L, 1977L, 1977L, 1977L,
1977L, 1977L, 1977L, 1977L, 1977L, 1977L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1989L, 1989L, 1989L,
1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L,
1989L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L,
1993L, 1993L, 1993L, 1996L, 1996L, 1996L, 1996L, 1996L, 1996L,
1996L, 1996L, 1996L, 1996L, 1996L, 2000L, 2000L, 2000L, 2000L,
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2004L,
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L,
2004L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
2008L, 2008L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L), partido = structure(c(30L, 28L, 23L, 2L, 29L, 10L, 24L,
31L, 15L, 14L, 5L, 19L, 30L, 28L, 23L, 2L, 10L, 32L, 27L, 24L,
17L, 15L, 14L, 4L, 22L, 33L, 28L, 2L, 30L, 23L, 10L, 7L, 24L,
17L, 15L, 14L, 28L, 2L, 7L, 10L, 23L, 24L, 17L, 14L, 8L, 22L,
6L, 35L, 28L, 2L, 23L, 7L, 10L, 24L, 17L, 21L, 35L, 13L, 14L,
22L, 6L, 28L, 2L, 23L, 10L, 24L, 6L, 17L, 15L, 22L, 13L, 35L,
2L, 28L, 23L, 10L, 24L, 6L, 3L, 17L, 15L, 13L, 35L, 2L, 28L,
23L, 10L, 24L, 3L, 6L, 21L, 15L, 18L, 13L, 9L, 28L, 2L, 23L,
10L, 15L, 24L, 6L, 3L, 9L, 13L, 20L, 28L, 2L, 23L, 10L, 24L,
34L, 15L, 3L, 6L, 20L, 2L, 28L, 23L, 34L, 10L, 1L, 24L, 15L,
3L, 6L, 12L, 16L, 20L, 2L, 28L, 11L, 25L, 25L, 23L, 25L, 15L,
10L, 25L, 24L, 17L, 6L, 2L, 28L, 25L, 11L, 25L, 25L, 15L, 10L,
25L, 24L, 17L, 6L, 28L, 2L, 11L, 25L, 36L, 15L, 25L, 10L, 24L,
17L, 25L, 12L, 6L, 37L, 26L), .Label = c("AMAIUR", "AP/PP", "BNG",
"C-UPC", "CAIC", "CC", "CDS", "CG", "CHA", "CIU/PDECAT", "CIUDADANOS",
"COMPROMIS", "EA", "EE", "ERC", "FAC", "HB/EHBILDU", "IC-V",
"INDEP", "NABAI/GBAI", "PA", "PAR", "PCE/IU", "PNV", "PODEMOS",
"PRC", "PSA-PA", "PSOE", "PSP-US", "UCD", "UDC-IDCC", "UN", "UPN",
"UPYD", "UV", "VOX", "NA+"), class = "factor"), diputadosPerc = c(47.1428571428571,
33.7142857142857, 5.71428571428571, 4.57142857142857, 1.71428571428571,
3.14285714285714, 2.28571428571429, 0.571428571428571, 0.285714285714286,
0.285714285714286, 0.285714285714286, 0.285714285714286, 48,
34.5714285714286, 6.57142857142857, 2.57142857142857, 2.28571428571429,
0.285714285714286, 1.42857142857143, 2, 0.857142857142857, 0.285714285714286,
0.285714285714286, 0.285714285714286, 0.285714285714286, 0.285714285714286,
57.7142857142857, 30.5714285714286, 3.14285714285714, 1.14285714285714,
3.42857142857143, 0.571428571428571, 2.28571428571429, 0.571428571428571,
0.285714285714286, 0.285714285714286, 52.5714285714286, 30, 5.42857142857143,
5.14285714285714, 2, 1.71428571428571, 1.42857142857143, 0.571428571428571,
0.285714285714286, 0.285714285714286, 0.285714285714286, 0.285714285714286,
50, 30.5714285714286, 4.85714285714286, 4, 5.14285714285714,
1.42857142857143, 1.14285714285714, 0.571428571428571, 0.571428571428571,
0.571428571428571, 0.571428571428571, 0.285714285714286, 0.285714285714286,
45.4285714285714, 40.2857142857143, 5.14285714285714, 4.85714285714286,
1.42857142857143, 1.14285714285714, 0.571428571428571, 0.285714285714286,
0.285714285714286, 0.285714285714286, 0.285714285714286, 44.5714285714286,
40.2857142857143, 6, 4.57142857142857, 1.42857142857143, 1.14285714285714,
0.571428571428571, 0.571428571428571, 0.285714285714286, 0.285714285714286,
0.285714285714286, 52.2857142857143, 35.7142857142857, 2.28571428571429,
4.28571428571429, 2, 0.857142857142857, 1.14285714285714, 0.285714285714286,
0.285714285714286, 0.285714285714286, 0.285714285714286, 0.285714285714286,
46.8571428571429, 42.2857142857143, 1.42857142857143, 2.85714285714286,
2.28571428571429, 2, 0.857142857142857, 0.571428571428571, 0.285714285714286,
0.285714285714286, 0.285714285714286, 48.2857142857143, 44, 0.571428571428571,
2.85714285714286, 1.71428571428571, 0.285714285714286, 0.857142857142857,
0.571428571428571, 0.571428571428571, 0.285714285714286, 53.1428571428571,
31.4285714285714, 3.14285714285714, 1.42857142857143, 4.57142857142857,
2, 1.42857142857143, 0.857142857142857, 0.571428571428571, 0.571428571428571,
0.285714285714286, 0.285714285714286, 0.285714285714286, 35.1428571428571,
25.7142857142857, 11.4285714285714, 12, 3.42857142857143, 0.571428571428571,
2.57142857142857, 2.57142857142857, 2.28571428571429, 1.71428571428571,
1.71428571428571, 0.571428571428571, 0.285714285714286, 39.1428571428571,
24.2857142857143, 12.8571428571429, 9.14285714285714, 3.42857142857143,
2.57142857142857, 2.57142857142857, 2.28571428571429, 1.42857142857143,
1.42857142857143, 0.571428571428571, 0.285714285714286, 35.1428571428571,
18.8571428571429, 16.2857142857143, 9.42857142857143, 6.85714285714286,
4.28571428571429, 2, 2, 1.71428571428571, 1.14285714285714, 0.571428571428571,
0.285714285714286, 0.571428571428571, 0.571428571428571, 0.285714285714286
), diputadosAbs = c(165, 118, 20, 16, 6, 11, 8, 2, 1, 1, 1, 1,
168, 121, 23, 9, 8, 1, 5, 7, 3, 1, 1, 1, 1, 1, 202, 107, 11,
4, 12, 2, 8, 2, 1, 1, 184, 105, 19, 18, 7, 6, 5, 2, 1, 1, 1,
1, 175, 107, 17, 14, 18, 5, 4, 2, 2, 2, 2, 1, 1, 159, 141, 18,
17, 5, 4, 2, 1, 1, 1, 1, 156, 141, 21, 16, 5, 4, 2, 2, 1, 1,
1, 183, 125, 8, 15, 7, 3, 4, 1, 1, 1, 1, 1, 164, 148, 5, 10,
8, 7, 3, 2, 1, 1, 1, 169, 154, 2, 10, 6, 1, 3, 2, 2, 1, 186,
110, 11, 5, 16, 7, 5, 3, 2, 2, 1, 1, 1, 123, 90, 40, 42, 12,
2, 9, 9, 8, 6, 6, 2, 1, 137, 85, 45, 32, 12, 9, 9, 8, 5, 5, 2,
1, 123, 66, 57, 33, 24, 15, 7, 7, 6, 4, 2, 1, 2, 2, 1)), row.names = c(NA,
-169L), class = "data.frame")
. 。
table(trial$partido, trial$year)
trial %>%
filter(partido == c("PSOE", "AP/PP")) %>% #, "CIU/PDECAT", "PNV","PCE/IU", "CDS", "UCD")) %>%
select(year, partido, diputadosPerc) %>%
# melt(natElecSeatsOnly, id = "year", measure.vars = names(natElecSeatsOnly)[c(2,14)], variable.name = "values") %>%
ggplot(aes(year, diputadosPerc, colour=partido)) + geom_line()
. 。
Session info: R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape2_1.4.3 bindrcpp_0.2.2 forcats_0.3.0 stringr_1.3.1 dplyr_0.7.8 purrr_0.2.5 readr_1.3.1
[8] tidyr_0.8.2 tibble_1.4.2 ggplot2_3.1.0 tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 cellranger_1.1.0 pillar_1.3.0 compiler_3.5.2 plyr_1.8.4 bindr_0.1.1
[7] tools_3.5.2 digest_0.6.18 jsonlite_1.5 lubridate_1.7.4 nlme_3.1-137 gtable_0.2.0
[13] lattice_0.20-38 pkgconfig_2.0.2 rlang_0.3.0.1 cli_1.0.1 rstudioapi_0.8 yaml_2.2.0
[19] haven_2.1.0 withr_2.1.2 xml2_1.2.0 httr_1.3.1 knitr_1.20 hms_0.4.2
[25] grid_3.5.2 tidyselect_0.2.5 glue_1.3.0 R6_2.3.0 readxl_1.1.0 modelr_0.1.2
[31] magrittr_1.5 backports_1.1.2 scales_1.0.0 rvest_0.3.2 assertthat_0.2.0 colorspace_1.3-2
[37] labeling_0.3 stringi_1.2.4 lazyeval_0.2.1 munsell_0.5.0 broom_0.5.0 crayon_1.3.4
Use %in%
, like so 使用%in%
,像这样
trial %>%
filter(partido %in% c("PSOE", "AP/PP")) %>% #, "CIU/PDECAT", "PNV","PCE/IU", "CDS", "UCD")) %>%
select(year, partido, diputadosPerc) %>%
# melt(natElecSeatsOnly, id = "year", measure.vars = names(natElecSeatsOnly)[c(2,14)], variable.name = "values") %>%
ggplot(aes(year, diputadosPerc, colour=partido)) + geom_line()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.