Dot Plots with multiple categories - R

2019-08-12 04:59发布

I'm definitely a neophyte to R for visualizing data, so bear with me.

I'm looking to create side-by-side dot plots of seven categorical samples with many gene expression values corresponding with individual gene names. mydata.csv file looks like the following

B27      B28      B30      B31 LTNP5.IFN.1 LTNP5.IFN.2 LTNP5.IL2.1
1 13800.91 13800.91 13800.91 13800.91    13800.91    13800.91    13800.91
2  6552.52  5488.25  3611.63  6552.52     6552.52     6552.52     6552.52
3  3381.70  1533.46  1917.30  2005.85     3611.63     4267.62     5488.25
4  2985.37  1188.62  1051.96  1362.32     2717.68     2985.37     5016.01
5  1917.30  2862.19  2625.29  2493.26     2428.45     2717.68     4583.02
6   990.69   777.97  1269.05  1017.26     5488.25     5488.25     4267.62

I would like each sample data to be organized in its own dot plot in one graph. Additionally, if I could point out individual data points of interest, that would be great.

Thanks!

标签: r plot ggplot2 dot
2条回答
forever°为你锁心
2楼-- · 2019-08-12 05:09

You can use base R, but you need to convert to matrix first.

dotchart(as.matrix(df))

dotchart by gene

or, we can transpose the matrix to arrange it by sample:

dotchart(t(as.matrix(df)))

dotchart by sample

查看更多
我只想做你的唯一
3楼-- · 2019-08-12 05:22

Considering your [toy] data is stored in a data frame called a:

library(reshape2)
library(ggplot2)
a$trial<-1:dim(a)[1]  # also, nrow(a)
b<-melt(data = a,varnames  = colnames(a)[1:7],id.vars = "trial")
b$variable<-as.factor(b$variable)
ggplot(b,aes(trial,value))+geom_point()+facet_wrap(~variable)

produces

enter image description here

What we did: Loaded required libraries (reshape2 to convert from wide to long and ggplot2 to, well, plot); melted the data into long formmat (more difficult to read, easier to process) and then plotted with ggplot.

I introduced trial to point to each "run" each variable was measured, and so I plotted trial vs value at each level of variable. The facet_wrap part puts each plot into a subplot region determined by variable.

查看更多
登录 后发表回答