如果帖子不清楚,请提前向您道歉。所以我有我的数据框架,74个观察值和43列。我对它们进行了聚类分析。然后我得到了5个簇,并将簇号分配给每个相应的行。现在,我的df有74行(obs)和44个变量。我想要绘制并查看每个集群中,对于所有变量,哪些变量是丰富的,哪些变量不是。
我想通过ggplot来实现这一点。我想象的输出面板是每行有5个箱图,42个行图,每行描述一个在数据集中测量的变量。
数据集的示例(对不起,它太大了,所以我做了一个示例,实际值是不同的)
df
ID EGF FGF_2 Eotaxin TGF G_CSF Flt3L GMSF Frac IFNa2 .... Cluster
4300 4.21 139.32 3.10 0 1.81 3.48 1.86 9.51 9.41 .... 1
2345 7.19 233.10 0 1.81 3.48 1.86 9.41 0 11.4 .... 1
4300 4.21 139.32 4.59 0 1.81 3.48 1.86 9.51 9.41 .... 1
....
3457 0.19 233.10 0 1.99 3.48 1.86 9.41 0 20.4 .... 3
5420 4.21 139.32 3.10 0.56 1.81 3.48 1.86 9.51 29.8 .... 1
2334 7.19 233.10 2.68 2.22 3.48 1.86 9.41 0 28.8 .... 5
str(df)
$ ID : Factor w/ 45 levels "4300"..... : 44 8 24 ....
$ EGF : num ....
$ FGF_2 : num ....
$ Eotaxin : num ....
....
$ Cluster : Factor w/ 5 levels "1" , "2"...: 1 1 1.....3 1 5
#now plotting
#thought I pivot the datafram
new_df <- pivot_longer(df[,2:44],df$cluster, names_to = "Cytokine measured", values_to = "count")
#ggplot
ggplot(new_df,aes(x = new_df$cluster, y = new_df$count))+
geom_boxplot(width=0.2,alpha=0.1)+
geom_jitter(width=0.15)+
facet_grid(new_df$`Cytokine measured`~new_df$cluster, scales = 'free')所以代码确实生成了一个小的图形面板,适合我想象的输出。但是我只能看到5行,而不是42行。
因此,回到new_df,最后3列引起了我的注意:
Cluster Cytokine measured count
1 EGF 2.66
1 FGF_2 390.1
1 Eotaxin 6.75
1 TGF 0
1 G_CSF 520
3 EGF 45
5 FGF_2 4
4 Eotaxin 0
1 TGF 0
1 G_CSF 43
....因此,似乎聚类数和计数列是正确的,而测量的细胞因子只是重复5个变量名称,而不是我想要看到的总共42个变量。
我认为表转换步骤是错误的,但我不太知道哪里出了问题,以及如何修复它。
请给我开导一下。
发布于 2020-02-07 06:00:01
我们可以试一下,我模拟一些看起来像你的数据框的东西:
df = data.frame(
ID=1:74,matrix(rnorm(74*43),ncol=43)
)
colnames(df)[-1] = paste0("Measurement",1:43)
df$cluster = cutree(hclust(dist(scale(df[,-1]))),5)
df$cluster = factor(df$cluster)然后融化:
library(ggplot2)
library(tidyr)
library(dplyr)
melted_df = df %>% pivot_longer(-c(cluster,ID),values_to = "count")
g = ggplot(melted_df,aes(x=cluster,y=count,col=cluster)) + geom_boxplot() + facet_wrap(~name,ncol=5,scale="free_y")

您可以将其另存为更大的绘图以供查看:
ggsave(g,file="plot.pdf",width=15,height=15)https://stackoverflow.com/questions/60100643
复制相似问题