text: user9788: I'm a biologist, not a programmer so please be gentle.
  So I have a dataset that looks like
  Genes  Patient1   Patient2   Patient3
  A          324      433         343
  B          431       342        124
  Z          232       234        267
  
  then I have the sample sheet where it contains sample info like:
  
  Patient1 - Healthy
  Patient2 - Disease
  Patient3 - Healthy 
  (110 samples)
  
  I am using:
  library(ggfortify)
  df <- dataset
  pca_res <- prcomp(df, scale. = TRUE)
  
  autoplot(pca_res)
  
  Then I want to do
  autoplot(pca_res, data = ?, colour = '?')
  
  I wish to use the info from the sample sheet to colour my PCA based on the state (healthy/disease) using the autoplot function. Is there a way to do this?
  
  user650: I've found this the way to work with autoplot (ggfortify's prcomp autoplot) - Use a df that has the field you need for color as the first column (to make things easy), then do:
  autoplot(prcomp(df[,-1], data=df, color='column1_name')
  
  If the color-by field is not the first column, do:
  autoplot(prcomp(df[,!(colnames(df) == "color_by_column_name")], data = df, color = "color_by_column_name")
  
  EDIT:
  That being said, you're going to need to process your data so patients are along rows instead of column. Transpose your dataset so rows are columns and columns become rows; change patient ID to rownames and Gene name to colnames, then merge the patient category dataset with the expression dataset. Once you merge, your dataset should look like:
           Category    A      B      Z
  Patient1 Healthy     324    431    232
  Patient2 Disease     433    342    124
  Patient3 Healthy     343    124    267
  
  And then,
  autoplot(prcomp(df[,!(colnames(df) == "Category")]), data = df, color = "Category")
  
  
  user8919: Can't say I've ever used autoplot, but this is fairly easy to achieve using "base" ggplot.
  One thing autoplot does is put the axis on the same scale reflecting the percentage variation captured by each PC - I think this is a good thing, but most PCA plots you'll see don't do this.
  As you haven't given us any info on your metadata, I'll just make a few assumptions - lets make it a data.frame called "colData" with a "Status" column.
  prcomp stores the PC scores in "pca_res$x"
  # generate some metadata
  colData <- data.frame(row.names=rownames(pca_res$x),status=as.factor(sample(c("diseased","healthy"),nrow(pca_res$x),replace = T)))
  
  # get % variation for each PC 
  pca_res$percentVar <- pca_res$sdev^2/sum(pca_res$sdev^2)
  
  # change scale to reflect % variation
  d <- t(data.frame(t(pca_res$x)*pca_res$percentVar))
  
  # merge metadata and modified PC scores by row.names (by=0)
  d <- merge(d,colData,by=0) 
  
  # make the base plot (PC1 vs PC2)
  g <- ggplot(d,aes(x=PC1,y=PC2,colour=status))
  # add the points
  g <- g + geom_point(size=2)
  # add a "wow" factor to the points
  geom_point(size=4,alpha=0.5)
  # display the plot
  g 
  
  There may be a way to do this with autoplot - e.g.
  autoplot(pca_res,colour=as.numeric(colData$status))
  
  gets you part of the way there, but I think using the standard ggplot syntax is easy enough anyway.
  
original_text: 9788: <p>I'm a biologist, not a programmer so please be gentle.</p>
  <p>So I have a dataset that looks like</p>
  <pre><code>Genes  Patient1   Patient2   Patient3
  A          324      433         343
  B          431       342        124
  Z          232       234        267
  </code></pre>
  <p>then I have the sample sheet where it contains sample info like:</p>
  <hr />
  <pre><code>Patient1 - Healthy
  Patient2 - Disease
  Patient3 - Healthy 
  (110 samples)
  </code></pre>
  <p>I am using:</p>
  <pre><code>library(ggfortify)
  df &lt;- dataset
  pca_res &lt;- prcomp(df, scale. = TRUE)
  
  autoplot(pca_res)
  </code></pre>
  <p>Then I want to do</p>
  <pre><code>autoplot(pca_res, data = ?, colour = '?')
  </code></pre>
  <p>I wish to use the info from the sample sheet to colour my PCA based on the state (healthy/disease) using the autoplot function. Is there a way to do this?</p>
   650: <p>I've found this the way to work with autoplot (ggfortify's prcomp autoplot) - Use a df that has the field you need for color as the first column (to make things easy), then do:</p>
  <pre><code>autoplot(prcomp(df[,-1], data=df, color='column1_name')
  </code></pre>
  <p>If the color-by field is not the first column, do:</p>
  <pre><code>autoplot(prcomp(df[,!(colnames(df) == &quot;color_by_column_name&quot;)], data = df, color = &quot;color_by_column_name&quot;)
  </code></pre>
  <p>EDIT:</p>
  <p>That being said, you're going to need to process your data so patients are along rows instead of column. Transpose your dataset so rows are columns and columns become rows; change patient ID to rownames and Gene name to colnames, then <code>merge</code> the patient category dataset with the expression dataset. Once you merge, your dataset should look like:</p>
  <pre><code>         Category    A      B      Z
  Patient1 Healthy     324    431    232
  Patient2 Disease     433    342    124
  Patient3 Healthy     343    124    267
  </code></pre>
  <p>And then,</p>
  <pre><code>autoplot(prcomp(df[,!(colnames(df) == &quot;Category&quot;)]), data = df, color = &quot;Category&quot;)
  </code></pre>
   8919: <p>Can't say I've ever used autoplot, but this is fairly easy to achieve using &quot;base&quot; ggplot.</p>
  <p>One thing autoplot does is put the axis on the same scale reflecting the percentage variation captured by each PC - I think this is a good thing, but most PCA plots you'll see don't do this.</p>
  <p>As you haven't given us any info on your metadata, I'll just make a few assumptions - lets make it a data.frame called &quot;colData&quot; with a &quot;Status&quot; column.</p>
  <p>prcomp stores the PC scores in &quot;pca_res$x&quot;</p>
  <pre><code># generate some metadata
  colData &lt;- data.frame(row.names=rownames(pca_res<span class="math-container">$x),status=as.factor(sample(c("diseased","healthy"),nrow(pca_res$</span>x),replace = T)))
  
  # get % variation for each PC 
  pca_res<span class="math-container">$percentVar &lt;- pca_res$</span>sdev^2/sum(pca_res$sdev^2)
  
  # change scale to reflect % variation
  d &lt;- t(data.frame(t(pca_res<span class="math-container">$x)*pca_res$</span>percentVar))
  
  # merge metadata and modified PC scores by row.names (by=0)
  d &lt;- merge(d,colData,by=0) 
  
  # make the base plot (PC1 vs PC2)
  g &lt;- ggplot(d,aes(x=PC1,y=PC2,colour=status))
  # add the points
  g &lt;- g + geom_point(size=2)
  # add a &quot;wow&quot; factor to the points
  geom_point(size=4,alpha=0.5)
  # display the plot
  g 
  </code></pre>
  <p>There may be a way to do this with autoplot - e.g.</p>
  <pre><code>autoplot(pca_res,colour=as.numeric(colData$status))
  </code></pre>
  <p>gets you part of the way there, but I think using the standard ggplot syntax is easy enough anyway.</p>
  