【问题标题】:plotting lines in ggplot I get x-axis length errors在ggplot中绘制线我得到x轴长度错误
【发布时间】:2018-10-28 05:06:57
【问题描述】:

我无法使用 ggplot 在另一个之上绘制 7 个时间序列。为什么这个可重现的代码不起作用? signal 是一个因子变量,有 7 个值,跨越 700 个值(每个 100 个值),但不知何故,如果我将 aes() 中的 x 更改为 1,这些值只会绘制:700。我希望每个信号从 1 绘制到 100。为什么没有发生这种情况?

signal_to_noise_ratio = 10
t=seq(0.1,10,0.1)

df <- data.frame(truesignal = sin(t))
df2 <- df

for (i in seq(5)) {
  noise = rnorm(t)
  k <- sqrt(var(t)/(signal_to_noise_ratio*var(noise)))
  data_wNoise = t + k*noise
  df2[,i] = sin(data_wNoise)
}
df[,2:6] = df2
df[,2:7] = rowSums(df2)

colnames(df) <- c("truesignal", "noisy1", "noisy2", "noisy3", "noisy4", "noisy5",
                  "stacked")
melt_df <- melt(df,measure.vars = 1:7, variable.name=c("signal"))

ggplot(data=melt_df,
       aes(x=t,y=value,colour=factor(signal))) +
  geom_path() +
  facet_grid(signal~.)

【问题讨论】:

    标签: r ggplot2 signal-processing facet


    【解决方案1】:

    你可能想要一个 id 变量之类的东西。

    melt_df$t.2 <- rep(1:100, 7)
    
    library(ggplot2)
    ggplot(data=melt_df,
           aes(x=t.2, y=value, colour=factor(signal))) +
      geom_path() +
      facet_grid(signal~.)
    

    产量:

    【讨论】:

    • 我真的认为它是隐含的,但我想我必须为 ggplot 拼写出来。谢谢。
    • 显然。你也可以df$t.2 &lt;- as.integer(rownames(df));也许这样更直观。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多