调整 ggplot 条形图中的 x 轴标签

问题描述 投票:0回答:1

我想移动上方平均线下方的条形标签。

我正在使用这段代码来制作这个图表

horiz <- mean(data$SPAD)
quux <- data %>%
  select(Genotype,Species, SPAD) %>%
  arrange(desc(SPAD)) %>%
  mutate(Genotype = factor(Genotype, levels = Genotype))

ggplot(quux, aes(Genotype, fill=Species)) +
  geom_rect(aes(xmin=as.numeric(Genotype)-0.4, xmax=as.numeric(Genotype) + 0.4, 
                ymin=pmax(horiz, SPAD), ymax=pmin(horiz, SPAD)), colour="black")+
  scale_fill_manual(values=c('#e9002d', '#ffaa00', '#00b000','blue'))+
  geom_errorbar(aes(ymin = SPAD, 
                    ymax = ifelse(SPAD > horiz, SPAD+3.3306, pmin(horiz, SPAD-3.3306))), width = 0.4,
                position = position_dodge(0.9)) + theme_bw()+
  scale_x_discrete() + 
  scale_y_continuous(breaks = seq(6.8, 13.50, 0.75),labels = number_format(accuracy = 0.01))+ 
  geom_hline(yintercept = horiz, linetype = "dashed", color = "black")+
  geom_text(aes(label = Genotype, y = horiz), angle=90, hjust = 1.1) +
  labs(title="a.",y="", x="")+ theme_light()+
  theme(panel.background = element_rect(colour = "black"),
        axis.text.x = element_blank(), 
        axis.ticks.x = element_blank(),
        axis.text.y =element_text(colour="black", size = "10", face = "bold"),
        plot.title = element_text(hjust=0.02,vjust = 1, size = "11", face = "bold"),
        legend.position=c(0.93, 0.9), legend.text = element_text(size=10,face= "italic"))

数据集 |基因型|物种|SPAD| |:-------:|:--------:|:-----:| |BGB003| sp1 |35.5642| |BGB008| sp1 |34.8968| |BGB009| sp1| 32.8186| |BGB027| sp1| 40.6409| |BGB045| sp1| 32.9753| |BGB083| sp2| 34.1282| |BGB086| sp2| 33.824| |BGB088| sp3| 36.9244| |BGB089| sp3| 37.7132| |BGB091| sp3| 43.968| |BGB093| sp3| 39.1745| |BGB096| sp2| 32.1512| |BGB098| sp2| 36.8385| |BGB101| sp2| 42.7797| |BGB102| sp2| 33.7136| |BGB103| sp2| 40.8532| |BGB107| sp2| 39.5082| |BGB109| sp2| 49.1457| |BGB113| sp2| 43.1033| |BGB313| sp4| 44.675| |BGB444| sp2| 33.3934| |BGB451| sp2| 33.4456| |BGB467| sp2| 37.6576| |BGB472| sp2| 35.7361|

r ggplot2 label bar-chart axis-labels
1个回答
0
投票

最后,我向 geom_text() 添加了条件公式 像这样我可以移动标签

 geom_text(aes(label = Genotype, y = ifelse(SPAD > horiz, horiz, pmax(SPAD, horiz+7))),angle=90, hjust = 1.1)

© www.soinside.com 2019 - 2024. All rights reserved.