R编程ggvis histogram verses hist

系统教程 行业动态 更新时间:2024-06-14 16:58:29
R编程ggvis histogram verses hist - 如何调整桶的大小,并定义X轴间距(刻度)(R programming ggvis histogram verses hist - How to size the buckets, and define X axis spacing (ticks))

我正在学习使用ggvis,并希望了解如何创建由hist生成的等效直方图。 具体来说,如何在ggvis直方图中设置bin的宽度以及x的上下界? 我错过了什么?

问题: 如何获得ggvis直方图输出以匹配hist输出?

让我举一个例子:

require(psych) require(RCurl) require(ggvis) if ( !exists("impact") ) { url <- "https://dl.dropboxusercontent.com/u/8272421/stat/stat_one.txt" myCsv <- getURL(url, ssl.verifypeer = FALSE) impact <- read.csv(textConnection(myCsv), sep = "\t") impact$subject <- factor(impact$subject) } describe(impact) hist(impact$verbal_memory_baseline, main = "Distribution of verbal memory baseline scores", xlab = "score", ylab = "frequency")

好吧,让我们尝试用ggvis重现...输出不匹配...

impact %>% ggvis( x = ~verbal_memory_baseline, fill := "white") %>% layer_histograms(width = 5) %>% add_axis("x", title = "score") %>% add_axis("y", title = "frequency")

如何获取ggvis输出以匹配hist输出?


> sessionInfo() R version 3.2.2 (2015-08-14) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X 10.11.2 (El Capitan) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] psych_1.5.6 knitr_1.11 ggvis_0.4.2.9000 setwidth_1.0-4 colorout_1.1-1 vimcom_1.2-3 loaded via a namespace (and not attached): [1] Rcpp_0.12.0 digest_0.6.8 dplyr_0.4.3.9000 assertthat_0.1 mime_0.3 [6] R6_2.1.1 jsonlite_0.9.16 xtable_1.7-4 DBI_0.3.1 magrittr_1.5 [11] lazyeval_0.1.10.9000 rstudioapi_0.3.1 rmarkdown_0.7 tools_3.2.2 shiny_0.12.2 [16] httpuv_1.3.3 yaml_2.1.13 parallel_3.2.2 rsconnect_0.4.1.4 mnormt_1.5-3 [21] htmltools_0.2.6

I am learning to use ggvis and wanted to understand how to create the equivalent histogram to that produced by hist. Specifically, how do you set the bin widths and upper and lower bounds of x in ggvis histograms? What am I missing?

Question: How do I get the ggvis histogram output to match the hist output?

Let me provide an example:

require(psych) require(RCurl) require(ggvis) if ( !exists("impact") ) { url <- "https://dl.dropboxusercontent.com/u/8272421/stat/stat_one.txt" myCsv <- getURL(url, ssl.verifypeer = FALSE) impact <- read.csv(textConnection(myCsv), sep = "\t") impact$subject <- factor(impact$subject) } describe(impact) hist(impact$verbal_memory_baseline, main = "Distribution of verbal memory baseline scores", xlab = "score", ylab = "frequency")

Ok, lets try and reproduce with ggvis... the output does not match...

impact %>% ggvis( x = ~verbal_memory_baseline, fill := "white") %>% layer_histograms(width = 5) %>% add_axis("x", title = "score") %>% add_axis("y", title = "frequency")

How do I get the ggvis output to match the hist output?


> sessionInfo() R version 3.2.2 (2015-08-14) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X 10.11.2 (El Capitan) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] psych_1.5.6 knitr_1.11 ggvis_0.4.2.9000 setwidth_1.0-4 colorout_1.1-1 vimcom_1.2-3 loaded via a namespace (and not attached): [1] Rcpp_0.12.0 digest_0.6.8 dplyr_0.4.3.9000 assertthat_0.1 mime_0.3 [6] R6_2.1.1 jsonlite_0.9.16 xtable_1.7-4 DBI_0.3.1 magrittr_1.5 [11] lazyeval_0.1.10.9000 rstudioapi_0.3.1 rmarkdown_0.7 tools_3.2.2 shiny_0.12.2 [16] httpuv_1.3.3 yaml_2.1.13 parallel_3.2.2 rsconnect_0.4.1.4 mnormt_1.5-3 [21] htmltools_0.2.6

最满意答案

尝试

impact %>% ggvis( x = ~verbal_memory_baseline, fill := "white") %>% layer_histograms(width = 5, boundary = 5) %>% add_axis("y", title = "frequency") %>% add_axis("x", title = "score", ticks = 5)

这使:

在此处输入图像描述


关于boundary和center如何工作,官方文档有点神秘。 看看DataCamp的如何用R中的ggvis制作直方图

width参数已经将bin宽度设置为5,但是bin在哪里开始,它们在哪里结束? 您可以使用center或boundary参数。 center应该引用其中一个bin的中心值,它自动确定其他bin位置。 boundary参数指定其中一个bin的边界值。 同样,指定单个值可修复所有箱的位置。 由于这两个参数以不同的方式指定相同的东西,因此您应该最多设置一个center或boundary 。


如果你想使用center而不是boundary尝试相同的结果:

impact %>% ggvis( x = ~verbal_memory_baseline, fill := "white") %>% layer_histograms(width = 5, center = 77.5) %>% add_axis("y", title = "frequency") %>% add_axis("x", title = "score", ticks = 5)

在这里指定一个箱子的中心(77.5),它会自动确定所有其他箱子

Try

impact %>% ggvis( x = ~verbal_memory_baseline, fill := "white") %>% layer_histograms(width = 5, boundary = 5) %>% add_axis("y", title = "frequency") %>% add_axis("x", title = "score", ticks = 5)

Which gives:

enter image description here


The official documentation is a bit cryptic about how boundary and center works. Have a look at DataCamp's How to Make a Histogram with ggvis in R

The width argument already set the bin width to 5, but where do bins start and where do they end? You can use the center or boundary argument for this. center should refer to one of the bins’ center value, which automatically determines the other bins location. The boundary argument specifies the boundary value of one of the bins. Here again, specifying a single value fixes the location of all bins. As these two arguments specify the same thing in a different way, you should set at most one of center or boundary.


If you want the same result using center instead of boundary try:

impact %>% ggvis( x = ~verbal_memory_baseline, fill := "white") %>% layer_histograms(width = 5, center = 77.5) %>% add_axis("y", title = "frequency") %>% add_axis("x", title = "score", ticks = 5)

Here you specify the center of a bin (77.5) and it determines all the others automatically

更多推荐

本文发布于:2023-04-15 03:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/f2e212dd747b1b4138a2cf0438c4d6bd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:histogram   ggvis   hist   verses

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!