无法垂直显示ggplot2构面面板或自定义geom_bar颜色

时间:2015-11-18 19:46:02

标签: r ggplot2 facet-wrap geom-bar

我在Windows7的数据系统中使用Rstudio输出的“dput(df)”数据:

> dput(df)
structure(list(time = structure(c(5L, 2L, 8L, 4L, 1L, 7L, 3L, 6L), .Label = c("G1", "G1_S", "G2", "G2_M", "G2M_G1", "M", "S", "S_G2"), class = "factor"), RB1_total = c(0.571337, -0.933077, -0.0660497, -0.1121, -0.571337, 0.36174, 0.42779, 0.53989), S.249 = c(0.270073, 0.170127, 0.0912204, 0.100353, -0.270073, -0.4402, -0.53142, -0.631773), T.252 = c(0.434422, -0.749758, -0.58248, NaN, -0.434422, 0.315335, 0.897816, NaN), T.345 = c(NaN, NaN, -0.0607041, NaN, NaN, NaN, 0.0607041, NaN), S.347 = c(NaN, NaN, 0.150658, NaN, NaN, NaN, -0.150658, NaN), T.353 = c(0.484345, NaN, 0.17213, -0.256059, -0.484345, NaN, -0.17213, 0.0839295), T.356 = c(0.489195, -0.312565, -0.0684749, NaN, -0.489195, -0.176629, -0.108154, NaN), S.807 = c(0.286962, NaN, 0.558877, NaN, -0.286962, NaN, -0.558877, NaN), S.816 = c(NaN, NaN, 0.197244, NaN, NaN, NaN, -0.197244, NaN), T.821 = c(0.32722, 0.654879, 0.318102, -0.680306, -0.32722, -0.982098, -1.3002, -0.619894), T.823 = c(0.387246, -0.375746, 0.244286, 0.489502, -0.387246, -0.0115, -0.255786, -0.745288), T.826 = c(-0.349522, 1.08947, 0.578631, 0.588223, 0.349522, -0.739952, -1.31858, -1.90681), S.855 = c(0.237007, -0.890089, NaN, NaN, -0.237007, 0.653083, NaN, NaN)), .Names = c("time", "RB1_total", "S.249", "T.252", "T.345", "S.347", "T.353", "T.356", "S.807", S.816", "T.821", "T.823", "T.826", "S.855"), class = "data.frame", row.names = c(NA, -8L))

我的代码,能够显示所有4个因子“时间”级别的数据,作为每个变量的单行面板(它需要ggplot2,reshape2,RColorBrewer,网格和plyr包):

dftop=df[1:4,]
dfbot=df[5:8,]

dRt=melt(dftop,id=c("time"))
dRt$time <- as.character(dRt$time)
dRt$time <- factor(dRt$time, levels=unique(dRt$time))

dRb=melt(dfbot,id=c("time"))
dRb$time <- as.character(dRb$time)
dRb$time <- factor(dRb$time, levels=unique(dRb$time))


cols<- colorRampPalette(c('black','grey','magenta','cyan','green','orange','red'))(13)

Rbot <- ggplot(dRb, aes(x=time, y=value,colour=variable, group = variable)) + 
  geom_line(size=2) +
  facet_wrap(~ variable, nrow = 1) +
  scale_colour_manual(values=c(cols)) + 
  xlab("Concatenated Sets") + 
  ylab("Log2 Normalized Relative Ratio") +
  geom_point(aes(size=value), show_guide=FALSE) +
  scale_size(guide="none") +
  geom_point(aes(fill=value,size=value-0.5),colour="black",shape=21) +
  geom_hline(yintercept=1, linetype="dotted", color="red", size=0.75) +
  geom_hline(yintercept=-1, linetype="dotted", color="red", size=0.75) +
  geom_hline(yintercept=0, color="black", size=0.5) +
  theme(legend.position="none")
Rbot

result1

上面的图表也可以作为垂直面板,通过简单的换线尝试:

 facet_wrap(variable ~ ., scales = "fixed",ncol = 1)

然而,这导致:     layout_base中的错误(data,vars,drop = drop):至少一个图层必须包含用于构面的所有变量。不清楚如何有效地修改它并得到想要的垂直显示。

最后,最后一位

Rtop <- ggplot(dRt, aes(x=time, y=value, width=.75, fill=variable)) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~ variable, nrow = 1) +
  xlab("Paired Sets") + 
  ylab("Log2 Normalized Relative Ratio") +
  geom_hline(yintercept=0.5, linetype="dotted", color="red", size=0.75) +
  geom_hline(yintercept=-0.5, linetype="dotted", color="red", size=0.75) +
  geom_hline(yintercept=0, color="black", size=0.75) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1),legend.position="none")
Rtop

result2

输出仅显示默认的ggplot2彩虹刻度,而不是采用result1中显示的geom_lines中使用的相同手动配色方案。

RtopVal <- ggplot(dRt, aes(x=time, y=value, width=.75)) + 
  geom_bar(aes(fill=value),stat="identity", position="dodge") +
  facet_wrap(~ variable, nrow = 1) +
  scale_colour_manual(values=c(cols)) + 
  xlab("Paired Sets") + 
  ylab("Log2 Normalized Relative Ratio") +
  geom_hline(yintercept=0.5, linetype="dotted", color="red", size=0.75) +
  geom_hline(yintercept=-0.5, linetype="dotted", color="red", size=0.75) +
  geom_hline(yintercept=0, color="black", size=0.75) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
RtopVal

result3

现在采用的替代方案最接近理想的结果。

感谢您的时间!

0 个答案:

没有答案