Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot绘制整个数据集和子集_R_Ggplot2 - Fatal编程技术网

R 使用ggplot绘制整个数据集和子集

R 使用ggplot绘制整个数据集和子集,r,ggplot2,R,Ggplot2,我有一个数据集,按照嵌套的项目符号分为子类别和子类别: -1 -1a -1ai -1aii -1b -1bi ……等等 我想使用ggplot2制作一个点图,显示1的所有数据,然后是1a的数据,然后是1ai的数据,依此类推 示例数据集: x <- data.frame(cat=1, subA=letters[rep(1:5,each=10)], subB=as.character(as.roman(rep(1:5,5,each=2))),value=rnorm(50,20

我有一个数据集,按照嵌套的项目符号分为子类别和子类别:

-1
 -1a
  -1ai
  -1aii
 -1b
  -1bi
……等等

我想使用ggplot2制作一个点图,显示1的所有数据,然后是1a的数据,然后是1ai的数据,依此类推

示例数据集:

x <- data.frame(cat=1, subA=letters[rep(1:5,each=10)], 
subB=as.character(as.roman(rep(1:5,5,each=2))),value=rnorm(50,20,7))

> head(x)
  cat subA subB    value
1   1    a    I 26.75573
2   1    a    I 12.52218
3   1    a   II 24.53499
4   1    a   II 23.21012
5   1    a  III 11.18173
6   1    a  III 25.01914
x头(x)
cat subA subB值
1 a I 26.75573
2 1 a I 12.52218
3 1 a II 24.53499
4 1 a II 23.21012
51A III 11.18173
6 1 a III 25.01914
我想以这样的图表结束:

x.m1 <- x
x.m1$grp <- x$cat
x.m2 <- do.call(rbind, lapply(split(x, interaction(x[, 1:2])), function(y) {
    y$grp <- do.call(paste0, y[, 1:2])
    y
}))
x.m3 <- do.call(rbind, lapply(split(x, interaction(x[, 1:3])), function(y) {
    y$grp <- do.call(paste0, y[, 1:3])
    y
}))

y <- rbind(x.m1, x.m2, x.m3)

ggplot(data = y, aes(x = value, y = grp)) + geom_point(aes(colour=subA, shape=subB))

我能够通过做大量的子集和rbinding来绘制这个图,以生成大量冗余的导数数据帧,但这似乎是一个明显的错误示例

x2 <- with(x,rbind(cbind(key="1",x), 
cbind(key="1 a",x[paste(cat,subA) == "1 a",]), 
cbind(key="1 a I",x[paste(cat,subA,subB) == "1 a I",]), 
cbind(key="1 a II",x[paste(cat,subA,subB) == "1 a II",])))

library(ggplot2)
library(plyr)
ggplot(x2,aes(x=reorder(key,desc(key)),y=value)) 
+ geom_point(position=position_jitter(width=0.1,height=0)) 
+ coord_flip() + scale_x_discrete("Category")

x2除了使用如下所示的单独组重建数据,我想不出其他方法:

x.m1 <- x[c("cat", "value")]
x.m2 <- do.call(rbind, lapply(split(x, interaction(x[, 1:2])), function(y) {
    y$cat <- do.call(paste0, y[, 1:2])
    y[c("cat", "value")]
}))
x.m3 <- do.call(rbind, lapply(split(x, interaction(x[, 1:3])), function(y) {
    y$cat <- do.call(paste0, y[, 1:3])
    y[c("cat", "value")]
}))

y <- rbind(x.m1, x.m2, x.m3)

ggplot(data = y, aes(x = value, y = cat)) + geom_point()

我有点搞不懂你想策划什么。。。但是您可以使用
交互
来获得这种效果<代码>ggplot(x,aes(x=值,y=交互(cat,subA,subB)))+geom_point()
。(另外,您在绘图步骤中调用的函数
desc
对我们大多数人都不可用。它来自何处?交互似乎只在其子类别(即1.a.i、1.a.ii等)内绘制结果。)我想展示价值观在整体分布以及子类别中的位置。哇,这是plyr软件包中的内容。我将编辑这个问题。除了@Arun建议的内容之外……我想指出,使用形状、大小和/或颜色美学可能会产生比您建议的visualizati更干净、更清晰的绘图关于。
ggplot(x,aes(x=value,y=interaction(cat,subA,subB),color=interaction(cat,subA),shape=factor(cat))…
或其他什么。谢谢!我想当我将上面的内容应用于实际数据时,会有意义,但我会记住这一点。