【问题标题】:How to fit the axis title with two lines in R?如何在 R 中用两条线拟合轴标题?
【发布时间】:2021-08-31 09:09:34
【问题描述】:

我使用代码ylab(expression(paste()制作了一个带有“两线”y轴标题的图表

Genotype <- c("CV1","CV2","CV1","CV2")
Category<- c("GN","GN","AGW","AGW")
mean <- c(1.47,1.66,0.98,0.93)
se<- c(0.10,0.20,0.03,0.06)
DataA<- data.frame(Genotype,Category,mean,se)
DataA


ggplot(data=DataA, aes(x=Genotype, y=mean, fill=Category))+
  geom_bar(stat="identity",position="dodge", width = 0.7) + 
  geom_errorbar(aes(ymin= mean-se, ymax=mean + se), position=position_dodge(0.7),
                width=0.2, size=1) +
  scale_fill_manual(values= c ("Dark gray", "Cadetblue")) + 
  scale_y_continuous(breaks = seq(0,2,0.2), labels = scales::percent, limits = c(0,2)) +  
  geom_hline(yintercept=1, linetype="dashed", color = "Dark red", size=1.5) + 
  xlab("Cultivar") +
  ylab(expression(paste("Value of thinning treatment \n relative to unthinning treatment"))) +
  theme(axis.title = element_text (face = "plain", size = 15, color = "black"),
        axis.text.x = element_text(size= 15),
        axis.text.y = element_text(size= 15),
        axis.line = element_line(size = 0.5, colour = "black"))+ 
  windows(width=7, height=5)

y 轴标题被分成两行,但整个标题并没有适合图表。即使我调整了图形大小的宽度,它也没有应用于轴标题。

您能告诉我如何将整个 y 轴标题很好地放入图形中,同时保持轴标题的大小(即字体大小 = 15)吗?

谢谢,

【问题讨论】:

    标签: r graph size axis-labels yaxis


    【解决方案1】:

    一种方法是调整边距,为左侧留出更多空间。

    library(ggplot2)
    
    ggplot(data=DataA, aes(x=Genotype, y=mean, fill=Category))+
      geom_bar(stat="identity",position="dodge", width = 0.7) + 
      geom_errorbar(aes(ymin= mean-se, ymax=mean + se), position=position_dodge(0.7),
                    width=0.2, size=1) +
      scale_fill_manual(values= c ("Dark gray", "Cadetblue")) + 
      scale_y_continuous(breaks = seq(0,2,0.2), labels = scales::percent, limits = c(0,2)) +  
      geom_hline(yintercept=1, linetype="dashed", color = "Dark red", size=1.5) + 
      xlab("Cultivar") +
      ylab(expression(paste("Value of thinning treatment \n relative to unthinning treatment"))) +
      theme(axis.title = element_text (face = "plain", size = 15, color = "black"),
            axis.text.x = element_text(size= 15),
            axis.text.y = element_text(size= 15),
            axis.line = element_line(size = 0.5, colour = "black")) + 
      theme(plot.margin=unit(c(1,0.5,0.5,1.2),"cm"))
    

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 2015-07-09
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多