【问题标题】:plot histogram from a vector by ggplot2 in r通过 ggplot2 在 r 中从向量绘制直方图
【发布时间】:2018-04-03 12:50:44
【问题描述】:

我想通过 ggplot 2 从向量中绘制直方图。 数据集是数据集包中的河流

rivers
  [1]  735  320  325  392  524  450 1459  135  465  600  330  336  280  315  870  906  202  329
 [19]  290 1000  600  505 1450  840 1243  890  350  407  286  280  525  720  390  250  327  230
 [37]  265  850  210  630  260  230  360  730  600  306  390  420  291  710  340  217  281  352
 [55]  259  250  470  680  570  350  300  560  900  625  332 2348 1171 3710 2315 2533  780  280
 [73]  410  460  260  255  431  350  760  618  338  981 1306  500  696  605  250  411 1054  735
 [91]  233  435  490  310  460  383  375 1270  545  445 1885  380  300  380  377  425  276  210
[109]  800  420  350  360  538 1100 1205  314  237  610  360  540 1038  424  310  300  444  301
[127]  268  620  215  652  900  525  246  360  529  500  720  270  430  671 1770

起初我尝试了这些但没有用:

> ggplot(rivers,aes(rivers))+geom_histogram()
Error: ggplot2 doesn't know how to deal with data of class numeric
> ggplot(rivers)+geom_histogram(aes(rivers))
Error: ggplot2 doesn't know how to deal with data of class numeric

然后我找到a similar question 并发现我可以通过以下方式实现我的目标:

ggplot()+aes(rivers)+geom_histogram()
or
ggplot()+geom_histogram(aes(rivers))

我阅读了 ggplot 帮助文档并有以下问题:

  • 为什么我在 ggplot() 或 geom_histogram() 中声明数据集时出现错误,例如 ggplot(data=rivers)?帮助文档指示默认情况下将向量强制转换为数据框,并且必须指定数据集。我的假设是,当未指定数据集时,该函数将搜索全局环境?
  • 为什么当我单独调用 aes(rivers) 或在 geom_histogram() 中调用时它会起作用,但是当我将它放入 gglot() 时出现错误。为什么在这种情况下 aes(rivers) 的位置很重要?
    ggplot(aes(rivers))+geom_histogram()
    Error: ggplot2 doesn't know how to deal with data of class uneval
    

【问题讨论】:

  • 看起来你有一个vector。将其转换为data.frame,然后使用ggplot

标签: r ggplot2


【解决方案1】:

如果您没有显式地为ggplot 提供参数名称,则将值分配给错误的ggplot 参数(按顺序排列的第一个是data arg)。参数顺序可以在相关函数帮助页面查看:

?ggplot 

ggplot(data = NULL, mapping = aes(), ..., environment = parent.frame())

因此,当您向 ggplot 提供 aes() 而不指定参数名称时,就像您执行以下操作一样:

ggplot(data = aes(rivers)) + geom_histogram()

因为data 参数不允许这种数据类型 - 你会得到一个错误。 提供正确的参数名称可以解决问题:

ggplot(mapping = aes(rivers)) + geom_histogram()

【讨论】:

    【解决方案2】:

    错误原因是riversvector

    ggplot(aes(rivers))+
                   geom_histogram()
    

    错误:ggplot2 不知道如何处理 uneval 类的数据。做过 您不小心将aes() 的结果提供给data 论据?

    将其转换为data.frame 然后就可以了

    library(ggplot2)
    library(dplyr)
    data_frame(val = rivers) %>%
              ggplot(., aes(val)) + 
                    geom_histogram()
    

    数据

    set.seed(24)
    rivers <- sample(700:1700, 150)
    

    【讨论】:

    • 谢谢~但是为什么即使没有将向量转换为数据框,它也可以通过 ggplot()+geom_histogram(aes(rivers)) 工作?
    • @Yin 在此方法中,您正在初始化一个骨架 ggplot 对象,riversaes 中用作向量。但是,在另一种情况下,如果您调用ggplot(rivers) +,它不会转换为data.frame,尽管它说data - Default dataset to use for plot. If not already a data.frame, will be converted to one by fortify() 我不确定它是否是一个错误
    • 再问一个问题~在这种情况下,为什么不能在ggplot()中调用美学映射?但是可以独立调用还是在geom_histogram()中调用? ^ ^
    • @Yin 如果你看?ggplot,有多种调用方式。 ggplot() 选项是创建一个骨架,里面的参数应该是data,然后是aes。在这里,datavector 并且由于某种原因它不是 fortifieddata.frame 尽管文档声称不同
    • @Yin,您过于依赖您实际上不知道的参数顺序。阅读预期的顺序(ggplotgeom_* 的顺序不同),或使用命名参数。那里有很多例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 2012-06-01
    • 1970-01-01
    相关资源
    最近更新 更多