R语言 绘制带有平均数的凸透图

R语言 绘制带有平均数的凸透图

在这篇文章中,我们将讨论如何在R编程语言中画一个带有平均值的博列表。

方法1:使用 points() 和 text()

在这种方法中,为了绘制带有数据平均值的boxplot,用户需要调用boxplot()函数,输入所需的参数来绘制给定数据的简单boxplot,在此基础上,用户需要调用point()函数来指出所绘制的每个boxplot的平均值,进一步在text()函数的帮助下,输入所需的参数,就可以在R编程语言中获得boxplot图的平均值。

  • Points(): 这是一个通用函数,用于在指定的坐标上绘制一连串的点。

语法

points(x, y = NULL, type = “p”, …)

  • Text 函数有助于在x和y给定的坐标上绘制矢量标签中给定的字符串。

语法:

text (x, y = NULL, labels = seq_along(x$x), adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, cex = 1, col = NULL, font = NULL, ... )

例子

gfg=data.frame(A=c(1,5,1,5,6,6,4,1,1,5,4,1,8,1,1),
               B=c(1,8,6,6,6,4,5,7,8,1,7,4,4,1,6),
               C=c(9,5,1,5,4,1,8,6,4,8,4,4,5,7,6))
  
gfg_mean=colMeans(gfg)
  
boxplot(gfg)
  
points(x = 1:ncol(gfg),y = gfg_mean, col = "green")
  
text(x = 1:ncol(gfg),y =gfg_mean - 0.20, 
     labels = paste("Mean:", round(gfg_mean, 1)), col = "green")

输出

在R语言中绘制带有平均数的凸透图

方法2:使用ggplot2包中的geom_boxplot()和stat_summary()

在这个方法中,用户首先需要导入并安装ggplot2包到R控制台,因为在这个方法中使用的函数来自ggplot2包,然后用户需要调用geom_boxplot()函数,并输入所需的参数,这将导致所提供的数据的boxplot的正常绘制,然后用户需要进一步调用stat_summary(),这将找到每个boxplot的平均值并在R编程语言中的图上标注。

geom_boxplot() 函数用于绘制盒式和晶须图。

语法

geom_boxplot( mapping = NULL, data = NULL, stat = "boxplot", position = "dodge2", ..., outlier.color = NULL, outlier.color = NULL,outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE,notchwidth = 0.5, varwidth = FALSE, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE)

  • stat_summary() 函数允许在指定总结函数时有巨大的灵活性

语法

stat_summary(mapping = NULL, data = NULL, geom = "pointrange", position = "identity", ...)

例子

library(ggplot2)
  
gfg=data.frame(values=c(1,8,6,6,6,4,5,7,8,1,7,4,4,1,6),
               group =LETTERS[1:3])
  
ggplot(gfg, aes(x = group, y = values)) + geom_boxplot() +
  stat_summary(fun = mean, geom = "point", col = "green") +  
stat_summary(fun = mean, geom = "text", col = "green",    
vjust = 1.5, aes(label = paste("Mean:", round(..y.., digits = 1))))

输出

在R语言中绘制带有平均数的凸透图

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程