【问题标题】:Overlaying 2 line plots With Factors R用因子 R 覆盖 2 个线图
【发布时间】:2018-07-26 21:55:54
【问题描述】:

我正在尝试使用 R 和闪亮的 Web 应用程序在同一空间上叠加两个折线图。

This is the only example 我可以找到类似的问题,但我看不出它们之间的关系。这也是唯一表现出这个问题的情节;我其他闪亮的应用程序图都很好。

我的代码(相关部分)是

x1 <- c( 10, 25, 19)
x2 <- c( 3, 24, 11)
x3 <- c( 4, 15, 1)
df <- data.frame( "October" = x1, "November" = x2, "August"=x3)
x <- factor(1:3, labels = c("October", "November", "August"))
t1 <- as.numeric(df[1,1:3])
t2 <- as.numeric(df[2,1:3])
plot(t1 ~ x, type="n", ylim=c(min(t1),max(t2)))
lines(t1 ~ x, col="blue")
lines(t2 ~ x, col="red")

但无论我将初始绘图的类型更改为什么,它都会在所有绘图点处为我提供水平线。我最初将它作为type="l",但它做了同样的事情,所以我手动添加了属于那里的行,并尝试使用type="n"使水平条消失。有没有人对这个问题有任何见解。

我更喜欢 base R 中的解决方案,所以不使用 ggplot。

【问题讨论】:

  • 如果你提供一个最小的reprex,帮助会容易得多。我得到:Error: object 'dataframerow1' not found
  • 这是我选择的通用名称,因为对象是数据框的行。不过我会定义它们。
  • 最好让您的示例独立(并且最小化)。这意味着如果有人将您的代码复制并粘贴到 R 终端中,它应该可以正常运行。有一个read on how to provide a minimal reproducible example/attempt
  • 现在应该运行

标签: r plot shiny


【解决方案1】:

因为xfactorplot(t1 ~ x) 实际上会产生一个条形图。您每个月只有一次测量值,所以您看到的只是一条水平线。

您可以执行以下操作:

plot.default(t1 ~ x, type="n", ylim=c(min(t1),max(t2)), xaxt = "n");
axis(1, at = as.numeric(x), labels = levels(x))
lines(t1 ~ x, col="blue")
lines(t2 ~ x, col="red")

【讨论】:

    猜你喜欢
    • 2021-05-03
    • 2015-11-27
    • 1970-01-01
    • 2023-03-28
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多