ggplot2 geom_line() 应指向指定值

发布于 2024-11-04 05:54:38 字数 1731 浏览 1 评论 0原文

我编写了以下代码:

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 的点。< /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:
plot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白衬杉格子梦 2024-11-11 05:54:39

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_pathplot

geom_path should do the trick:

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

ぃ双果 2024-11-11 05:54:39

可能有一种更有效的方法来做到这一点,但一种方法是使用geom_segment()而不是geom_line()。这将允许您轻松指定线的起点和终点。我们必须重构数据,以便可以指定 x、y、xend 和 Yend。我将通过合并来重组,尽管您可能可以通过强制转换或重塑来做到这一点。

zz <- merge(data[data$year == 2010 ,], data[data$year == 2011 ,]
  , by = "matching", suffixes = 1:2) 

  matching x1 y1 year1 x2 y2 year2
1  person1  1  1  2010  6  6  2011
2  person2  6  7  2010  1  2  2011
3  person3  3  5  2010  5  5  2011
4  person4  4  6  2010  2  6  2011
5  person5  2  3  2010  1  5  2011
6  person6  3  4  2010  5  2  2011

然后,我们将在调用 ggplot 时使用两个数据集:

ggplot() +                                                       #Blank call to ggplot
  geom_point(data = data, aes(x=x, y=y, colour=year), shape=16, size=6) +    #Points
  geom_segment(data = zz, aes(x = x1, y = y1, xend = x2, yend = y2),         #Segments
    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))

There is probably a more efficient way to do this, but one approach is to use geom_segment() instead of geom_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.

zz <- merge(data[data$year == 2010 ,], data[data$year == 2011 ,]
  , by = "matching", suffixes = 1:2) 

  matching x1 y1 year1 x2 y2 year2
1  person1  1  1  2010  6  6  2011
2  person2  6  7  2010  1  2  2011
3  person3  3  5  2010  5  5  2011
4  person4  4  6  2010  2  6  2011
5  person5  2  3  2010  1  5  2011
6  person6  3  4  2010  5  2  2011

We will then use two datasets in our call to ggplot:

ggplot() +                                                       #Blank call to ggplot
  geom_point(data = data, aes(x=x, y=y, colour=year), shape=16, size=6) +    #Points
  geom_segment(data = zz, aes(x = x1, y = y1, xend = x2, yend = y2),         #Segments
    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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文