gpt4 book ai didi

r - ggplot2 geom_line() 应该指向指定的值

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

我编写了以下代码:

library(ggplot2)

data <- structure(list(x = c(1L, 6L, 3L, 4L, 2L, 3L, 6L, 1L, 5L, 2L,
1L, 5L), y = c(1L, 7L, 5L, 6L, 3L, 4L, 6L, 2L, 5L, 6L, 5L, 2L
), year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("2010", "2011"), class = "factor"), matching = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("person1",
"person2", "person3", "person4", "person5", "person6"), class = "factor")), .Names = c("x",
"y", "year", "matching"), row.names = c(NA, -12L), class = "data.frame")

data$year <- factor(data$year)

colors <- c("#4cb5ee", "#a0d099", "red")

p <- ggplot(data, aes(x=x, y=y)) +
geom_point(aes(colour=year), shape=16, size=6) +
geom_line(aes(group=matching), arrow=arrow(length=unit(0.15,"cm")), colour="black", size=1) +
xlab("x") + ylab("y") +
scale_colour_manual("year", values=colors) +
scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))

print(p)

它提供以下输出:
plot

但是我想要 geom_line() 做的是:始终指向 year=2011 的点。 我不明白为什么线的箭头有时指向指向 year=2010 的点,有时指向指向 year=2011 的点。

我发现箭头需要几个参数:
arrow(angle = 30, length = unit(0.25, "inches"), ends = "last", type = "open") 

这样我就可以说 ends="first" .但我不能一概而论 ends总是 first或总是 last .

我试图在我的 data.frame 中添加一列,其中包含箭头应该首先结束还是最后结束的信息,但它没有给我想要的输出。

非常感谢每一个帮助:-)

提前致谢!

最佳答案

geom_path应该做的伎俩:

p <- ggplot(data, aes(x=x, y=y)) +
geom_point(aes(colour=year), shape=16, size=6) +
geom_path(aes(group=matching),
arrow=arrow(length=unit(0.15,"cm")),
colour="black", size=1) +
xlab("x") + ylab("y") +
scale_colour_manual("year", values=colors) +
scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))

print(p)

geom_path plot

关于r - ggplot2 geom_line() 应该指向指定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5831838/

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