【问题标题】:How do I fill the space between multiple line segments in ggplot?如何填充ggplot中多个线段之间的空间?
【发布时间】:2019-11-23 16:54:23
【问题描述】:

假设我在 ggplot2 中有以下情节:

p <- ggplot() +
  geom_point(aes(x=15,y=50),shape=19,fill="gray0", size=5)+
  geom_point(aes(x=28, y=75),shape=19, fill="gray0", size=5)+
  geom_point(aes(x=13, y=100),shape=19, fill="gray0", size=5)+
  geom_segment(aes(x = 15, y = 50, xend = 28, yend = 75), size=1)+
  geom_segment(aes(x = 13, y = 100, xend = 28, yend = 75), size=1)+
  geom_segment(aes(x = 15, y = 50, xend = 13, yend = 100), size=1)

这会绘制在公共点相交的三个线段,从而创建一个三角形。然后我将如何用颜色填充形成的三角形? (假设我需要使用geom_segment)

【问题讨论】:

  • 我建议先退后一步,阅读一些 ggplot 教程。在线文档提供了几个链接。 ggplot 通常适用于数据帧,而不仅仅是手动输入单个数字,它适用于长格式的数据,这样您就不会多次调用同一个 geom 来做同样的事情。目前没有任何信息表明一个片段与另一个片段有任何关系,或者它们内部的空间有任何意义

标签: r ggplot2 graph charts


【解决方案1】:

您可以改用 geom_polygon:

df <- data.frame(x = c(15, 28, 13), y = c(50,75,100))

ggplot() +
      geom_point(df, mapping = aes(x = x, y = y), size = 5) +
      geom_polygon(df, mapping=aes(x = x, y = y), fill="grey")

你可能想要三角形顶部的点,如果是这样,geom_polygon 然后 geom_point

【讨论】:

  • 假设我需要使用一系列 geom_segments 来构建形状。那么有没有办法填充 geom_segments 之间的空间?
猜你喜欢
  • 2023-04-06
  • 2018-12-28
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多