本文介绍了如何使用'facet'在GGPLOT中创建多个密度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下即时创建的数据: > (200,平均值= .8),rnorm(200,平均值= .9),rnorm(200,平均值= 1),rnorm(200,平均值= 2),rnorm(200,mean = .3)),rnorm(200,mean = 4),rorm(200,mean = .5))> colnames(df)< -c(w.cancer,w.normal,x.cancer,x.normal,y.cancer,y.normal,z.cancer ,z.normal)> df_log< -log2(df)#忽略NA >的警告头像(df_log) 我想要在一个面板中创建多个图,如下面的草图使用'facet'。 我该怎么办? 解决方案您必须先准备好您的数据。我已经在 data.frame df 中说明了这一点,因为它是一个适当的正态分布。 require(ggplot2) require(reshape2) df $ id< - 1:nrow(df) df.m df.m $ grp1< - factor(gsub(\\ .. * $,,df.m $ variable)) df.m $ grp2 p p p + scale_fill_brewer(palette =Set1) 用 df_log df >你会得到这样的东西: require(ggplot2) require(reshape2) df_log $ id< -1:nrow(df_log) df.m< - melt(df_log,id) df.m $ grp1< - factor(gsub \\ .. * $,,df.m $ variable)) df.m $ grp2< - factor(gsub(。* \\\。,,df.m $ variable)) p p = af(x = value))+ geom_density code> I have the following data created on the fly: > df <- data.frame( cbind(rnorm(200),rnorm(200, mean=.8),rnorm(200, mean=.9),rnorm(200, mean=1),rnorm(200, mean=.2),rnorm(200, mean=.3)),rnorm(200, mean=4),rnorm(200, mean=.5)) > colnames(df) <- c("w.cancer","w.normal","x.cancer","x.normal","y.cancer","y.normal","z.cancer","z.normal") > df_log<-log2(df) # ignore the warning with NA > head(df_log)What I want to do is to create multiple plots in one panel like the sketch below using 'facet'.How can I go about it? 解决方案 You'll have to prepare your data first. I've illustrated this on your data.frame df as it is a proper normal distribution.require(ggplot2)require(reshape2)df$id <- 1:nrow(df)df.m <- melt(df, "id")df.m$grp1 <- factor(gsub("\\..*$", "", df.m$variable))df.m$grp2 <- factor(gsub(".*\\.", "", df.m$variable))p <- ggplot(data = df.m, aes(x=value)) + geom_density(aes(fill=grp2), alpha = 0.4)p <- p + facet_wrap( ~ grp1)p + scale_fill_brewer(palette = "Set1")Doing the same by replacing df with df_log you'd get something like this:require(ggplot2)require(reshape2)df_log$id <- 1:nrow(df_log)df.m <- melt(df_log, "id")df.m$grp1 <- factor(gsub("\\..*$", "", df.m$variable))df.m$grp2 <- factor(gsub(".*\\.", "", df.m$variable))p <- ggplot(data = df.m, aes(x=value)) + geom_density(aes(fill=grp2), alpha = 0.5)p <- p + facet_wrap( ~ grp1)p 这篇关于如何使用'facet'在GGPLOT中创建多个密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 20:59