【问题标题】:In ggplot 2, how to connect the start and the end points in geom_line在ggplot 2中,如何连接geom_line中的起点和终点
【发布时间】:2016-08-31 05:57:03
【问题描述】:

我想用geom_line连接三个点“a”、“b”和“c”,但它只连接“ab”和“bc”,而不连接“ca”,我怎样才能使用geom_line或geom_path来实现呢? , 而不是 geom_polygon?

library(ggplot2)
df <- data.frame (line =c ("a", "b", "c"), x = c(1, 2, 3), y = c(5, 2, 5))

ggplot() +
geom_path(data = df, aes (x = x, y = y, color = line))

【问题讨论】:

  • 好问题!那么,你也想要传奇吗?你试过了吗?如果是这样,请分享您的代码。

标签: r ggplot2


【解决方案1】:

您需要在最后重复第一行以获得闭合多边形而不使用geom_polygon

ggplot(rbind(df, head(df, 1)), aes(x = x, y = y, color = line, group = 1)) + 
  geom_path()

【讨论】:

    【解决方案2】:

    这是一种方法,您可以通过添加向量中的第一个点来绘制平行于 x 轴的线段。本质上,如果要创建多边形,则需要提供到 geom_path 的完整路径。

    注意:d 行:

    df <- data.frame (line =c ("a", "b", "c","d"), x = c(1, 2, 3,1), y = c(5, 2, 5,5))
    
         ggplot ()+
           geom_path(data = df, aes (x = x, y = y))
    

    希望这会有所帮助!

    【讨论】:

    • 它可以工作,但它仍然有点棘手,尤其是当我使用图例时。
    猜你喜欢
    • 2015-01-08
    • 2019-08-27
    • 2021-04-11
    • 2019-09-27
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多