强调绘图中的线段范围 – 仅限闭区间

huangapple go评论130阅读模式
英文:

Accentuate range of line in plot - only closed interval

问题

I understand your request. Here's the translated portion:

我想突出 ggplot 线图中的一个封闭区域。这是一个示例:

  1. df = data.frame(
  2. x = c(1,2,3,4,5,1,2,3,4,5),
  3. y = c(1,2,1,2,1,3,4,3,4,3),
  4. se = 0.1,
  5. cond = c(F,T,T,F,F,F,F,T,T,F),
  6. g = c('M','M','M','M','M','F','F','F','F','F')
  7. )
  8. ggplot(df, aes(x=x, y=y, alpha = cond, color = g, group = g)) +
  9. geom_errorbar(aes(ymin=y-2*se, ymax=y+2*se), width=.01) +
  10. geom_line() +
  11. geom_point() +
  12. scale_alpha_manual(values = c(TRUE = 1, FALSE = .3))

这将产生以下图表:

强调绘图中的线段范围 – 仅限闭区间

我只想突出 cond == T 的数据点之间的区域,而不是之后的线元素。

如果我尝试以下操作,突出显示是正确的,但线的不同部分不再正确连接:

  1. ggplot(df, aes(x=x, y=y, alpha = cond, color = g)) +
  2. geom_errorbar(aes(ymin=y-2*se, ymax=y+2*se), width=.01) +
  3. geom_line() +
  4. geom_point() +
  5. scale_alpha_manual(values = c(TRUE = 1, FALSE = .3))

强调绘图中的线段范围 – 仅限闭区间

英文:

I would like to accentuate a closed range in a ggplot line plot. Here is an example:

  1. df = data.frame(
  2. x = c(1,2,3,4,5,1,2,3,4,5),
  3. y = c(1,2,1,2,1,3,4,3,4,3),
  4. se = 0.1,
  5. cond = c(F,T,T,F,F,F,F,T,T,F),
  6. g = c('M','M','M','M','M','F','F','F','F','F')
  7. )
  8. ggplot(df, aes(x=x, y=y, alpha = cond, color = g, group = g)) +
  9. geom_errorbar(aes(ymin=y-2*se, ymax=y+2*se), width=.01) +
  10. geom_line() +
  11. geom_point() +
  12. scale_alpha_manual(values = c(`TRUE` = 1, `FALSE` = .3))

This produces the following plot:

强调绘图中的线段范围 – 仅限闭区间

I would like only the region between the data points with cond == T to be highlighted, not the line element afterwards.

If I try the following, the highlighting is correct, but the different parts of the lines are not properly connected anymore:

  1. ggplot(df, aes(x=x, y=y, alpha = cond, color = g)) +
  2. geom_errorbar(aes(ymin=y-2*se, ymax=y+2*se), width=.01) +
  3. geom_line() +
  4. geom_point() +
  5. scale_alpha_manual(values = c(`TRUE` = 1, `FALSE` = .3))

强调绘图中的线段范围 – 仅限闭区间

答案1

得分: 0

你可以创建一个新变量,只有当cond的值和后续值都为TRUE时,它才为TRUE。我们可以使用dplyr::lead()来实现这一点。然后,在ggplot::geom_line()的调用中,不再使用cond作为alpha参数,而是使用新创建的between_cond

  1. df <-
  2. df %>%
  3. mutate(between_cond = cond & lead(cond))
  4. ggplot(df, aes(x=x, y=y, alpha = cond, color = g, group = g)) +
  5. geom_errorbar(aes(ymin=y-2*se, ymax=y+2*se), width=.01) +
  6. geom_line(aes(alpha = between_cond)) +
  7. geom_point() +
  8. scale_alpha_manual(values = c(`TRUE` = 1, `FALSE` = .3))

强调绘图中的线段范围 – 仅限闭区间

英文:

You can create a new variable that is TRUE only when the the value for cond
and following value
are TRUE. We can use dplyr::lead() for this. Then instead of using cond for
the alpha argument we use the newly created between_cond in the ggplot::geom_line()
call.

  1. df &lt;-
  2. df |&gt;
  3. mutate(between_cond = cond &amp; lead(cond))
  4. ggplot(df, aes(x=x, y=y, alpha = cond, color = g, group = g)) +
  5. geom_errorbar(aes(ymin=y-2*se, ymax=y+2*se), width=.01) +
  6. geom_line(aes(alpha = between_cond)) +
  7. geom_point() +
  8. scale_alpha_manual(values = c(`TRUE` = 1, `FALSE` = .3))

强调绘图中的线段范围 – 仅限闭区间<!-- -->

huangapple
  • 本文由 发表于 2023年6月26日 22:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定