gpt4 book ai didi

r - ggplot2: theme(legend.title) 忽略 vjust 和 hjust 参数

转载 作者:行者123 更新时间:2023-11-30 23:54:29 25 4
gpt4 key购买 nike

从下面的代码来看,theme(legend.title) 中的参数 hjustvjust 没有做任何事情(至少在我的机器)。两个 ggplot() 调用生成的两个图是相同的。我测试了 theme_bw() 是否是罪魁祸首,但事实并非如此。移动图例标题的解决方案是什么?

library(ggplot2)
library(reshape)
library(RColorBrewer)
test <- structure(list(names = structure(c(1L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 2L, 3L), .Label = c("Spec1", "Spec10", "Spec11", "Spec2",
"Spec3", "Spec4", "Spec5", "Spec6", "Spec7", "Spec8", "Spec9"
), class = "factor"), A = c(0.217031528, 0.370637045, 0.152473461,
0.08340091, 0.094257483, 0.00619633, 0.043205056, 0.017717884,
0.004354587, 0.000349229, 0.010376486), B = c(0.312070285, 0.311236549,
0.139193608, 0.07284637, 0.097903335, 0.003568091, 0.028477238,
0.021042976, 0.004963161, 0.000374577, 0.00832381), C = c(0.281876963,
0.326092831, 0.152957419, 0.07237009, 0.10259602, 0.004158686,
0.026662316, 0.020823785, 0.004313649, 0.00037274, 0.007775501
), D = c(0.275453548, 0.337083347, 0.154469202, 0.070573145,
0.099172727, 0.005437018, 0.02768352, 0.018713749, 0.003950163,
0.000362092, 0.00710149), E = c(0.278508956, 0.334527205, 0.154663425,
0.068355587, 0.101934925, 0.005645608, 0.025961027, 0.019175166,
0.004090645, 0.000386265, 0.00675119), F = c(0.306981913, 0.328928827,
0.147765154, 0.058429727, 0.094863912, 0.003795248, 0.024415702,
0.02349575, 0.003980418, 0.000353114, 0.006990233)), .Names = c("names",
"A", "B", "C", "D", "E", "F"), class = "data.frame", row.names = c(NA,
-11L))

n1 <- as.vector(test$names)
test.melt <- melt(test, id.vars="names")
names(test.melt) <- c("Species", "Treatment", "Abundance")

colourCount <- length(unique(test$names))
getPalette <- colorRampPalette(brewer.pal(colourCount, "Set3"))

ggplot() +
geom_bar(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species), stat="identity", colour="black") +
scale_fill_manual(values= getPalette(colourCount), breaks=n1) +
xlab("Treatments") +
ylab("Relative Abundance") +
ggtitle("Some Title") +
theme_bw() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
legend.title = element_text(size=16, face="bold", vjust=20, hjust=10),
plot.title = element_text(size=20, vjust = 2, face="bold"))

ggplot() +
geom_bar(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species), stat="identity", colour="black") +
scale_fill_manual(values= getPalette(colourCount), breaks=n1) +
xlab("Treatments") +
ylab("Relative Abundance") +
ggtitle("Some Title") +
theme_bw() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
legend.title = element_text(size=16, face="bold", vjust=2, hjust=1),
plot.title = element_text(size=20, vjust = 2, face="bold"))

最佳答案

我不知道最近是否修复了这个问题,但解决方案是在 theme() 中使用 legend.title.align

以上面的例子为例

ggplot() + 
geom_bar(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species),
stat="identity", colour="black") +
scale_fill_manual(values= getPalette(colourCount), breaks=n1) +
xlab("Treatments") +
ylab("Relative Abundance") +
ggtitle("Some Title") +
theme_bw() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
legend.title = element_text(size=16, face="bold"),
legend.title.align=-30, #<<- here
plot.title = element_text(size=20, vjust = 2, face="bold"))

plot

关于r - ggplot2: theme(legend.title) 忽略 vjust 和 hjust 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com