【问题标题】:Adjust Font Size and Decimal Places in R Boxplot (ggpubr)在 R Boxplot (ggpubr) 中调整字体大小和小数位
【发布时间】:2019-06-28 13:01:08
【问题描述】:

我正在使用 R 包 GGPubr 制作箱线图。我真的很喜欢它提供的漂亮视觉效果,但我遇到了问题。有谁知道如何增加轴上数字、轴标签和类标签的字体大小?另外,如何设置平均值以使其仅显示 2 位小数?

这是我正在使用的代码:

library("ggpubr")
mydata <- read.csv("C:\\temp\\ndvi.csv")
ggboxplot(mydata, x = "class", y = "NDVI", 
      color = "class",
      order = c("Conifer", "Deciduous", "Grasslands"),  ggtheme=theme_gray(),
      ylab = "NDVI Value", xlab = "Land Cover Class",
      add="mean",
      font.label = list(size = 30, face = "bold"))+ stat_summary(fun.data 
= function(x) data.frame(y=1, label = paste("Mean=",mean(x))),  geom="text")
+theme(legend.position="none")

还有 csv:

NDVI,class
0.25,Conifer
0.27,Conifer
0.29,Conifer
0.403,Deciduous
0.38,Deciduous
0.365,Deciduous
0.31983489,Grasslands
0.32005,Grasslands
0.328887766,Grasslands

我更愿意使用 GGPubr 而不是 boxplot() 或 ggplot/ggplot 2 来实现上述预期效果。谢谢。

【问题讨论】:

    标签: r ggplot2 ggpubr


    【解决方案1】:

    这是一个选项,我们使用round() 处理小数点后两位并添加另一个theme() 来更改文本大小。

    ggboxplot(mydata, x = "class", y = "NDVI", 
              color = "class",
              order = c("Conifer", "Deciduous", "Grasslands"),  ggtheme=theme_gray(),
              ylab = "NDVI Value", xlab = "Land Cover Class",
              add="mean",
              font.label = list(size = 30, face = "bold")) +
      # use round() and set y = .45 
      stat_summary(fun.data = function(x) data.frame(y=1, label = paste("Mean=", round(mean(x), 2))), geom="text") + 
      theme(legend.position="none") +
      theme(text = element_text(size = 16)) # change text size of theme components
    

    【讨论】:

    • 非常感谢@markus。用于舍入小数和文本的代码效果很好!
    猜你喜欢
    • 2021-12-06
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    相关资源
    最近更新 更多