选择标签因子(ggplot2,directlabels) [英] Selecting factor for labels (ggplot2, directlabels)

查看:209
本文介绍了选择标签因子(ggplot2,directlabels)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用包裹的直接标签来标记我的情节。不过,我希望标签是每个点的ID。

 图书馆(ggplot2)
图书馆是否真的没有办法选择标注哪个因子? (直接标签)
df< - 结构(
list(id = 1:10,
group = structure(c(1L,1L,1L,1L,1L,2L,2L,2L ,2L,2L),
.Label = c(A,B),
class =factor),
value1 = c(4,1,6,2 ,5,7,3,2,5,8),
value2 = c(6,2,6,2,8,9,7,5,2,6)
),
.Names = c(id,group,value1,value2),
row.names = c(NA,-10L),
class =data.frame )

p1 < - ggplot(df,aes(x = value1,y = value2))+ geom_point(aes(color = group))
direct.label(p1)

解决方案

检查



(注意你不需要再调用 direct.label()。)



directlabels 包确实有点稀缺。


I'd like to use the package directlabels to label my plot. However, I'd like the label to be the ID of each point. Is there really no way to select which factor to label or did I miss that?

library(ggplot2)
library(directlabels)
df <- structure(
    list(id = 1:10,
         group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
         .Label = c("A", "B"),
         class = "factor"),
         value1 = c(4, 1, 6, 2, 5, 7, 3, 2, 5, 8),
         value2 = c(6, 2, 6, 2, 8, 9, 7, 5, 2, 6)
         ),
.Names = c("id", "group", "value1", "value2"),
row.names = c(NA, -10L),
class = "data.frame")

p1 <- ggplot(df, aes(x=value1, y=value2)) + geom_point(aes(colour=group))
direct.label(p1)

解决方案

Inspecting the code of direct.label.ggplot() shows that geom_dl() is called in the end. This function expects an aesthetic mapping and a positioning method. The positioning method used by default is the return value of default.picker("ggplot"), which uses call stack examination and in your case is equivalent to calling defaultpf.ggplot("point",,,). The following works for me:

p1 <- ggplot(df, aes(x=value1, y=value2)) +
  geom_point(aes(colour=group)) +
  geom_dl(aes(label = id), method = defaultpf.ggplot("point",,,))
p1

(Note that you don't need to call direct.label() anymore.)

The documentation of the directlabels package is indeed a bit scarce.

这篇关于选择标签因子(ggplot2,directlabels)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆