我试图根据第三个变量在散点图上有选择地设置图例的大小。我有以下数据集:
structure(list(horizontal = c(24, 88, 58, 14, 38, 43, 68, 90,
16, 23, 66, 81, 53, 36, 86, 60, 76, 10, 48, 52, 26, 9, 34, 85,
12, 75, 41, 63, 15, 51, 92, 80, 17, 6, 28, 1, 55, 21, 56, 29,
11, 22, 83, 69, 27, 67, 30, 49, 84, 3, 13, 87, 57, 95, 91, 35,
93, 72, 45, 65, 47, 70, 74, 4, 25, 2, 71, 20, 61, 79, 32, 89,
50, 54, 40, 77, 19, 62, 18, 7, 42, 44, 31, 82, 33, 64, 5, 94,
73, 39, 78, 46, 59, 8, 37), vertical = c(19, 87, 52, 94, 84,
93, 20, 34, 45, 7, 8, 22, 10, 65, 3, 47, 95, 86, 49, 85, 77,
36, 58, 15, 44, 66, 32, 42, 73, 71, 2, 75, 60, 46, 55, 48, 4,
5, 26, 30, 33, 70, 24, 67, 63, 92, 81, 51, 56, 23, 78, 11, 80,
79, 53, 25, 14, 29, 61, 21, 43, 72, 54, 6, 28, 35, 18, 16, 38,
31, 88, 50, 40, 91, 62, 89, 1, 37, 57, 82, 68, 64, 41, 12, 59,
17, 39, 13, 90, 74, 83, 9, 76, 27, 69), object_n = structure(c(95L,
14L, 15L, 18L, 8L, 42L, 69L, 13L, 16L, 17L, 44L, 5L, 47L, 50L,
89L, 75L, 22L, 39L, 53L, 56L, 78L, 30L, 33L, 2L, 61L, 92L, 72L,
64L, 59L, 87L, 84L, 19L, 81L, 9L, 36L, 27L, 24L, 45L, 57L, 79L,
31L, 34L, 1L, 90L, 76L, 23L, 40L, 54L, 6L, 48L, 51L, 3L, 62L,
93L, 67L, 73L, 85L, 20L, 82L, 10L, 37L, 28L, 25L, 46L, 70L, 43L,
65L, 60L, 66L, 4L, 63L, 94L, 68L, 12L, 74L, 58L, 80L, 32L, 35L,
91L, 77L, 41L, 55L, 7L, 49L, 52L, 88L, 86L, 21L, 83L, 11L, 38L,
29L, 26L, 71L), .Label = c("bzö08", "cda06", "cda10", "cda12",
"cons05", "cons10", "cons15", "cultlib", "cvp07", "cvp11", "cvp15",
"d6212", "defense", "ecolib", "ecoreform", "edu", "env", "europe",
"fdp_ch07", "fdp_ch11", "fdp_ch15", "fdp_d05", "fdp_d09", "ff07",
"ff11", "ff16", "fg07", "fg11", "fg16", "fpö06", "fpö08", "fpö13",
"gr_a06", "gr_a08", "gr_a13", "gr_ch07", "gr_ch11", "gr_ch15",
"gr_d05", "gr_d09", "gr_d13", "immig", "indep_irl11", "infra",
"lab_irl07", "lab_irl11", "lab_uk05", "lab_uk10", "lab_uk15",
"libdem05", "libdem10", "libdem15", "linke/pds05", "linke/pds09",
"linke/pds13", "övp06", "övp08", "övp13", "psf07", "psf12", "pvda06",
"pvda10", "pvda12", "rpr/ump07", "rpr/ump12", "rr_f12", "rr_nl10",
"rr_nl12", "security", "sf11", "sf16", "sp06", "sp10", "sp12",
"spd05", "spd09", "spd13", "spö06", "spö08", "spö13", "sps07",
"sps11", "sps15", "svp07", "svp11", "svp15", "udf07", "ukip15",
"union05", "union09", "union13", "vvd06", "vvd10", "vvd12", "welfare"
), class = "factor"), year = structure(c(12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L
), .Label = c("05", "06", "07", "08", "09", "10", "11", "12",
"13", "15", "16", "9999"), class = "factor")), .Names = c("horizontal",
"vertical", "object_n", "year"), row.names = c(NA, -95L), class = "data.frame")
我尝试了以下代码:
ggplot(dat, aes(horizontal, vertical)) +
geom_point(color = 'red') +
geom_text_repel(data=subset(dat, year!=unique(year, na.rm=T)[1]),
aes(horizontal, vertical, label = object_n, size=10)) +
geom_text_repel(data=subset(dat, year==unique(year, na.rm=T)[1]),
aes(horizontal, vertical, label = object_n, size=12)) +
theme_classic(base_size = 16)
结果是:
问题在于,无论我为size
选择多近接近的值,都会将这些值视为因子,并且差异总是太大。我确信这是一个简单的解决方案,但不幸的是我无法理解。谢谢!
答案 0 :(得分:1)
这是:
library(ggplot2)
library(ggrepel)
ggplot(data, aes(horizontal, vertical)) +
geom_point(color = 'red') +
geom_text_repel(data=subset(data, year!=unique(year, na.rm=T)[1]),
aes(horizontal, vertical, label = object_n), size=5) +
geom_text_repel(data=subset(data, year==unique(year, na.rm=T)[1]),
aes(horizontal, vertical, label = object_n),size=3) +
theme_classic(base_size = 16)
问题是你把size参数放在aes()
里面,它应该在它之外。