Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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'中使用稀疏数据按列分组;s ggplot2_R_Plot_Ggplot2 - Fatal编程技术网

如何在R'中使用稀疏数据按列分组;s ggplot2

如何在R'中使用稀疏数据按列分组;s ggplot2,r,plot,ggplot2,R,Plot,Ggplot2,我正在使用R格式的data.frame(df)进行工作 A B G1 G2 G3 1 1 6 0 1 0 2 2 7 0 1 1 3 3 8 1 1 0 4 4 9 0 0 1 5 5 10 1 1 1 A列和B列只是连续的数字列。G1、G2和G3是每行的类别,其中0表示行不在该类别中,1表示行在该类别中。显然,一行可能有多个类别 我想使用ggplot2(geom_point)绘制散点图,这些散点图按G1:G3类别进行颜色编码,或按G1:G3以其他方

我正在使用R格式的data.frame(df)进行工作

  A  B G1 G2 G3
1 1  6  0  1  0
2 2  7  0  1  1
3 3  8  1  1  0
4 4  9  0  0  1
5 5 10  1  1  1
A列和B列只是连续的数字列。G1、G2和G3是每行的类别,其中0表示行不在该类别中,1表示行在该类别中。显然,一行可能有多个类别

我想使用ggplot2(geom_point)绘制散点图,这些散点图按G1:G3类别进行颜色编码,或按G1:G3以其他方式分组。我不知道怎么做。我目前正在使用以下代码绘制A对B:

ggplot(df, aes(x = A, y = B)) + geom_point()

我目前还不知道如何将我的类别合并到G1:G3中。

我建议有两种方法,第一种是简单的情况,代码中唯一缺少的值是将美学颜色设置为一个类别。在本例中,您将为每个类别创建ggplot

set.seed(1)
A <- rnorm (20)
B <- rnorm (20)
G <- matrix(sample(c(0,1), replace=TRUE, 60), 20, 3)

df <- cbind.data.frame(A, B, G)
colnames(df) <- c("A", "B", "G1", "G2", "G3")
head(df)
# A           B G1 G2 G3
# 1 -0.6264538  0.91897737  0  1  1
# 2  0.1836433  0.78213630  1  0  0
# 3 -0.8356286  0.07456498  0  0  0
# 4  1.5952808 -1.98935170  0  1  0
# 5  0.3295078  0.61982575  1  1  1
# 6 -0.8204684 -0.05612874  0  0  0

# Note colour = factor(G1) to get categorical, if colour = G1 get continuous
gg_point <- ggplot(df, aes(x = A, y = B, colour=factor(G1))) + geom_point()
gg_point

# Example on how to edit the legend values
G1_legend <- scale_colour_discrete("G1")
G2_legend <- scale_colour_manual("G1", values=c("blue", "red"), labels=c("non-member", "member"))

gg_point + G1_legend
gg_point + G2_legend

# you can use grid.arrange to plot all categories together once you create them
grid.arrange(gg_point_G1, gg_point_G2, gg_point_G3, ncol=3) # or nrow = 3
set.seed(1)

A您还可以将类别设置为data.table中的factors。然后将“形状”或“颜色”设置为一个因子,ggplot将显示类别:

library(data.table)

a <- c(1,2,3,4,5)
b <- c(6,7,8,9,10)
G1 <- c(0,0,1,0,1)
G2 <- c(1,1,1,0,1)
G3 <- c(0,1,0,1,1)

cc <- data.table(a,b,G1,G2,G3)
cc[,G1 := as.factor(G1)][,G2 := as.factor(G2)][,G3 := as.factor(G3)]

ggplot(cc)+
  geom_point(aes(a, b, color = G1, shape = G2), size = 4)
库(data.table)

a是的,这是一种很好的压缩绘图的替代方法。如果你有有限的类别,你也可以将美学尺寸设置为G3:
ggplot(cc)+geom_point(aes(a,b,color=G1,shape=G2,size=G3))
将所有3个都放在那里…@johncarter-Yup。。使用gridExtra软件包,您可以在一个大网格上绘制多个具有行业单个类别的绘图。有很多可视化选项。
library(data.table)

a <- c(1,2,3,4,5)
b <- c(6,7,8,9,10)
G1 <- c(0,0,1,0,1)
G2 <- c(1,1,1,0,1)
G3 <- c(0,1,0,1,1)

cc <- data.table(a,b,G1,G2,G3)
cc[,G1 := as.factor(G1)][,G2 := as.factor(G2)][,G3 := as.factor(G3)]

ggplot(cc)+
  geom_point(aes(a, b, color = G1, shape = G2), size = 4)