【问题标题】:ggplot: draw segment that goes off the chart's limitsggplot:绘制超出图表限制的段
【发布时间】:2020-09-28 20:24:27
【问题描述】:

我想绘制一个图表,其分段超出了绘图的限制。 Ggplot 只是简单地抑制段而不是显示它的可见部分。

这是一个示例:

library(ggplot2)

df <- data.frame(x1 = 2, x2 = 6, y1 = 10, y2 = 50)

ggplot(mtcars, aes(wt, mpg)) +
    geom_point() +
    geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2), data = df) +
    xlim(c(0, 7)) +
    ylim(c(0, 35)) +
    coord_fixed(.1)

这就是我想要的:

但是该段没有被绘制,我收到以下警告:

Warning message:
Removed 1 rows containing missing values (geom_segment). 

我想保持图表限制和coord_fixed(.1) 设置不变。

【问题讨论】:

  • 没有必要删除标记为重复的帖子 - 它们是指向规范来源中最佳信息的有用指针。

标签: r ggplot2


【解决方案1】:

使用xlimylim 将删除超出限制的数据。要达到预期的结果,请在 coord_fixed 内设置限制。

library(ggplot2)

df <- data.frame(x1 = 2, x2 = 6, y1 = 10, y2 = 50)

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2), data = df) +
  coord_fixed(.1, xlim= c(0,7), ylim = c(0, 35))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2020-05-20
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多