【问题标题】:ggplot: remove lines at ribbon edgesggplot:删除色带边缘的线条
【发布时间】:2011-10-13 13:42:51
【问题描述】:

我正在使用ggplot 绘制时间进程数据(屏幕上不同对象随时间的固定比例)并希望使用功能区来显示 SE,但功能区本身在顶部和底部边缘有线条,这使得阅读图表有点困难。我一直无法弄清楚如何摆脱那些边缘线。这是我的情节代码:

ggplot(d, aes(Time, y, color = Object, fill = Object)) +
  stat_summary(fun.y = "mean", geom = "line", size = 2) +
  stat_summary(fun.data = "mean_se", geom = "ribbon", alpha = .3)

有什么建议吗?

这是一个最小的工作示例。我已将数据压缩到:

   Time Object          y      lower     upper
 1 1000      C 0.12453389 0.04510504 0.2039627
 2 1000      T 0.58826856 0.37615078 0.8003864
 3 1000      U 0.09437160 0.03278069 0.1559625
 4 1100      C 0.12140127 0.03943988 0.2033627
 5 1100      T 0.64560823 0.44898727 0.8422292
 6 1100      U 0.06725172 0.01584248 0.1186610

d <- structure(list(Time = c(1000L, 1000L, 1000L, 1100L, 1100L, 1100L), Object = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("C", 
"T", "U"), class = "factor"), y = c(0.12453389, 0.58826856, 0.0943716, 
0.12140127, 0.64560823, 0.06725172), lower = c(0.04510504, 0.37615078, 
0.03278069, 0.03943988, 0.44898727, 0.01584248), upper = c(0.2039627, 
0.8003864, 0.1559625, 0.2033627, 0.8422292, 0.118661)), .Names = c("Time", 
"Object", "y", "lower", "upper"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))

这是新的情节代码:

ggplot(d, aes(Time, y, color = Object, fill = Object)) +
  geom_line(size = 2) +
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = .3)

【问题讨论】:

  • 你能构建一个最小的工作示例吗?这意味着您应该添加一些简单的数据来运行您的图形命令。
  • 谢谢,这似乎有所帮助。

标签: r ggplot2


【解决方案1】:

您可以使用colour 参数移除边框:

ggplot(d, aes(Time, y, color = Object, fill = Object)) +
  geom_line(size = 2) +
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = .3, colour = NA)

【讨论】:

  • 当您已经将colour 分配给排序变量时,这不起作用。相反,user443854 的answer 下方的comments 中提到的linetype=0 方法效果很好!
【解决方案2】:

geom_ribbon 了解linetype 审美。如果您想将线型映射到变量,请将其包含在 aes() 参数中,否则,请将 linetype 放在外面并给它 0,如下所示:

ggplot(d, aes(Time, y, color = Object, fill = Object)) +
  geom_line(size = 2) +
  geom_ribbon(aes(ymin = lower, ymax = upper), linetype = 0, alpha = .3)

更多信息在这里:http://docs.ggplot2.org/current/geom_ribbon.html

【讨论】:

  • linetype=NA 对我不起作用,但是,linetype=0linetype='blank' 有效。 ggplot2.tidyverse.org/reference/aes_linetype_size_shape.html
  • 我正在使用 ggplot2_2.2.1 -- 我可以确认 linetype=NA 不再有效(给出错误 invalid line type)。 linetype=0linetype='blank' 目前都可以使用。
【解决方案3】:

ggplot2geom_ribbon() 现在包含一个outline.type 参数,可帮助控制功能区轮廓的显示方式。

大纲类型

library(tidyverse)

huron <- tibble(year = 1875:1972, level = as.vector(LakeHuron))

huron %>%
    ggplot(aes(year, level)) +
    geom_ribbon(aes(ymin = level - 1, ymax = level + 1), 
                fill = "grey70", color = "red", 
                outline.type = "lower") +
    geom_line(aes(y = level))

reprex package (v0.3.0) 于 2020 年 5 月 28 日创建

线型 = 0

或者,按照建议,我们可以设置linetype = 0 来删除所有行。

library(tidyverse)

huron <- tibble(year = 1875:1972, level = as.vector(LakeHuron))

huron %>%
    ggplot(aes(year, level)) +
    geom_ribbon(aes(ymin = level - 1, ymax = level + 1), 
                fill = "grey70", color = "red", linetype = 0) +
    geom_line(aes(y = level))

reprex package (v0.3.0) 于 2020 年 5 月 28 日创建

【讨论】:

    【解决方案4】:

    给你

    ggplot(d, aes(Time, y,  fill=Object)) + 
      geom_line(size=2, aes(colour = Object)) + 
      geom_ribbon(aes(ymin=lower, ymax=upper), alpha=.3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 2016-12-22
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 2023-02-25
      相关资源
      最近更新 更多