#### The support.bioconductor.org editor has been updated to markdown! Please see more info at: Tutorial: Updated Support Site Editor

Question: gene expression visualization: lineplot with ggplot2
0
3.7 years ago by
alakatos80
United States
alakatos80 wrote:

Hello,

I am trying to visualize a subset of my gene expression data with "ggplot2" as a lineplot. The x axis represents the subjects and the y axis represents the corresponding gene expression values.  In my  dataset, there are 4 genotype groups (ReA, ReWT, WTA and WTWT,) with 4 biological replicates. I would like to graph the subjects after each other ordered by genotype . Unfortunately, the order of the subject on the x axis is random.  The d.f2 was ordered by "sample"  but it did not make any difference. I also  tried to incorporate the genotype info but it did not work.

In addition, I would like to change the  line colour palette to make the plotted lines  more distinguishable or plot each variable name next to the corresponding line to be discernible.

I am wondering if someone could advise me how to fix this.

Thank you in advance.

Anita

My code:

mat= t(exprs(eSet))
colnames(mat) = fData(eSet)$Symbol rownames(mat) = pData(eSet)$Sample_ID

genotype= pData(eSet)$Genotype "ReA" "ReWT" "ReA" "ReA" "WTA" "WTWT" etc  mat   Cacna1s Ncstn Psen2 Capn2 Grin1 Mme Aph1a Re-A1 6.263089 9.363995 7.043813 8.958579 10.35145 6.737548 8.411508 Re-WT2 6.395183 9.268441 7.061662 8.774474 10.43331 6.849888 8.342589 Re-A3 6.158634 9.303803 6.992950 8.928849 10.24111 6.697132 8.382859 Re-A4 6.317536 9.221448 7.027650 8.898634 10.21116 6.946621 8.292705 WT-A6 6.410811 9.312704 7.100520 8.901446 10.35887 6.375570 8.363781 WT-WT7 6.315270 9.182447 6.935652 8.707210 10.26362 6.978126 8.198171 etc.  sample <- rownames(mat) d.f <- data.frame(sample, mat) d.f2 <- melt(d.f, id.vars = "sample")   sample variable value 1 Re-A1 Cacna1s 6.263089 2 Re-WT2 Cacna1s 6.395183 3 Re-A3 Cacna1s 6.158634 4 Re-A4 Cacna1s 6.317536 5 WT-A6 Cacna1s 6.410811 6 WT-WT7 Cacna1s 6.315270  ggplot(d.f2, aes(sample, value, group = variable, colour = variable)) + geom_line() visualization ggplot2 lineplot • 1.3k views ADD COMMENTlink modified 3.7 years ago by Jeremy Ng180 • written 3.7 years ago by alakatos80 Answer: gene expression visualization: lineplot with ggplot2 1 3.7 years ago by Jeremy Ng180 Singapore Jeremy Ng180 wrote: Hi Anita, The ordering on the x-axis in ggplot is usually done alphabetically. There are two ways which can be used to do this: (1) You need to re-order the factors in the data.frame. This is done as such: d.f2$Sample <- factor(d.f2$Sample,levels=c("WT-A6","WT-WT7","Re-WT2","Re-A1","Re-A3","Re-A4")) (2) You can manually set the axis using scale_x_discrete, as such: ggplot(d.f2, aes(Sample, value, group = variable, colour = variable)) + geom_line()+scale_x_discrete(breaks=c("WT-A6","WT-WT7","Re-WT2","Re-A1","Re-A3","Re-A4")) As for the second question on coloring, you can manually assign colors using scale_color_manual, as such: ggplot(d.f2, aes(Sample, value, group = variable, colour = variable)) + geom_line() + scale_color_manual(values=c("Cacna1s"="red",...)) HTH, Jeremy On Sat, May 30, 2015 at 4:15 AM, alakatos [bioc] <noreply@bioconductor.org> wrote: > Activity on a post you are following on support.bioconductor.org > > User alakatos <https: support.bioconductor.org="" u="" 6983=""/> wrote Question: > gene expression visualization: lineplot with ggplot2 > <https: support.bioconductor.org="" p="" 68182=""/>: > > Hello, > > I am trying to visualize a subset of my gene expression data with* > "ggplot2" as* a lineplot. The x axis represents the subjects and the y > axis represents the corresponding gene expression values. In my dataset, > there are 4 genotype groups (ReA, ReWT, WTA and WTWT,) with 4 biological > replicates. I would like to graph the subjects after each other ordered > by genotype . Unfortunately, the order of the subject on the x axis is > random. The d.f2 was ordered by "sample" but it did not make any > difference. I also tried to incorporate the genotype info but it did not > work. > > In addition, I would like to change the line colour palette to make the > plotted lines more distinguishable or plot each variable name next to the > corresponding line to be discernible. > > I am wondering if someone could advise me how to fix this. > > Thank you in advance. > > Anita > > My code: > > mat= t(exprs(eSet)) > colnames(mat) = fData(eSet)$Symbol > rownames(mat) = pData(eSet)$Sample_ID > > genotype= pData(eSet)$Genotype > > "ReA" "ReWT" "ReA" "ReA" "WTA" "WTWT" etc > > <font style="background-color: rgb(225, 226, 229);">mat</font> > > Cacna1s Ncstn Psen2 Capn2 Grin1 Mme Aph1a > Re-A1 6.263089 9.363995 7.043813 8.958579 10.35145 6.737548 8.411508 > Re-WT2 6.395183 9.268441 7.061662 8.774474 10.43331 6.849888 8.342589 > Re-A3 6.158634 9.303803 6.992950 8.928849 10.24111 6.697132 8.382859 > Re-A4 6.317536 9.221448 7.027650 8.898634 10.21116 6.946621 8.292705 > WT-A6 6.410811 9.312704 7.100520 8.901446 10.35887 6.375570 8.363781 > WT-WT7 6.315270 9.182447 6.935652 8.707210 10.26362 6.978126 8.198171etc. > > sample <- rownames(mat) > d.f <- data.frame(sample, mat) > d.f2 <- melt(d.f, id.vars = "sample") > > sample variable value > 1 Re-A1 Cacna1s 6.263089 > 2 Re-WT2 Cacna1s 6.395183 > 3 Re-A3 Cacna1s 6.158634 > 4 Re-A4 Cacna1s 6.317536 > 5 WT-A6 Cacna1s 6.410811 > 6 WT-WT7 Cacna1s 6.315270 > > > > ggplot(d.f2, aes(sample, value, group = variable, colour = variable)) + geom_line() > > > > > > ------------------------------ > > You may reply via email or visit gene expression visualization: lineplot with ggplot2 >

Thanks Jeremy.

Anita