ML33M
ML33M

Reputation: 415

R: ggplot to visualize all variables in each cluster after cluster analysis

Sorry in advance if the post isn't clear. So I have my dataframe, 74 observations and 43 columns. I performed cluster analysis on them. I then got 5 clusters, and assigned the cluster number to each respective row. Now, my df has 74 rows (obs) and 44 variables. And I would like to plot and see in each cluster what variables are enriched and what variables are not, for all variables.

I want to achieve this by ggplot. My imaginary output panel is to have 5 boxplots per row, and 42 rows plots, each row will describe a variable measured in the dataset.

Example of the dataset (sorry its very big so I made an example, actual values are different)

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')

So the code did generate a small panel of the graphs that fit my imaginary output. But I can see only 5 rows instead of 42.

So going back to new_df, the last 3 columns draw my attention:

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
 ....

So it seems the cluster number and count column is correct whereas the cytokine measured just kept repeating the 5 variable names, instead of the total 42 variables I want to see.

I think the table conversion step is wrong, but I dont quite know what went wrong and how to fix it.

Please enlighten me.

Upvotes: 0

Views: 681

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

We can try this, I simulate something that looks like your data frame:

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)

Then melt:

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")

enter image description here

You can save it as a bigger plot to look at:

ggsave(g,file="plot.pdf",width=15,height=15)

Upvotes: 1

Related Questions