【问题标题】:Pipe a single variable (i.e. a vector) to a ggplot?将单个变量(即向量)通过管道传输到 ggplot?
【发布时间】:2020-02-26 10:13:13
【问题描述】:

我们可以看到如何plot a single variable(连同它的索引)。

我们如何通过管道连接到 ggplot?

示例

自从

library(ggplot2)

qplot(seq_along(iris$Sepal.Length), iris$Sepal.Length)

产量

我期待

iris$Sepal.Length %>%  { qplot(seq_along(.), .) }

产生相同的结果。但是

Error: Discrete value supplied to continuous scale

问题

我们如何通过管道将单个变量传递给 ggplot?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    似乎要让它工作,你需要在链中明确地print 它。

    library(magrittr)
    library(ggplot2)
    
    iris$Sepal.Length %>% {print(qplot(seq_along(.), .))}
    

    【讨论】:

    • 它与打印一起工作真的很奇怪,但没有错误,还是只是我?
    • 是的,很奇怪没有print就返回错误。
    【解决方案2】:

    您可以使用以下代码

    library(tidyverse)
    
    iris %>% ggplot(aes(seq_along(Sepal.Length), Sepal.Length))+
      geom_point() + theme_bw()+
      labs(title="Plot of Sepal length",x="Sepal.Length seq", y = "Sepal.Length")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 1970-01-01
      相关资源
      最近更新 更多