如何在ggplot2中将变量与自身进行比较?

时间:2018-01-24 17:16:24

标签: r ggplot2

我目前有一个用基础R编写的图,我想将其转换为ggplot2-而且我不确定如何在一个方面内将变量与自身进行比较。

enter image description here

这里有一个可重复的数据示例:

structure(list(state_abbv = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 
8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 
14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 
16L, 16L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 19L, 
19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 
21L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 
24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 
27L, 27L, 27L, 27L, 27L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 
29L, 29L, 30L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 32L, 
32L, 32L, 32L, 32L, 33L, 33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 
34L, 35L, 35L, 35L, 35L, 35L, 36L, 36L, 36L, 36L, 36L, 37L, 37L, 
37L, 37L, 37L, 38L, 38L, 38L, 38L, 38L, 39L, 39L, 39L, 39L, 39L, 
40L, 40L, 40L, 40L, 40L, 41L, 41L, 41L, 41L, 41L, 42L, 42L, 42L, 
42L, 42L, 43L, 43L, 43L, 43L, 43L, 44L, 44L, 44L, 44L, 44L, 45L, 
45L, 45L, 45L, 45L, 46L, 46L, 46L, 46L, 46L, NA, 47L, 47L, 47L, 
47L, 47L, 48L, 48L, 48L, 48L, 48L, 49L, 49L, 49L, 49L, 49L, 50L, 
50L, 50L, 50L, 50L, 51L, 51L, 51L, 51L, 51L), .Label = c("AK", 
"AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", 
"IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", 
"MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", 
"NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", 
"VA", "VT", "WA", "WI", "WV", "WY"), class = "factor"), year = c(2008L, 
2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 
2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 
2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 
2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 
2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 
2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 
2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 
2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 
2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 
2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 
2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 
2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 
2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 
2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 
2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 
2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 
2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 
2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 
2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 
2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 
2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 
2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 
2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 
2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 
2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 
2010L, 2012L, 2014L, 2016L, NA, 2008L, 2010L, 2012L, 2014L, 2016L, 
2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 2014L, 
2016L, 2008L, 2010L, 2012L, 2014L, 2016L, 2008L, 2010L, 2012L, 
2014L, 2016L), eavs_completeness = c(1, 1, 1, 1, 1, 0.4860942, 
0.5942518, 0.5824811, 0.6252103, 0.7425884, 0.7073488, 0.977605, 
0.9692506, 0.950947, 0.9330504, 0.9894488, 0.9910133, 0.990735, 
1, 0.9855512, 0.9675562, 0.9853116, 0.9831181, 0.9956588, 0.9912663, 
0.9396685, 1, 1, 1, 0.9444444, 0.7778371, 0.9439592, 0.7777778, 
0.9444444, 0.8888376, 0.7777778, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
0.8888889, 1, 1, 0.9959325, 0.9998797, 0.993493, 0.9413403, 0.9985401, 
1, 0.9012241, 0.9262081, 0.9262494, 0.9494085, 0.9626083, 0.9444444, 
0.9993886, 1, 1, 0.8313369, 0.9375, 0.9375, 0.9366804, 0.9375, 
0.9375, 0.6392842, 0.9379956, 0.8224649, 0.8199335, 0.9441188, 
0.9436936, 0.9826866, 0.9932502, 0.968887, 1, 0.8860591, 1, 0.9444444, 
0.9444444, 0.9444444, 0.7222222, 0.9995568, 1, 0.9444444, 0.9444444, 
1, 1, 1, 1, 1, 0.6666667, 0.8756685, 0.9998856, 1, 0.9999976, 
0.8888889, 0.9997876, 1, 1, 1, 0.9314949, 0.9997647, 0.9999949, 
0.9999197, 0.7222222, 0.9634053, 1, 1, 1, 0.9444444, 0.968371, 
0.9750772, 1, 1, 1, 0.9568514, 0.9761097, 0.9968871, 1, 0.9331664, 
0.7352235, 0.784887, 0.7214549, 0.9352053, 0.9444444, 0.9959454, 
0.9995347, 1, 1, 0.9444444, 1, 1, 1, 1, 1, 1, 1, 1, 1, NA, 1, 
0.9742549, 0.9822419, 1, 0.9444444, 0.6875, 0.8125, 1, 1, 1, 
0.8954756, 1, 1, 1, 0.9444444, 0.81182, 0.8955516, 0.9605955, 
0.9275535, 0.8713285, 0.996626, 0.9874724, 1, 1, 1, 0, 0.6675814, 
0.8380723, 0.9062535, 0.9365531, 0.974529, 0.9905872, 0.9921069, 
0.9951113, 1, 0.9444444, 0.9423813, 0.9444444, 1, 1, 0.6541629, 
0.9049812, 0.9388529, 0.9444444, 0.8333333, 0.9444652, 1, 1, 
1, 0.9444444, 0.5, 0.9439893, 0.9455113, 0.9444444, 0.8888889, 
0.8796172, 0.889398, 0.8180441, 0.9444444, 0.8888889, 0.8119472, 
0.9694064, 0.9725954, 1, 0.9848165, 0.8842599, 0.9799823, 0.9898868, 
0.9955469, 1, 0.9739842, 0.9999651, 0.9450474, 0.9749324, 0.9527729, 
0.973514, 1, 0.9444444, NA, 0.8333333, 0.9978397, 0.9456893, 
0.997191, 1, 0.9444444, NA, 0.995383, 0.907802, 0.927192, 0.9763107, 
0.6669849, 0.8869602, 0.965042, 1, 1, 0.9916472, 0.8815994, 0.8888889, 
0.9990603, 1, 1, 0.7684922, 0.813571, 0.9601688, 1, 0.9995461, 
0.8834472, 0.8937522, 0.9005393, 0.8959528, 0.9444444)), class = "data.frame", row.names = c(NA, 
-256L), .Names = c("state_abbv", "year", "eavs_completeness"))

2 个答案:

答案 0 :(得分:1)

一种方法是将数据与自身相连,同时生成year.xyear.y以及要绘制的两个值。这可以使用dplyr' inner_join()完成。

使用您的数据(尚未抛光)的示例:

library(ggplot2)
library(dplyr)

dat %>%
  filter(!is.na(year)) %>%
  inner_join(dat, by = "state_abbv") %>%
  filter(year.x != year.y) %>%
  ggplot(aes(eavs_completeness.x, eavs_completeness.y)) +
  geom_point() +
  geom_text(aes(label = state_abbv), vjust = 1, hjust = 1) +
  facet_grid(year.y ~ year.x)

enter image description here

答案 1 :(得分:0)

在没有看到您的代码的情况下,我相信您正在寻找的命令是来自ggally包的ggpairs()。这是一篇关于它的stack overflow文章。