绘制所有观察结果,但根据不同的组对其进行着色

问题描述 投票:1回答:2

我有一个下面给出的样本数据,样本ID是唯一的,有3组。我需要在'df'中绘制所有观察(行),但是根据组ID('groupid')对它们进行着色。这是我到目前为止所拥有的:

# sample data creation
samples <- paste0("S",c(1:9))
groupid <- c("group1", "group2", "group3")
foo <- data.frame(Samples = samples, Group = rep(groupid, each = 3))

bar <- data.frame()
for(i in 1:length(samples)){
  ran.data <- rnorm(10, 0.5)
  #colnames <- paste0("w",c(1:length(ran.data)))
  for(j in 1:length(ran.data)){
    bar[i,j] <- ran.data[j]
  }
}
df <- cbind(foo, bar)

# ******************
# creating plot data
plotdf <- as.data.frame(t(df))
cols <- as.character(unlist(plotdf[1,]))
plotdf <- plotdf[-c(1,2),] # removing rows
groupid <- df$Group # var to group by
names(plotdf) <- cols
plotdfrows <- names(df[,3:ncol(df)])
plotdf$rownames <- plotdfrows

# melt and plot
library(reshape2)
library(ggplot2)
melteddf <- melt(plotdf, id.var = "rownames")

final.plot <- ggplot(melteddf, aes(rownames, value, colour = variable, group = groupid)) + geom_point() + #geom_line() +
  scale_y_discrete(breaks=seq(-3, 3, by = 0.5)) + scale_x_discrete() + 
  labs(title = paste("Sample plot"))  #breaks=seq(0, 4, by = 0.5)

print(final.plot)

当我使用group = 1时,我设法获得绘图,但观察的颜色不同。但是我在哪里可以指定'groupid'信息?提前致谢。

r ggplot2 reshape2 melt
2个回答
1
投票

除了@ onlyphantom的答案之外,您的代码还存在一些问题。

你有不必要的操纵你的df转换为长格式。请注意,您的原始数据框df具有在操作数据时丢失的列group。更重要的是,如果您查看融化数据框melteddf的结构,您的代码将创建字符值而不是数值:

str(melteddf)
'data.frame':   90 obs. of  3 variables:
$ rownames: chr  "V1" "V2" "V3" "V4" ...
$ variable: Factor w/ 9 levels "S1","S2","S3",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value   : chr  " 0.5705084" " 0.62928774" " 2.2150650" " 0.96091621" ...

您只需要一行代码转换为长格式,并且为了保留您的组ID,您可以将Group变量添加到id.vars

melteddf2 <- melt(df, id.vars=c("Samples", "Group"))

str(melteddf2)
'data.frame':   90 obs. of  4 variables:
$ Samples : Factor w/ 9 levels "S1","S2","S3",..: 1 2 3 4 5 6 7 8 9 1 ...
$ Group   : Factor w/ 3 levels "group1","group2",..: 1 1 1 2 2 2 3 3 3 1 ...
$ variable: Factor w/ 10 levels "V1","V2","V3",..: 1 1 1 1 1 1 1 1 1 2 ...
$ value   : num  0.571 0.611 -0.229 1.378 2.669 ...

head(melteddf2)
Samples  Group variable      value
1      S1 group1       V1  0.5705084
2      S2 group1       V1  0.6106827
3      S3 group1       V1 -0.2288912
4      S4 group2       V1  1.3781335
5      S5 group2       V1  2.6689560
6      S6 group2       V1  1.8686023

最后关于你的ggplot2代码,你的y轴值是连续的,你不应该使用scale_y_discrete,而你的x轴已经是离散的,并且不需要scale_x_discrete。如果要使用aes(colour=Group)定义颜色组,请使用Group

ggplot(melteddf2, aes(x=variable, y=value, colour = Group)) + geom_point() +
  scale_y_continuous(breaks=seq(-3, 3, by = 0.5)) + 
  labs(title = paste("Sample plot"))

2
投票

传递给aes()的值必须是关联数据框中的有效列名。

这是我们要使用的数据:

set.seed(0)
dat <- data.frame(
  rownames=LETTERS[1:25],
  variables=sample(c("S1", "S2", "S3"), 25, replace = TRUE),
  value=runif(25)
)

groupid = sample(c("group1", "group2", "group3"), 25, replace = TRUE)
# assigning group as a new variable to the data we use for plotting
dat$group <- groupid

数据如下所示:

'data.frame':   25 obs. of  4 variables:
 $ rownames : Factor w/ 25 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ variables: Factor w/ 3 levels "S1","S2","S3": 3 1 2 2 3 1 3 3 2 2 ...
 $ value    : num  0.2672 0.3861 0.0134 0.3824 0.8697 ...
 $ group    : chr  "group3" "group2" "group3" "group2" ...

注意group变量如何出现在原始数据中。 ggplot的代码相对简单:

ggplot(dat, aes(x=rownames, y=value, color=group))+
  geom_point()

产生这个:enter image description here

你的代码不起作用的原因是你传递给groupid调用的数据中没有ggplot。您指定melteddf作为数据参数,但groupid数据框中没有melteddf变量。

如果由于某种原因你需要颜色美学(col)来引用不同于你在ggplot2调用中指定的数据帧的数值,你也可以这样做。

以下代码产生相同的结果:

set.seed(0)
dat <- data.frame(
  rownames=LETTERS[1:25],
  variables=sample(c("S1", "S2", "S3"), 25, replace = TRUE),
  value=runif(25)
)
# group is a different data frame from dat
group = data.frame("groupid"=sample(c("group1", "group2", "group3"), 25, replace = TRUE))

ggplot(data=dat, aes(x=rownames, y=value))+
  geom_point(aes(col=group$groupid))
© www.soinside.com 2019 - 2024. All rights reserved.