gpt4 book ai didi

r - 标签的选择因素(ggplot2、directlabels)

转载 作者:行者123 更新时间:2023-12-01 15:26:15 25 4
gpt4 key购买 nike

我想使用包 directlabels 来标记我的情节。但是,我希望标签是每个点的 ID。真的没有办法选择要标记的因素还是我错过了?

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)

enter image description here

最佳答案

检查 the code of direct.label.ggplot()表明 geom_dl() 最后被调用了。此功能需要美学映射和定位方法。使用的定位方式by defaultdefault.picker("ggplot") 的返回值,它使用调用堆栈检查,在您的情况下相当于调用 defaultpf.ggplot("point",,,)。以下对我有用:

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

Plot

(请注意,您不再需要调用 direct.label()。)

directlabels包的文档确实有点少。

关于r - 标签的选择因素(ggplot2、directlabels),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32819466/

25 4 0