sách gpt4 ai đã đi

Cách chú thích ggplot2 một cách tốt nhất (thủ công)

In lại 作者:行者123 更新时间:2023-12-03 06:57:31 29 4
mua khóa gpt4 Nike

sử dụngggplot2我通常使用geom_textposition=jitter之类的东西来注释我的图。

但是 - 对于一个漂亮的情节,我经常发现手动注释是值得的。如下所示:

data2 <- structure(list(type = structure(c(5L, 1L, 2L, 4L, 3L, 5L, 1L, 
2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L), .Label = c("EDS",
"KIU", "LAK", "MVH", "NA*"), class = "factor"), value = c(0.9,
0.01, 0.01, 0.09, 0, 0.8, 0.05, 0, 0.15, 0, 0.41, 0.04, 0.03,
0.52, 0, 0.23, 0.11, 0.02, 0.64, 0.01), time = c(3L, 3L, 3L,
3L, 3L, 6L, 6L, 6L, 6L, 6L, 15L, 15L, 15L, 15L, 15L, 27L, 27L,
27L, 27L, 27L), year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2007L,
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
2006L, 2006L, 2006L, 2006L, 2006L)), .Names = c("type", "value",
"time", "year"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L,
11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 21L, 22L, 23L, 24L), class = "data.frame")
ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw()+
annotate("text", x=6, y=0.9, label="this is a wrong color")+
annotate("text", x=15, y=0.6, label="this is a second annotation with a wrong color")

问题是,我无法让文本注释颜色与线条颜色相匹配。我想我可以用手动秤来解决这个问题,但我希望有更好的方法吗?

1 Câu trả lời

我也遇到了类似的问题,并通过 JD Long 回答解决了它。但由于 ggplot2 更新到版本 0.9.0,我注意到所有 geom_text() 调用在绘图上都显得有些模糊。

感谢kohske我发现这段代码

ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw() +
geom_text(aes(7, .9, label="correct color", color="NA*")) +
geom_text(aes(15, .6, label="another correct color!", color="MVH"))

绘制 geom_text nrow(data2) 次!

向 geom_text 提供数据的正确方法是构建一个不同的 data.frame,其中包含要绘制的字符串的坐标、标签和颜色:

data2.labels <- data.frame(
time = c(7, 15),
value = c(.9, .6),
label = c("correct color", "another correct color!"),
type = c("NA*", "MVH")
)

ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw() +
geom_text(data = data2.labels, aes(x = time, y = value, label = label))

关于r - 如何很好地注释 ggplot2(手册),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2409357/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com