ggplot2 geom_line() 应指向指定值
我编写了以下代码:
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)
它给出了以下输出:
但是我想要 geom_line() 做的是:始终指向 Year=2011 的点。< /strong> 我不明白为什么该线的箭头有时指向year=2010 的点,有时指向year=2011 的点。
我发现箭头需要几个参数:
arrow(angle = 30, length = unit(0.25, "inches"), ends = "last", type = "open")
这样我就可以说 ends="first"
。但我不能概括地说 ends
始终是 first
或始终 last
。
我尝试在 data.frame 中添加一列,其中包含箭头是否应首先结束或最后结束的信息,但它没有提供我想要的输出。
非常感谢每一个帮助:-)
提前致谢!
I have written the following code:
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)
It gives the following output:
But what I want geom_line() to do is: always points at the point where year=2011. I can't figure out why the arrow of the line is point sometimes at a point which refers to year=2010 and sometimes points at a point where year=2011.
What I found out is that arrow takes several arguments:
arrow(angle = 30, length = unit(0.25, "inches"), ends = "last", type = "open")
So that I could say ends="first"
. But I can't generalize that ends
is always first
or always last
.
I tried to add a column to my data.frame which has the information if the arrow should end first or last, but it didn't gives me the output I wanted.
Every help is highly appreciated :-)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
geom_path
应该可以解决问题:geom_path
should do the trick:可能有一种更有效的方法来做到这一点,但一种方法是使用
geom_segment()
而不是geom_line()
。这将允许您轻松指定线的起点和终点。我们必须重构数据,以便可以指定 x、y、xend 和 Yend。我将通过合并来重组,尽管您可能可以通过强制转换或重塑来做到这一点。然后,我们将在调用 ggplot 时使用两个数据集:
There is probably a more efficient way to do this, but one approach is to use
geom_segment()
instead ofgeom_line()
. This will allow you to specify the beginning and ending points of the line with ease. We have to restructure the data so that we can specify x, y, xend, and yend. I will restructure with merge, though you could probably do this with cast or reshape.We will then use two datasets in our call to
ggplot
: