6

我有一个看起来像这样的数据框:

        Samp1    Samp2    Samp3     Samp4    Samp5
Gene1    84.1     45.2     34.3      54.6     76.2
Gene2    94.2     12.4     68.0      75.3     24.8
Gene3    29.5     10.5     43.2      39.5     45.5
...

我正在尝试创建一个散点图,其中 x 轴是样本(Samp1-5),y 轴是行(Gene1-3 等),但我希望绘制每行的数据作为同一情节上的不同颜色。

关于如何在 R 中执行此操作的任何想法?我非常愿意在 R 中使用 ggplot2、lattice、car 或任何其他包。

4

4 回答 4

2

这是一个解决方案ggplot2

数据:

dat <- read.table(text="Samp1    Samp2    Samp3     Samp4    Samp5
  Gene1    84.1     45.2     34.3      54.6     76.2
  Gene2    94.2     12.4     68.0      75.3     24.8
  Gene3    29.5     10.5     43.2      39.5     45.5", header = TRUE)

剧情:

library(ggplot2)  
ggplot(stack(dat), aes(x = ind, y = values, colour = rownames(dat))) +
  geom_point()

在此处输入图像描述

于 2012-11-20T09:37:36.400 回答
1

如果您想在 lattice 或 ggplot2 中执行此操作,那么您可能需要将数据重塑为长格式,请参阅reshape函数或reshape2包。

对于基本图形,该matplot函数可能会执行您想要的操作,axis如果您不希望仅将数字 1 到 5 作为轴刻度线,则可能需要抑制 x 轴并使用该函数添加您自己的。

于 2012-11-19T22:41:29.430 回答
0

将数据放入矩阵:

foo <- as.matrix(structure(list(Samp1 = c(84.1, 94.2, 29.5),
    Samp2 = c(45.2, 12.4, 10.5),Samp3 = c(34.3, 68, 43.2),
    Samp4 = c(54.6, 75.3, 39.5),Samp5 = c(76.2, 24.8, 45.5)),
  .Names = c("Samp1", "Samp2","Samp3", "Samp4", "Samp5"),
  class = "data.frame", row.names = c("Gene1","Gene2", "Gene3")))

和情节:

plot(seq(1,ncol(foo)),foo[1,],xlab="",ylab="",xaxt="n",
  pch=21,bg=1,ylim=c(min(foo),max(foo)))
axis(side=1,at=seq(1,ncol(foo)),labels=colnames(foo))
for ( ii in 2:nrow(foo) ) points(seq(1,ncol(foo)),foo[ii,],pch=21,col=ii,bg=ii)

请注意,我正在按颜色循环显示它们的数字 ( col=ii,bg=ii)。见?palette

你可能还想看看?legend

散点图

于 2012-11-20T09:26:26.180 回答
0

在引入 tidyverse 之后,推荐的方法是使用 tidyr 将数据转换为长格式。例如

dat <- read.table(text="Gene Samp1    Samp2    Samp3     Samp4    Samp5
  Gene1    84.1     45.2     34.3      54.6     76.2
  Gene2    94.2     12.4     68.0      75.3     24.8
  Gene3    29.5     10.5     43.2      39.5     45.5", header = TRUE)

dat %>% 
  # transform data to long form
  tidyr::gather("sample", "value", contains("Samp")) %>%
  # plot the data
  ggplot(aes(x = sample, y = value, col = Gene)) + geom_point()
于 2019-09-04T08:26:37.460 回答