首页 > 解决方案 > 使用 facet_wrap() 在每个面板中独立放置文本后,如何更改 R 中的文本颜色?

问题描述

这是一个数据

Genotype<- rep(c("CV1","CV2"),each=3)
treatment<-rep(c("T1","T2","T3"),2)
RATIO<- c(0.8,0.7,0.9,1.1,1.0,0.8)
DataA<- data.frame(Genotype,treatment,RATIO)

这是一个使用 facet_wrap() 的图

A_labels <- data.frame(Genotype=c("CV1", "CV1", "CV1"),
                             perc= c("NA", "NA","PASS"), treatment=c("T1", "T2", "T3"),
                             RATIO=c(1.0, 0.90, 1.0))

B_labels <- data.frame(Genotype=c("CV2", "CV2", "CV2"),
                       perc= c("TEST", "PASS","FAIL"), treatment=c("T1", "T2", "T3"),
                       RATIO=c(1.2, 1.1, 1.1))

ggplot(data=DataA, aes(x=treatment, y=RATIO, fill=treatment)) +
  geom_bar(stat="identity",position="dodge", width = 0.7) + 
  scale_fill_manual(values= c ("Dark gray","Dark blue", "Dark red")) +
  scale_y_continuous(breaks = seq(0,1.5,0.2), labels = scales::percent, limits = c(0,1.5)) +
  
  geom_label(data=A_labels, aes(x=treatment, y=RATIO, label=perc, col=treatment), size=6, fill="white") +
  geom_label(data=B_labels, aes(x=treatment, y=RATIO, label=perc, col=treatment), size=6, fill="white") +
  scale_color_manual(values= c ("Blue","Red", "Blue")) +
  
  labs(x="treatment", y="Ratio (%)") +
  facet_wrap(~Genotype) + 
  theme(axis.title = element_text (face = "plain", size = 20, color = "black"),
        axis.text.x = element_text(size= 18),
        axis.text.y = element_text(size= 18),
        axis.line = element_line(size = 0.5, colour = "black"),
        legend.position = 'none')+
  windows(width=9, height=5)   

在此处输入图像描述

使用 facet_wrap() 在每个面板中独立放置文本是成功的,但我不知道如何在每个面板中独立放置不同的颜色。例如,在右侧面板中,我想将文本颜色更改为绿色、黄色和橙色。

由于scale_color_manual(values= c ("Blue","Red", "Blue"))每个处理的代码文本颜色在两个面板中是相同的。

您能否让我知道如何在每个面板中独立更改每个处理的文本颜色?

谢谢,

标签: rggplot2facet-wrapgeom-text

解决方案


我从上面 Ben 的建议中找到了解决这个问题的方法。首先,我们在 2 个因素之间进行一个组合,这个组合将有 6 个级别。(2 基因型 x 3 处理)。然后,在 scale_color_manual 中,我们可以指定每种颜色大约 6 个级别!!

#Loading the data and creating the the labels
Genotype<- rep(c("CV1","CV2"),each=3)
treatment<-rep(c("T1","T2","T3"),2)
COMBI<- c("CV1T1","CV1T2","CV1T3","CV2T1","CV2T2","CV2T3")
RATIO<- c(0.8,0.7,0.9,1.1,1.0,0.8)
DataA<- data.frame(Genotype,treatment,COMBI, RATIO)

A_labels <- data.frame(Genotype=c("CV1", "CV1", "CV1"),
                       perc= c("NA", "NA","PASS"), treatment=c("T1", "T2", "T3"),
                       RATIO=c(1.0, 0.90, 1.0))

B_labels <- data.frame(Genotype=c("CV2", "CV2", "CV2"),
                       perc= c("TEST", "PASS","FAIL"), treatment=c("T1", "T2", "T3"),
                       RATIO=c(1.2, 1.1, 1.1))

#GGPLOT
ggplot(data=DataA, aes(x=treatment, y=RATIO, fill=treatment)) +
  geom_bar(stat="identity",position="dodge", width = 0.7) + 
  scale_fill_manual(values= c ("Dark gray","Dark blue", "Dark red")) +
  scale_y_continuous(breaks = seq(0,1.5,0.2), labels = scales::percent, limits = c(0,1.5)) +
  
  geom_label(data=A_labels, aes(x=treatment, y=RATIO, label=perc, col=subset(DataA, Genotype=="CV1")$COMBI), size=6, fill="white") +
  geom_label(data=B_labels, aes(x=treatment, y=RATIO, label=perc, col=subset(DataA, Genotype=="CV2")$COMBI), size=6, fill="white") +
  scale_color_manual(values= c ("Blue","Red","Blue","Green","Yellow", "Orange")) +
  labs(x="treatment", y="Ratio (%)") +
  facet_wrap(~Genotype) + 
  theme(axis.title = element_text (face = "plain", size = 20, color = "black"),
        axis.text.x = element_text(size= 18),
        axis.text.y = element_text(size= 18),
        axis.line = element_line(size = 0.5, colour = "black"),
        legend.position = 'none')+
  windows(width=9, height=5)   

在此处输入图像描述


推荐阅读