【发布时间】: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_barwithstat = "identity"?