ggplot(scatterplot)中的direct.label无法正常工作

时间:2015-12-25 13:15:36

标签: r ggplot2

所以我希望直接标签功能以不重叠的方式定位数据点的标签。但是,我收到一个错误,告诉我我需要指定标签的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))

2 个答案:

答案 0 :(得分:2)

使用您的代码,我收到以下错误:

     Error in direct.label.ggplot(p, method = "smart.grid") : 
       Need colour aesthetic to infer default direct labels. 

所以,我添加了这样的色彩美学:

p<- ggplot(d, aes(x=ILE2, y=TE, col=CA)) +
  geom_point(shape=20,size=6) +
  geom_smooth(method=lm,se=F, aes(group=1))+
  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

direct.label(p,method="smart.grid")

输出:

enter image description here

但是,如果您希望点是黑色且没有颜色,则可以使用directlabels 2.0中的geom_dl,如此,

p<- ggplot(d, aes(x=ILE2, y=TE)) +
  geom_point(shape=20,size=6) + geom_dl(aes(label=d$CA), method="smart.grid")+ 
  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)) 

输出:enter image description here

请注意,direct.labels的目的是隐藏颜色图例。 (direct.label的描述:&#34;为绘图添加直接标签,并隐藏颜色图例。像lattice和ggplot2这样的现代绘图包根据为颜色指定的变量显示自动图例,但这些图例如果出现则可能会引起混淆颜色太多。直接标签是许多常见情节中令人困惑的传奇的有用而明确的替代品。&#34;)

如果绘图没有颜色图例(代码就是这种情况),则direct.label无效。我认为你应该使用geom_dl,因为你想要呈现你的数据,它不需要颜色图例。

希望有所帮助。

答案 1 :(得分:2)

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