R语言 GGPLOT字母颜色

xtfmy6hx  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(144)

我想改变颜色的字母一样的酒吧。我用这个代码

ggplot(df, aes(x=reorder(Genotype,-Phy), y=Phy, fill=Specie, colour=Specie)) + 
  geom_bar(stat="identity", position=position_dodge(), color="black") + 
  geom_errorbar(aes(ymin=Phy, ymax=Phy+sd), width=0.5,position=position_dodge(0.9)) + 
  geom_text(aes(y = Phy+sd+0.02, label = tk), position = position_dodge(width = 0.9),
            vjust=0.3,hjust=0.5, size = 3.5, angle=90) +
  theme(panel.background = element_rect(colour = "black",fill = "white"),
        panel.grid.major = element_line(size = 0.5, linetype = 'solid',colour = "lightgray"),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, colour = "black", face = "bold"),
        axis.text.y =element_text(colour="black", size = "10", face = "bold"),
        legend.position=c(0.915, 0.85),legend.background = element_rect(fill='transparent'),
        legend.key.size = unit(4, 'mm'),
        legend.text = element_text(size = 9, face="italic", colour = "black")) + 
  scale_fill_manual(values=c('#e9002d', '#ffaa00', '#00b000'))+ expand_limits(y=0.68)+
  scale_y_continuous(breaks = seq(0, 1, 0.1)) + labs(title="",y=y, x="")

字符串
得到这个图

gcuhipw9

gcuhipw91#

这个方法可能有帮助吗?我假设你说的“字母”是指变量tk
在缺乏可重复数据的情况下,我拼凑了一个有代表性的框架,只包含了解决这个问题的ggplot参数。你可以调整代码以适应你的用例。
更新以包括按种属显示颜色的误差条。

library(ggplot2)


df1 <- data.frame(species = c("s.ch", "s.co", "s.tu"),
                  genotype = c("BGB451", "BGB460", "BGB093"),
                  phy = c(0.52, 0.35, 0.29),
                  tk = c("a", "bcfg", "cfhij"),
                  sd = c(0.02, 0.06, 0.08))

ggplot(df1, aes(reorder(genotype, -phy), phy, fill = species))+
  geom_col() +
  geom_errorbar(aes(ymin = phy, 
                    ymax = phy + sd,
                    colour = species), width = 0.5) + 
  geom_text(aes(label = tk, colour = species),
            size = 10,
            nudge_y = 0.05, 
            angle = 90) +
  scale_fill_manual(values=c('#e9002d', '#ffaa00', '#00b000')) +
  scale_colour_manual(values=c('#e9002d', '#ffaa00', '#00b000'))

字符串

创建于2023-11-11使用reprex v2.0.2

1zmg4dgp

1zmg4dgp2#

因此,您可以**将颜色指定为element_text的向量(参见here for a non-vectorized example),但它不受支持,并且可能会更改。

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)
df <- data.frame(Name, Age)

ggplot(df) + geom_col(aes(x = Name, y = Age, fill = Name)) + 
theme(axis.text.x = element_text(colour=c("black", "red", "blue", "green", "orange")))

字符串
制作:

如何生成向量取决于你自己。我没有费心去适应你的代码,但你可以按照这个来做。记住ggplot警告你这是不支持的,将来可能会改变,所以你不应该依赖这个在未来的版本中可用或现在是正确的。

相关问题