将颜色正确传递到具有多层的ggplot2中的手动比例

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

我有一个相对复杂的

ggplot2
图,具有不同的图层:
geom_line
geom_segment
geom_point
geom_text
...

这是我的MWE:

myseq <- "ACGTCAGTCGTAGCTAGCTAGCTGCATTATCGATCGATCGATCGA"
basecolors <- c("green","blue","black","red","magenta","grey")
aacolors <- c("#E60A0A","#E6E600","#145AFF","#FA9600","#3232AA","#00DCDC","#EBEBEB","#0F820F","#C8C8C8","#B45AB4","#8282D2","#DC9682","#BEA06E")
set.seed(123)
xlabels <- unlist(strsplit(myseq, ""))
xbreaks <- 1:length(xlabels)
trace_df <- data.frame(time=xbreaks,
                       A=rnorm(length(xlabels), mean=10, sd=2),
                       C=rnorm(length(xlabels), mean=5, sd=1),
                       G=rnorm(length(xlabels), mean=3, sd=1),
                       T=rnorm(length(xlabels), mean=8, sd=3),
                       call=xlabels)
xcolors <- ifelse(xlabels=="A", basecolors[1],
                  ifelse(xlabels=="C", basecolors[2],
                         ifelse(xlabels=="G", basecolors[3],
                                ifelse(xlabels=="T", basecolors[4],
                                       ifelse(xlabels=="X", basecolors[5], basecolors[6])))))
trace_df <- as.data.frame(tidyr::pivot_longer(trace_df, c(A,C,G,T), names_to="base", values_to="signal"))
#
cdr3_aa <- "VWXYZ"
cdr3_st2 <- 7
cdr3_en2 <- 22
cdr3_call <- unlist(strsplit(cdr3_aa, ""))
aacolors <- c("#E60A0A","#E6E600","#145AFF","#FA9600","#3232AA","#00DCDC","#EBEBEB","#0F820F","#C8C8C8","#B45AB4","#8282D2","#DC9682","#BEA06E")
cdr3_mat <- t(matrix(c("VEE","WEE","XEE","YEE","ZEE","#E60A0A","#E6E600","#145AFF","#FA9600","#0F820F","white","black","white","black","white"), ncol=3))
df_arrows <- data.frame(x=seq(cdr3_st2, cdr3_en2-3, 3) -0.5, xend=seq(cdr3_st2+3, cdr3_en2, 3) -0.5,
                        y=18, yend=18, label=cdr3_mat[1,], color=cdr3_mat[2,], text=cdr3_mat[3,])
##
P <- ggplot2::ggplot() +
  ggplot2::annotate("rect", xmin=xbreaks[cdr3_st2]-0.5, xmax=xbreaks[cdr3_en2]-0.5, ymin=-Inf, ymax=Inf, fill="grey", alpha=0.25) +
  ggplot2::geom_vline(data=df_arrows[-1,], ggplot2::aes(xintercept=x), color="grey", linetype=2, linewidth=0.5) +
  ggplot2::geom_line(data=trace_df, ggplot2::aes(x=time, y=signal, group=base, colour=base), linewidth=0.5) +
  ggplot2::scale_color_manual(values=c(basecolors,aacolors)) +
  ggplot2::scale_x_continuous(breaks=xbreaks, labels=xlabels, expand=c(0,1),
                              sec.axis=ggplot2::sec_axis(~., breaks=xbreaks, labels=xbreaks)) +
  ggplot2::scale_y_continuous(limits=c(0,20)) +
  ggplot2::geom_segment(data=df_arrows, ggplot2::aes(x=x, xend=xend, y=y, yend=yend, color=color), linewidth=8) +
  ggplot2::geom_point(data=df_arrows, ggplot2::aes(x=xend, y=y, color=color), shape=18, size=8) +
  ggplot2::geom_text(data=df_arrows, ggplot2::aes(x=(x+xend)/2, y=y, label=label, color=text), fontface="bold") +
  ggplot2::theme_light() +
  ggplot2::theme(axis.text.x=ggtext::element_markdown(face="bold", size=12, color=xcolors),
                 axis.text.x.top=ggplot2::element_text(size=10, color="grey", angle=45, hjust=0),
                 axis.text.y=ggplot2::element_text(size=12),
                 axis.title=ggplot2::element_blank(),
                 panel.grid.minor.x=ggplot2::element_blank(),
                 panel.grid.minor.y=ggplot2::element_line(linetype=2),
                 legend.position="none")
grDevices::pdf(file="test.pdf", height=3, width=10)
print(P)
grDevices::dev.off()

起初我只是将

basecolors
传递给
scale_color_manual
,但我收到了错误:

手动刻度值不足

现在我尝试的每一种颜色组合都会把一切搞乱。我需要图中线条的颜色与 x 轴中字母的颜色相同(这些是正确的),并且线段及其文本的颜色与

df_arrows
... 中指定的一样。

目前上面的代码会产生这样的结果:

fig

如何正确传递不同的配色方案?谢谢

r ggplot2 colors scale
1个回答
0
投票

您有 4 行(A、C、G、T),因此要覆盖

scale_color_manual
中的默认配色方案,您必须为“values”参数指定 4 种颜色。

你的问题是

df_arrow
中有5种颜色,它们的颜色不同。

目前,仅

ggplot
不允许两个不同的图层使用两种不同的配色方案。但是,
ggnewscales
包允许您拥有多种颜色/填充比例。

basecolors <- c("green","blue","black","red")

library(ggnewscale)

ggplot2::ggplot() +
  ggplot2::annotate("rect", xmin=xbreaks[cdr3_st2]-0.5, xmax=xbreaks[cdr3_en2]-0.5, ymin=-Inf, ymax=Inf, fill="grey", alpha=0.25) +
  ggplot2::geom_vline(data=df_arrows[-1,], ggplot2::aes(xintercept=x), color="grey", linetype=2, linewidth=0.5) +
  ggplot2::geom_line(data=trace_df, ggplot2::aes(x=time, y=signal, group=base, colour=base), linewidth=0.5) +
  ggplot2::scale_color_manual(values=basecolors) +
  ggplot2::scale_x_continuous(breaks=xbreaks, labels=xlabels, expand=c(0,1),
                              sec.axis=ggplot2::sec_axis(~., breaks=xbreaks, labels=xbreaks)) +
  ggplot2::scale_y_continuous(limits=c(0,20)) +

# Add this line
  ggnewscale::new_scale_color() +

  ggplot2::geom_segment(data=df_arrows, ggplot2::aes(x=x, xend=xend, y=y, yend=yend, color=color), linewidth=8) +
  ggplot2::geom_point(data=df_arrows, ggplot2::aes(x=xend, y=y, color=color), shape=18, size=8) +
  ggplot2::geom_text(data=df_arrows, ggplot2::aes(x=(x+xend)/2, y=y, label=label, color=text), fontface="bold") +
  ggplot2::theme_light() +
  ggplot2::theme(axis.text.x=ggtext::element_markdown(face="bold", size=12, color=xcolors),
                 axis.text.x.top=ggplot2::element_text(size=10, color="grey", angle=45, hjust=0),
                 axis.text.y=ggplot2::element_text(size=12),
                 axis.title=ggplot2::element_blank(),
                 panel.grid.minor.x=ggplot2::element_blank(),
                 panel.grid.minor.y=ggplot2::element_line(linetype=2),
                 legend.position="none")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.