【问题标题】:seq function with stat_smooth带有 stat_smooth 的 seq 函数
【发布时间】:2021-06-15 15:08:46
【问题描述】:

我有一个包含 3 个变量的融化 data.frame。

      rn      variable       value
1     1 mhits_L$fhits 0.002262443
2     2 mhits_L$fhits 0.000000000
3     3 mhits_L$fhits 0.000000000
4     4 mhits_L$fhits 0.002262443
5     5 mhits_L$fhits 0.002262443
6     6 mhits_L$fhits 0.000000000
7     7 mhits_L$fhits 0.000000000
8     8 mhits_L$fhits 0.002262443
9     9 mhits_L$fhits 0.000000000
10   10 mhits_L$fhits 0.000000000



     rn      variable       value
1     1 mhits_H$fhits 0.000000000
2     2 mhits_H$fhits 0.000000000
3     3 mhits_H$fhits 0.000000000
4     4 mhits_H$fhits 0.000000000
5     5 mhits_H$fhits 0.004273504
6     6 mhits_H$fhits 0.000000000
7     7 mhits_H$fhits 0.000000000
8     8 mhits_H$fhits 0.000000000
9     9 mhits_H$fhits 0.000000000
10   10 mhits_H$fhits 0.004273504



    rn       variable       value
1     1 mhits_VH$fhits 0.000000000
2     2 mhits_VH$fhits 0.000000000
3     3 mhits_VH$fhits 0.000000000
4     4 mhits_VH$fhits 0.004291845
5     5 mhits_VH$fhits 0.004291845
6     6 mhits_VH$fhits 0.004291845
7     7 mhits_VH$fhits 0.000000000
8     8 mhits_VH$fhits 0.004291845
9     9 mhits_VH$fhits 0.000000000
10   10 mhits_VH$fhits 0.000000000

我想使用 seq(rn) 绘制平滑图:

ggplot(aes(x = seq(rn), y = value, color = variable)) + 
  stat_smooth(aes(y = value), position = "identity", method = "loess", span = 0.1,se = FALSE)

显示图(如图所示),其中每个变量都是分开的,而不是一个图,所有三个都具有相同的 x 轴 0-550。基本上所有三个都必须叠加在 0-550 的相同 seq(rn) 上。我错过了什么? plot

【问题讨论】:

  • 您希望通过seq(rn) 完成什么?您的问题中也没有显示图像。请编辑并重新发布,以便我们了解发生了什么。
  • 抱歉,情节已添加。没有seq(),smooth就不行,因为点太多了?打开其他选项而不是 seq() - 但是使用 seq,每个变量单独看起来都是我想要的方式。
  • 看起来您只是显示了一小部分数据,因此很难找出问题所在。你能分享data %>% group_by(variable) %>% summarize_all(range)的输出吗?

标签: r ggplot2 seq


【解决方案1】:

不确定您的数据存在问题,但这里有一个示例,其中包含一些可能有帮助的模拟数据。

library(tidyverse)

# set siulation size
n <- 100

# simulate some data
data1 <-
  data.frame(
    rn = rep(1:n, times = 3),
    variable = rep(LETTERS[1:3], each = n),
    value = c(1:n * (1 + runif(n, 0, 1)),
              1:n * (1 + runif(n, 0, 2)),
              1:n * (1 + runif(n, 0, 3)))
  )

# plot with seq
data1 %>% 
  ggplot(aes(seq(rn), value, color = variable)) +
  geom_point() +
  geom_smooth(span = 0.2, se=F)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

# plot without seq
data1 %>% 
  ggplot(aes(rn, value, color = variable)) +
  geom_point() +
  geom_smooth(span = .2, se=F)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

reprex package (v2.0.0) 于 2021-06-15 创建

【讨论】:

  • 感谢您指出 - 我编辑了问题以包含其他变量的前 10 行。所有 3 个的 rn 从 1-550 变化,但所有数据看起来都相同。没有 seq - 看起来像这样 (imgur.com/a/k6hTgqo),我不希望它是 facet_wrap()。
  • 你为什么认为这张图是错误的?根据您目前所展示的内容,我认为这实际上是合理的。
  • 我希望它看起来像这样imgur.com/a/xXNQ46q。通过旋转数据而不是熔化来解决。不知道为什么效果更好。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
相关资源
最近更新 更多