[英]How to replace underscores with spaces in ggplot2 legend?
我输入的 csv 文件(“genus_counts.csv”)的前几行如下所示
Sample,Woeseia,Candidatus_Nitrosopumilus,Nitrospira,Nitrospina,Pseudahrensia,AqS1,Salinicola,Pir4_lineage,Subgroup_10,BD1-7_clade,Sva0996_marine_group,Anoxybacillus,Others,Unclassified
BW1,1.73959,0.474433,0,1.15973,0,0,3.32103,0,0,0,0,8.69794,27.464423,57.1429
BW2,0.424628,0.679406,0,0,0,0,9.95754,0,0.191083,0,1.18896,0,35.7749522,51.7834
我打算以此为基础制作堆积条形图。 我尝试了下面的 R 代码,但图例上的名称仍然带有下划线。 我引入了行pcm %>% rename_all(~gsub("_", " ", .))
以便用空格替换所有下划线,但图例没有任何变化(下划线保留在 plot 上。)。 这里的任何帮助将不胜感激。
library(ggplot2)
library(reshape2)
pc = read.csv("genus_counts.csv", header = TRUE)
pcm = melt(pc, id = c("Sample"))
pcm$Sample <- factor(pcm$Sample,levels=unique(pcm$Sample))
pcm %>% rename_all(~gsub("_", " ", .))
mx = ggplot(pcm, aes(x = Sample, fill = variable, y = value)) +
geom_bar(stat = "identity", colour = "black") +
theme(axis.text.x = element_text(angle = 90, size = 14, colour = "black", vjust = 0.5, hjust = 1, face= "bold"),
axis.title.y = element_text(size = 16, face = "bold"), legend.title = element_text(size = 16, face = "bold"),
legend.text = element_text(size = 12, face = "bold", colour = "black"),
axis.text.y = element_text(colour = "black", size = 12, face = "bold")) +
scale_y_continuous(expand = c(0,0)) +
labs(x = "", y = "Relative Abundance (%)", fill = "Taxon") +
scale_fill_viridis_d(option="plasma")
mx
rename_all
用于更改function的列名。 melt
dataframe后,变量不再是列名,而是variable
列的值。
因此,您可以在melt
步骤之前使用rename_all
,也可以在熔化 dataframe 之后替换mutate
语句中的值。
library(dplyr)
library(ggplot2)
pcm %>%
mutate(variable = gsub("_", " ", variable)) %>%
ggplot(aes(x = Sample, fill = variable, y = value)) +
geom_bar(stat = "identity", colour = "black") +
theme(axis.text.x = element_text(angle = 90, size = 14,
colour = "black", vjust = 0.5, hjust = 1, face= "bold"),
axis.title.y = element_text(size = 16, face = "bold"),
legend.title = element_text(size = 16, face = "bold"),
legend.text = element_text(size = 12, face = "bold", colour = "black"),
axis.text.y = element_text(colour = "black", size = 12, face = "bold")) +
scale_y_continuous(expand = c(0,0)) +
labs(x = "", y = "Relative Abundance (%)", fill = "Taxon") +
scale_fill_viridis_d(option="plasma")
实现所需结果的第二个选项是将 function 传递给 scale 的labels
参数,用空格替换下划线,即labels = ~ gsub("_", " ", .x)
:
library(ggplot2)
library(reshape2)
pcm <- melt(pc, id = c("Sample"))
pcm$Sample <- factor(pcm$Sample, levels = unique(pcm$Sample))
ggplot(pcm, aes(x = Sample, fill = variable, y = value)) +
geom_bar(stat = "identity", colour = "black") +
theme(
axis.text.x = element_text(angle = 90, size = 14, colour = "black", vjust = 0.5, hjust = 1, face = "bold"),
axis.title.y = element_text(size = 16, face = "bold"), legend.title = element_text(size = 16, face = "bold"),
legend.text = element_text(size = 12, face = "bold", colour = "black"),
axis.text.y = element_text(colour = "black", size = 12, face = "bold")
) +
scale_y_continuous(expand = c(0, 0)) +
labs(x = "", y = "Relative Abundance (%)", fill = "Taxon") +
scale_fill_viridis_d(option = "plasma", labels = ~ gsub("_", " ", .x))
数据
pc <- structure(list(
Sample = c("BW1", "BW2"), Woeseia = c(
1.73959,
0.424628
), Candidatus_Nitrosopumilus = c(0.474433, 0.679406),
Nitrospira = c(0L, 0L), Nitrospina = c(1.15973, 0), Pseudahrensia = c(
0L,
0L
), AqS1 = c(0L, 0L), Salinicola = c(3.32103, 9.95754),
Pir4_lineage = c(0L, 0L), Subgroup_10 = c(0, 0.191083), BD1.7_clade = c(
0L,
0L
), Sva0996_marine_group = c(0, 1.18896), Anoxybacillus = c(
8.69794,
0
), Others = c(27.464423, 35.7749522), Unclassified = c(
57.1429,
51.7834
)
), class = "data.frame", row.names = c(NA, -2L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.