gpt4 book ai didi

r - ggplot(散点图)中的 direct.label 不起作用

转载 作者:行者123 更新时间:2023-12-04 10:15:21 27 4
gpt4 key购买 nike

所以我希望直接标签函数以不重叠的方式定位数据点的标签。但是,我收到一条错误消息,提示我需要指定标签的 aes 函数。这很奇怪,因为我已经包含了它。

这是我正在使用的代码:

p<- ggplot(d, aes(x=ILE2, y=TE,label=d$CA)) +
geom_point(shape=20,size=6,label=d$CA)+
geom_smooth(method=lm,se=F)+
scale_colour_hue(l=50)+
ggtitle("Tasa de Empleo según Índice de Libertad Económica") +
labs(x="Índice de Libertad Económica",y="Tasa de Empleo") +
theme(plot.title = element_text(family = "Arial", color="#666666", face="bold", size=22, hjust=0.5)) +
theme(axis.title = element_text(family = "Arial", color="#666666", face="bold", size=22))
direct.label(p,method="smart.grid")

这是输出:

enter image description here

这是数据集:

structure(list(CA = structure(c(1L, 2L, 3L, 4L, 6L, 8L, 9L, 5L, 
7L, 10L, 11L, 12L, 14L, 15L, 16L, 17L, 13L), .Label = c("Andalucía",
"Aragón", "Asturias", "Balears", "C. La Mancha", "C. Valenciana",
"C. y León", "Canarias", "Cantabria", "Cataluña", "Extremadura",
"Galicia", "La Rioja", "Madrid", "Murcia", "Navarra", "País Vasco"
), class = "factor"), CA.excel = structure(c(1L, 2L, 3L, 4L,
10L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 15L, 16L, 17L), .Label = c("Andalucía",
"Aragón", "Asturias, Principado de", "Balears, Illes", "Canarias",
"Cantabria", "Castilla - La Mancha", "Castilla y León", "Cataluña",
"Comunitat Valenciana", "Extremadura", "Galicia", "Madrid, Comunidad de",
"Murcia, Región de", "Navarra, Comunidad Foral de", "País Vasco",
"Rioja, La"), class = "factor"), ILE = c(0.64, 0.45, 0.61, 0.36,
0.4, 0.4, 0.48, 0.54, 0.5, 0.5, 0.72, 0.53, 0.19, 0.49, 0.43,
0.46, 0.39), ILE2 = c(0.36, 0.55, 0.39, 0.64, 0.6, 0.6, 0.52,
0.46, 0.5, 0.5, 0.28, 0.48, 0.81, 0.51, 0.58, 0.54, 0.61), TE = c(39.04,
47.6, 40.61, 48.82, 44.65, 43.06, 45.77, 41.85, 43.49, 49.76,
38.38, 41.82, 53.08, 43.4, 49.49, 47.98, 48.83), migdest = c(21774L,
5511L, 3147L, 9333L, 17187L, 7568L, 2689L, 12547L, 8701L, 19727L,
3878L, 6147L, 38182L, 6678L, 3024L, 7363L, 1736L), Poblacion = c(8399618L,
1326403L, 1049875L, 1124972L, 4939674L, 2126144L, 585359L, 2062767L,
2478079L, 7396991L, 1091623L, 2734656L, 6385298L, 1463773L, 636402L,
2165100L, 313569L), MigraPob = c(0.002592261, 0.004154845, 0.002997501,
0.008296203, 0.003479379, 0.003559496, 0.004593765, 0.006082607,
0.003511188, 0.002666895, 0.003552507, 0.002247815, 0.005979674,
0.004562182, 0.004751713, 0.003400767, 0.005536262), Ocupados = structure(c(3L,
12L, 9L, 10L, 1L, 14L, 5L, 13L, 16L, 7L, 8L, 17L, 4L, 11L, 6L,
15L, 2L), .Label = c("1.836.300", "126.900", "2.683.700", "2.786.600",
"226.300", "258.200", "3.023.200", "350.100", "371.800", "455.900",
"513.400", "524.500", "707.000", "771.500", "870.300", "913.300",
"987.500"), class = "factor"), Activos = structure(c(11L, 15L,
12L, 14L, 6L, 2L, 7L, 17L, 3L, 9L, 13L, 4L, 8L, 16L, 10L, 1L,
5L), .Label = c("1.041.500,00", "1.115.000,00", "1.147.000,00",
"1.263.200,00", "153.900,00", "2.425.100,00", "277.900,00", "3.389.400,00",
"3.781.300,00", "306.100,00", "4.042.900,00", "458.900,00", "501.800,00",
"586.600,00", "644.300,00", "700.300,00", "991.500,00"), class = "factor"),
Tocup = c(0.664, 0.814, 0.81, 0.777, 0.757, 0.692, 0.814,
0.713, 0.796, 0.8, 0.698, 0.782, 0.822, 0.733, 0.844, 0.836,
0.825), Paro = c(0.336, 0.186, 0.19, 0.223, 0.243, 0.308,
0.186, 0.287, 0.204, 0.2, 0.302, 0.218, 0.178, 0.267, 0.156,
0.164, 0.175), X..Emp.disueltas14 = structure(c(9L, 16L,
12L, 15L, 17L, 8L, 14L, 1L, 7L, 4L, 11L, 2L, 13L, 10L, 5L,
3L, 6L), .Label = c("1.102", "1.529", "1.544", "1.953", "160",
"196", "2.465", "260", "3.172", "349", "362", "467", "5.147",
"552", "833", "846", "915"), class = "factor"), EmpD1000h = c(0.3776,
0.6378, 0.4448, 0.7405, 0.1852, 0.1223, 0.943, 0.5342, 0.9947,
0.264, 0.3316, 0.5591, 0.8061, 0.2384, 0.2514, 0.7131, 0.6251
), EmpCreadas = c(15541L, 1933L, 1364L, 2887L, 11206L, 3486L,
819L, 2812L, 3000L, 17664L, 1186L, 4266L, 20268L, 2732L,
905L, 3447L, 448L), TasaEmpC = c(1.850203188, 1.45732481,
1.299202286, 2.566286094, 2.26857076, 1.639587911, 1.399141382,
1.363217465, 1.210615158, 2.387998039, 1.086455672, 1.559976831,
3.174166656, 1.866409614, 1.422057127, 1.592074269, 1.42871266
), RentaMediaHogar = c(21332L, 29120L, 25623L, 26923L, 22392L,
21539L, 23905L, 22271L, 24587L, 30407L, 19364L, 26001L, 31587L,
21269L, 33047L, 34240L, 26666L), GananciaMediaTrab = c(20782.03,
22054.85, 21994.99, 20776.29, 19167.93, 20052.12, 20440.56,
20630.07, 24253.73, 20878.02, 19129.72, 19824.66, 26215.36,
20449.83, 23836.93, 26915.07, 20628.81)), .Names = c("CA",
"CA.excel", "ILE", "ILE2", "TE", "migdest", "Poblacion", "MigraPob",
"Ocupados", "Activos", "Tocup", "Paro", "X..Emp.disueltas14",
"EmpD1000h", "EmpCreadas", "TasaEmpC", "RentaMediaHogar", "GananciaMediaTrab"
), class = "data.frame", row.names = c(NA, -17L))

最佳答案

directlabels 从未真正用于标记散点图中的点。很多时候,directlabels 做得不错,但新包 ggrepel 可能更适合散点图中的点标记。 (需要 ggplot2 v2.0.0)

library(ggplot2)
library(ggrepel)

p <- ggplot(d, aes(x = ILE2, y = TE, label = CA)) +
geom_point(shape = 20, size = 5) +
geom_smooth(method = lm, se = F) +
scale_colour_hue(l = 50) +
ggtitle("Tasa de Empleo según Índice de Libertad Económica") +
labs(x = "Índice de Libertad Económica", y = "Tasa de Empleo")

p + geom_text_repel(aes(label = CA), segment.color = "black",
box.padding = unit(0.45, "lines"))

enter image description here

关于r - ggplot(散点图)中的 direct.label 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com