【问题标题】:How to apply histogram on dependent data in R?如何在R中的依赖数据上应用直方图?
【发布时间】:2016-11-18 13:36:25
【问题描述】:

我想通过 R. 欢迎ggplot2 方法和任何其他方法! 伪代码

  • N11.1、...、N32.1 组的第二列和第三列直方图

代码

        N11.1 N22.1 N33.1 N44.1 N21.1 N31.1 N32.1
Sinus     1.0   0.0   0.0   0.0   0.0   0.0  12.0
Arr/AHB   1.0   0.0   0.0   0.1   0.0   0.0  20.9
        N11.1 N22.1 N33.1 N44.1 N21.1 N31.1 N32.1
Sinus     1.0   0.0   0.0   0.0   0.0   0.0   4.0
Arr/AHB   1.0   0.0   0.0   0.0   0.0   0.0  24.0

第一列有row.names。用数据编码

library("ggplot2")

data.female <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0", 
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("", 
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L, 
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(2:3, .Label = c("", 
"0.0", "0.1", "0.2", "N44"), class = "factor"), N21.1 = structure(c(2L, 
2L), .Label = c("", "0.0", "N21"), class = "factor"), N31.1 = structure(c(2L, 
2L), .Label = c("", "0.0", "N31"), class = "factor"), N32.1 = structure(c(5L, 
7L), .Label = c("", "0.0", "10.8", "11.0", "12.0", "17.0", "20.9", 
"22.8", "24.0", "3.0", "4.0", "44.0", "N32"), class = "factor")), .Names = c("N11.1", 
"N22.1", "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus", 
"Arr/AHB"), class = "data.frame")

data.male <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0", 
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("", 
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L, 
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(c(2L, 
2L), .Label = c("", "0.0", "0.1", "0.2", "N44"), class = "factor"), 
    N21.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N21"), class = "factor"), 
    N31.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N31"), class = "factor"), 
    N32.1 = structure(c(11L, 9L), .Label = c("", "0.0", "10.8", 
    "11.0", "12.0", "17.0", "20.9", "22.8", "24.0", "3.0", "4.0", 
    "44.0", "N32"), class = "factor")), .Names = c("N11.1", "N22.1", 
"N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus", 
"Arr/AHB"), class = "data.frame")

尝试单个数据行

data.female.sinus <- data.female[1:1,1:7]
print(data.female.sinus)

g <- ggplot(data.female.sinus)
g + geom_bar()
#Warning messages:
#1: In min(x, na.rm = na.rm) :
#  no non-missing arguments to min; returning Inf
#2: In max(x, na.rm = na.rm) :
#  no non-missing arguments to max; returning -Inf
#3: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf
#4: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
#5: Computation failed in `stat_count()`:
#arguments imply differing number of rows: 0, 1 
#null device 

预期输出:男性和女性比较的直方图,强调 Arr/AHB 是因变量

测试 hhh 的 answer

我不明白为什么你不能使用带有列名的给定数据,比如没有列名

Sinus <- c(1,0,0,0,0,0,12)
ArrAHB <- c(1,0,0,0.1,0,0,20.9)
# Things work with this data  

Sinus <- data.female[1, 1:7]
ArrAHB <- data.female[2, 1:7]
# Things do not work with this data which has column names

Labels <- c("N11.1","N22.2","N33.1","N44.1","N21.1","N31.1","N32.1")
ID <- c("Sinus","Arr/AHB")
data.female <- data.frame(Sinus,ArrAHB,row.names=Labels)
data.female <- t(data.female)

barchart(data.female,auto.key=list(space='right'))

R:3.3.1
操作系统:Debian 8.5

【问题讨论】:

  • 问题不是很清楚。您正在寻找ggplot2 解决方案?
  • @drmariod 是或任何其他也可以。一些基准会很棒。
  • 你能提供一个可重现的例子吗?您当前的数据。* 是所有因素。这可能是我的密集问题,但是您能否创建一个示例图(可以手写)来说明结果的样子?
  • @roman 等 y 轴的正弦值和 x 轴的列名。
  • 所以基本上你正在寻找geom_bar with stat = "identity"?

标签: r ggplot2 histogram


【解决方案1】:

您的数据如下所示:

> data.female
        N11.1 N22.1 N33.1 N44.1 N21.1 N31.1 N32.1
Sinus     1.0   0.0   0.0   0.0   0.0   0.0  12.0
Arr/AHB   1.0   0.0   0.0   0.1   0.0   0.0  20.9
> data.male
        N11.1 N22.1 N33.1 N44.1 N21.1 N31.1 N32.1
Sinus     1.0   0.0   0.0   0.0   0.0   0.0   4.0
Arr/AHB   1.0   0.0   0.0   0.0   0.0   0.0  24.0

并且您想在多列上绘制每一行的直方图(如here),所以下面的演示。

1. Sinus 和 ArrAHB 组分开的每一行的直方图

您想为 Sinus 和 Arr/AHB 创建一个通用标识符,因此我们为此创建一个新的 ID 列。我们使用这种方法here 和 lattice pkg。

require(lattice)
Sinus<-c(1,0,0,0,0,0,12)
ArrAHB<-c(1,0,0,0.1,0,0,20.9)
Labels<-c("N11.1","N22.1","N33.1","N44.1","N21.1","N31.1","N32.1")
ID<-c("Sinus","Arr/AHB")
data.female<-data.frame(Sinus,ArrAHB,row.names=Labels)
data.female<-as.data.frame(t(data.female))
data.female$ID<-ID

barchart(N11.1+N22.1+N33.1+N44.1+N21.1+N31.1+N32.1 ~ ID,
         data=data.female,
         auto.key=list(space='right')
         )

相比之下,这是人类的图表:

1.2.您的因子数据必须转换为向量或更好:将原始文件直接读入向量,而不是因子!

您的输入数据格式错误为因子数据,这里很糟糕,这可能是误用 read.csv 的结果,例如缺少 hte 标志 na.strings="." 或某些格式错误的元素。更多:

"Sometimes when a data frame is read directly from a file, a column you’d thought would produce a numeric vector instead produces a factor. This is caused by a non-numeric value in the column, often a missing value encoded in a special way like . or -. To remedy the situation, coerce the vector from a factor to a character vector, and then from a character to a double vector. (Be sure to check for missing values after this process.) Of course, a much better plan is to discover what caused the problem in the first place and fix that; using the na.strings argument to read.csv() is often a good place to start.*

为了使用这种格式错误的数据,必须将因子元素转换为数值。类命令揭示了您在将原始数据读入 R 时的错误,因此

> class(data.female$N22.1)
[1] "factor"
> as.double(as.character(data.female$N22.1))
[1] 0 0

as.double(as.character(...)) 允许再次正确操作数据对象。所以代码

require(lattice)
data.female <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0", "1.0", "N11"), class = "factor"),
                              N22.1 = structure(c(2L, 2L), .Label = c("", "0.0", "2.0", "N22"), class = "factor"),
                              N33.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N33"), class = "factor"),
                              N44.1 = structure(2:3, .Label = c("", "0.0", "0.1", "0.2", "N44"), class = "factor"),
                              N21.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N21"), class = "factor"),
                              N31.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N31"), class = "factor"),
                              N32.1 = structure(c(5L, 7L), .Label = c("", "0.0", "10.8", "11.0", "12.0", "17.0", "20.9", "22.8", "24.0", "3.0", "4.0", "44.0", "N32"),
                                                class = "factor")), .Names = c("N11.1", "N22.1", "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"),
                         row.names = c("Sinus", "Arr/AHB"), class = "data.frame")
data.female$ID<-c("Sinus","Arr/AHB")
data.female<-as.data.frame(data.female)

f<-function(x) as.double(as.character(x))   #factors converted to vectors

barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
         data=data.female,
         auto.key=list(space='right')
         )

函数f 将因子转换为向量,唉,因子是具有类对象和属性值的特殊向量,更多here

您需要自己操作图例的地方。

2。再次显示比例的条形图

数据输入变为可读格式(不是某些CSZ文件的输出):N32.1中的值远大于其他列中的任何其他数据。

require(lattice)
Sinus<-c(1,0,0,0,0,0,12)
ArrAHB<-c(1,0,0,0.1,0,0,20.9)
Labels<-c("N11.1","N22.2","N33.1","N44.1","N21.1","N31.1","N32.1")
ID<-c("Sinus","Arr/AHB")
data.female<-data.frame(Sinus,ArrAHB,row.names=Labels)
data.female<-t(data.female)

barchart(data.female,auto.key=list(space='right'))

> data.female
       N11.1 N22.2 N33.1 N44.1 N21.1 N31.1 N32.1
Sinus      1     0     0   0.0     0     0  12.0
ArrAHB     1     0     0   0.1     0     0  20.9

【讨论】:

  • @Masi 您提供的输入是我不擅长理解的东西,例如 3L 代表双数字,但是对于这个问题,这是完全不必要的。一些CSV文件输出?无论如何,我重新格式化了方法 2 中的输入以显示比例。
  • @Masi 你可以试试 as.data.frame(你的输入数据)。新问题是将结构数据对象重新格式化为适当的数据框。如果卡住,请询问有关格式化的新简洁问题。这里使用的格子和条形图需要一定的格式。我稍后会看这个
  • @Masi 完成,见 1.2。您的输入格式错误,可能是原始数据文件中的一些错误或滥用 read.csv 之类的命令,因此将数值转换为因子。您应该将数据框放在向量中,而不是因素中——更多关于转换here 或我的回答点 1.2。
  • 我认为你可以以某种方式从rownames 中获取ID。我认为这可能是无关紧要的ID&lt;-c("Sinus","Arr/AHB")
猜你喜欢
  • 2020-03-13
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
  • 2014-07-08
  • 2011-04-21
  • 2021-07-27
相关资源
最近更新 更多