Simulate some data for drug and ctrl:
normal_data=sapply(seq(1:3), function(x) x=rnorm(10,6,1))
tumor_data=sapply(seq(1:3), function(x) x=rnorm(10,10,2))
data=cbind(normal_data, tumor_data)
genes=paste("gene",seq(1:10), sep="")
row.names(data)=genes
colnames(data)=c(paste0("ctrl_",seq(1:3)), paste0("drug_",seq(1:3)))
condition=rep(c("ctrl","drug"), each=3)
Do PCA on dataframe:
pca_data=prcomp(t(data))
Do some calculation (% variance covered by component to show on X and Y axis for PC1 and PC2)
pca_data_perc=round(100*pca_data$sdev^2/sum(pca_data$sdev^2),1)
Create dataframe with PC1 and PC2 with some metadata:
df_pca_data=data.frame(PC1 = pca_data$x[,1], PC2 = pca_data$x[,2], sample = colnames(data), condition=condition)
Plot using ggplot2 (color by each sample):
library(ggplot2)
ggplot(df_pca_data, aes(PC1,PC2, color = sample))+
geom_point(size=8)+
labs(x=paste0("PC1 (",pca_data_perc[1],")"), y=paste0("PC2 (",pca_data_perc[2],")"))
Plot using ggplot2 (color by each group):
ggplot(df_pca_data, aes(PC1,PC2, color = condition))+
geom_point(size=8)+
labs(x=paste0("PC1 (",pca_data_perc[1],")"), y=paste0("PC2 (",pca_data_perc[2],")"))
Follow @Kevin's guides in:
PCA plot from read count matrix from RNA-Seq
PCA in a RNA seq analysis
Thanks!
We cannot see the resulting plot. could you edit your question.
Anyway if you have a dataframe as :
where group is either "CTLR" or "DRUG". You can do :
salvatore.raieli2 : You appear to have linked the same image in both locations.
I edited, do you visualized the two images correctly? the first image is a link for the dataset image.
Looks good now.
I tryed to do this:
I have applied this in my case:
if I do not put groups = x I have this PCA results, but they are not colored as different samples:
otherwise I get this error:
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric.
How I can solve this?
Please post your example data as data (not as image).
colmeans(as.numeric(rownames(x))
should work assuming that row. names are numbers stored as chars.salvatore.raieli2 : Please use
ADD COMMENT/ADD REPLY
when responding to existing posts to keep threads logically organized.Submit Answer
should only be used for new answers for the original question.