首页 > 解决方案 > 如何重命名图例并在 R 中包含调色板

问题描述

我正在尝试更改图例的标签。该图应用了一个调色板(调色板 npg)。但是,当我重命名图例“10^7”、“10^5”时,颜色会重置为默认调色板。

样本数据集:

Concentration Strain Values1  
10000CFU F 0.01  
10000CFU F 0.03  
10000CFU F 0  
10000CFU M3 0.05  
10000CFU M3 0.03  
10000CFU M3 0  
10000CFU M5 0.02  
10000CFU M5 0.02  
10000CFU M5 0.01  
1000000CFU F 0.01  
1000000CFU F 0.02  
1000000CFU F 0.01  
1000000CFU M3 0.05  
1000000CFU M3 0.04  
1000000CFU M3 0.04  
1000000CFU M5 0.12  
1000000CFU M5 0.11  
1000000CFU M5 0.10  
mydata <- read.csv(file.choose(), header=TRUE, sep="\t")

library(ggplot2)
library(ggpubr)


p <- ggboxplot(mydata, x = "Strain",  y = "Values1",palette="npg",fill="Concentration") + stat_compare_means(
    comparisons = list(c("thing1", "thing2")), 
    label = "p.signif", method="t.test") + 
    theme(legend.title=element_blank())  + 
    theme(legend.position="bottom") + labs(title="Title", x = "Type", y = expression('OD'[600])) + scale_fill_discrete(labels=c(expression('10'^7),expression('10'^5)))

print(p)

有没有办法设置调色板以及重命名图例?如果我使用上面的代码,我会得到下图:

添加了图例的图表

但是,我想使用不同的调色板(例如,调色板 npg)。如果我删除代码

scale_fill_discrete(labels=c(expression('10'^7),expression('10'^5)))

我得到了图表: 没有图例的图表,带有调色板

我对将配色方案应用于图表的替代方法持开放态度。但是,我有很多图表并且希望保持一致。

标签: rggplot2

解决方案


推荐阅读