小编Ken*_*son的帖子

如何在函数内抑制qplot的binwidth警告?

我正在编写一个qplot()用于绘制直方图的函数,例如,

> library(ggplot2)
> d=rnorm(100)
> myfun=function(x) qplot(x)
Run Code Online (Sandbox Code Playgroud)

运行它会发出警告:

> myfun(d)
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Run Code Online (Sandbox Code Playgroud)

为了抑制警告,我尝试自己计算binwidth,但这会产生错误并且不会绘制:

> myfun=function(x) print(qplot(x, binwidth=diff(range(x))/30))
> myfun(d)
Error in diff(range(x)) : object 'x' not found
Run Code Online (Sandbox Code Playgroud)

我有两个相关的问题:

  • 这里发生了什么?为什么找不到对象'x'?
  • 如何编写函数以便不生成警告?

谢谢!

r ggplot2

10
推荐指数
2
解决办法
6712
查看次数

如何使用计数、百分比和边际总数格式化表格

我经常需要创建一个带有计数、百分比和边际总数的格式化表格。例如,我可能有包含三个类和两个类别的数据。我想为每个类制作一个表格,其中包含每个类别的类内计数和百分比以及该类别的总计数。最后,底部的总计行汇总类别,将类别显示为总计的百分比和总体总计。

代码总是很丑:-),我很想找到更好的方法。这是一个非常简单的例子;通常情况比这复杂得多。

=====  ===  =======  ===  ======  =====
Class  Yes  Yes pct   No  No pct  Total
=====  ===  =======  ===  ======  =====
one     35      65%   19     35%     54
two     21      70%    9     30%     30
three    9      56%    7     44%     16
Total   65      65%   35     35%    100
=====  ===  =======  ===  ======  =====
Run Code Online (Sandbox Code Playgroud)

我知道addmargins(不适用于 data.frame),prop.table(给出一个单独的比例表),descr::CrossTable(将值放在单元格中,而不是分布在行中)。欢迎任何有关如何清理它的建议。

下面是创建上表的代码:

library(formattable) # For nice percents
library(tidyverse)

# Make up some data. Three classes with two categories within each class …
Run Code Online (Sandbox Code Playgroud)

r

6
推荐指数
1
解决办法
2032
查看次数

如何保持旋转格子云散射图的尺度?

背景:我正在尝试使用R的lattice :: cloud()函数为旋转的3D散点图设置动画.我在R中创建了一系列PNG图像,然后从序列中创建一个动画GIF.

问题是绘图的比例随着旋转而变化,因此动画立方体在转动时会变大和缩小.

以下是仅使用两个图表的示例(基于其中一个云示例):

library(lattice)
par.set <-
  list(axis.line = list(col = "transparent"),
     clip = list(panel = "off"))

print(cloud(Sepal.Length ~ Petal.Length * Petal.Width, 
    data = iris, cex = .8, 
    groups = Species, 
    screen = list(z = 0, x = 0, y = 0),
    par.settings = par.set,
    scales = list(col = "black")),
  split = c(1,1,2,1), more = TRUE)
print(cloud(Sepal.Length ~ Petal.Length * Petal.Width,
    data = iris, cex = .8, 
    groups = Species,
    screen = list(z = 0, x = 0, y …
Run Code Online (Sandbox Code Playgroud)

animation r lattice

5
推荐指数
1
解决办法
1471
查看次数

标签 统计

r ×3

animation ×1

ggplot2 ×1

lattice ×1