ggplot 将 geom_point 与 geom_line 分开的方法?

M00N 骑士

我有一个情节,我想通过灰线将点连接在一起。我想知道有没有办法在点周围添加边框,以便线接触到它们的边界,或者甚至不接触点,而不是直接进入点的中心。我正在使用 ggplot。

在此处输入图片说明

我已经为任何想要使用它的人提供了一些示例数据。

df <- data.frame(Name = c("a","b","c","d","e","f"),
                  Category = c("x","y","x","y","x","y"),
                  N = c(500,540,470,500,480,520))

我还包括了我的 ggplot 代码,用于制作上面的图

df %>% 
  arrange(N) %>%
  mutate(Name = factor(Name, levels = unique(Name))) %>% 
  ggplot(aes(Name, N)) +
  geom_point(aes(color = Category)) +
  geom_point(shape = 1,size = 2,colour = "black") +
  scale_colour_manual(values = c("green","cyan")) +
  geom_line(aes(group = Name), colour = "grey", alpha = 1) +
  theme(
    axis.title.x = element_blank(),
    axis.ticks = element_blank(),
    axis.title.y = element_blank(),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank(),
    axis.text.x = element_blank(),
    panel.background = element_rect(fill = "white"),
    legend.position = "none")

编辑:

如下所示,在将线切换到点之前的注释中,会产生以下结果:

在此处输入图片说明

托马斯·比拉赫

I don't think your plot is reproducible with unique letters for Name. To fit a vertical line, Name requires unique 'pairs' of letters. In your call to ggplot(), Name is mapped onto the x-axis and also passed to the group aesthetic, which produces a line between two points at the same x-value. For example, your data frame should look something like the following:

  Name Category   N
1    A        x 500
2    A        y 540
3    B        x 470
4    B        y 500
5    C        x 480
6    C        y 520

Note, each group consists of two observations, which should only vary along the vertical dimension.

I imagine your data frame was corrected to account for this, but it isn't clear from the code you supplied. It appears @erocoar already addressed your principal concern in the comments. It is a simple matter of precedence with respect to the geom_ layers.

我使用新的数据框重现了您的情节。请记住,对于这项工作,我们必须对每组进行多次观察。

library(dplyr)
library(ggplot2)

df <- data.frame(
  Name = rep(c("A", "B", "C"), each = 2),  # unique pairs (two observations per group)
  Category = c("x", "y", "x", "y", "x", "y"),
  N = c(500, 540, 470, 500, 480, 520)
  )

df %>% 
  arrange(N) %>%
  mutate(Name = factor(Name, levels = unique(Name))) %>% 
    ggplot(aes(x = Name, y = N)) +
      geom_line(aes(group = Name), colour = "grey", alpha = 1) +  # feeding Name to the group aesthetic
      geom_point(aes(color = Category)) +
      geom_point(shape = 1,size = 2,colour = "black") +
      scale_colour_manual(values = c("green", "cyan")) +
      theme(
        axis.title.x = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        panel.background = element_rect(fill = "white"),
        legend.position = "none")

每对两个观察

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

以重复方式将ggplot2 :: geom_point()与ggplot2 :: geom_line()连接

无法在 r 中使用 ggplot2 将组合 geom_point 和 _line 图的颜色和线型图例分开

ggplot2:合并geom_line,geom_point和geom_bar的图例

使用facet_grid和多个数据点(R ggplot2)时,将geom_point和geom_line组合

ggplot2:将geom_line和geom_point组合在一起的图形中的坐标图例

ggplot2:如何将geom_line和geom_point的图层与错误栏组合在一起

ggplot2中带有geom_line和geom_point的图例

ggplot2-结合了geom_point和geom_line的图例

结合ggplot geom_point和geom_line图产生的冗余图例

ggplot:两组的geom_line和geom_point重叠顺序?

使用geom_point()和geom_line()进行多个系列的ggplot中的图例错误

将gganimate与geom_point和geom_line一起使用

将多个geom_line添加到ggplot

ggplot:遍历geom_line

如何将geom_bar,geom_point和geom_line合并为一个图形?

添加geom_line以链接盒状图中所有geom_point(条件为ggplot2)

将 geom_line 和 geom_point 与 ggplotly 一起使用时的奇怪图例和工具提示文本

ggplot方面和条件geom_point

ggplot,条件填充 geom_point

ggplot,基于变量的geom_point颜色

将高斯拟合到ggplot2中的数据geom_point

将单个分类 geom_point 间隔标签更改为希腊表达式 ggplot

geom_line 的 ggplot 渐变颜色

用ggplot曲线geom_line()

ggplot geom_line 情节问题

通过数据将geom_line()顺序调整为ggplot函数

抖动geom_line和geom_point

添加geom_point和geom_line进行绘图

动画geom_line()崩溃为geom_point()