R语言 使用ggplot2绘制箱形图
在这篇文章中,我们将使用ggplot2包在R编程语言中创建一个具有各种功能的Bowxplot。
对于数据的分布,你可能需要比中心趋势值(中位数、平均值、模式)更多的信息。为了分析数据的可变性,你需要知道数据的分散程度。那么,箱形图是一个说明数据中数值分布的图形。箱形图通常被用来以标准的方式展示数据的分布,呈现五个摘要值。下面的列表总结了最小值、Q1(第一四分位数)、中位数、Q3(第三四分位数)、和最大值。 总结这些数值可以为我们提供关于离群值和它们的信息。
在ggplot2中,geom_boxplot()被用来创建一个boxplot。
语法: geom_boxplot( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., outlier.color = NULL, outlier.color = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, notch = FALSE,na.rm = FALSE, show. legend = FALSE, inherit.aes = FALSE)
使用中的数据集 :Crop_recommendation
让我们首先创建一个常规的boxplot,为此我们首先要导入所有需要的库和使用中的数据集。然后在ggplot()函数中简单地把所有要绘制的属性和geom_boxplot放在一起。
例子
library(ggplot2)
# Create the dataset or load the dataset
# for the chart
Dataset <- c(17, 32, 8, 53, 1,45,56,678,23,34)
Dataset
# loading data set and storing it in ds variable
ds <- read.csv(
"c://crop//archive//Crop_recommendation.csv", header = TRUE)
# create a boxplot by using geom_boxplot() function
# of ggplot2 package
crop=ggplot(data=ds, mapping=aes(x=label, y=temperature))+geom_boxplot()
crop
输出:
将均值添加到boxplot中
均值也可以被添加到boxplot中,为此我们必须在stat_summary()中指定我们正在使用的函数。这个函数用于添加新的摘要值,并将这些摘要值添加到图中。通过使用这个函数,你不需要在绘图前计算均值。
语法
stat_summary( fun, geom)
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv(
"c://crop//archive//Crop_recommendation.csv", header = TRUE)
# add mean to ggplot2 boxplot
ggplot(ds, aes(x = label, y = temperature, fill = label)) +
geom_boxplot() +
stat_summary(fun = "mean", geom = "point", shape = 8,
size = 2, color = "white")
输出
现在让我们来讨论使用theme()函数在Bexplot中的图例位置。我们可以将图例的位置改为顶部或底部,也可以删除boxplot中的图例位置。可以通过使用主题来定制绘图组件,如标题、标签、字体、背景、网格线和图例。图形可以通过使用主题来定制。你可以使用theme()方法修改单个绘图的主题,也可以通过调用theme_update()方法修改活动主题,这将影响所有后续的绘图。
语法
theme( line, rect, text, title, aspect.ratio, axis.title, axis.title.x, axis.title.x.top, axis.title.x.bottom, axis.title.y, axis.title.y.left, axis.title.y.right, axis.text, axis.text.x, axis.text.x.top, axis.text.x.bottom, axis.text.y, axis.text.y.left, ......, validate = TRUE)
在这个函数中,如果你将legend.position参数设置为top或bottom,那么位置将改变。
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
# change the legend position in R using ggplot2
ggplot(ds, aes(x = label, y = temperature, fill = label)) +
geom_boxplot() +
theme(legend.position = "top")
输出
在R中使用ggplot2的水平博弈图
使用 coord_flip()函数,也可以将博列表水平放置。这个函数只是切换x轴和y轴。
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
# Creating a Horizontal Boxplot using ggplot2 in R
ggplot(ds, aes(x = label, y = temperature, fill = label)) +
geom_boxplot() +
coord_flip()
输出
改变箱形图线的颜色
1) 默认
使用 color=label 命令,为条形图的轮廓添加颜色。
语法
color=label
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv(
"c://crop//archive//Crop_recommendation.csv", header = TRUE)
# Change box plot line colors by groups
crop2<-ggplot(ds, aes(x=label, y=temperature, color=label)) +
geom_boxplot()
crop2
输出
2) 手动
- 使用自定义调色板: 要使用自定义调色板,请使用scale_color_manual()函数,并在该函数中为每个boxplot提供轮廓颜色。
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
# Change box plot line colors by groups
crop2<-ggplot(ds, aes(x=label, y=temperature, color=label)) +
geom_boxplot()
crop2
# Now, it is also possible to change line colors manually
crop2+scale_color_manual(values=c("#999999", "#E69F00",
"#56B4E9","#999999","Red",
"green","yellow"))
输出
- 使用brewer调色板: 你可以用brewer调色板改变boxplot的轮廓颜色。要做到这一点,你只需要使用scale_color_brewer()函数并在该函数中设置调色板参数。
语法
scale_color_brewer(palette)
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv(
"c://crop//archive//Crop_recommendation.csv", header = TRUE)
# Change box plot line colors by groups
crop2<-ggplot(ds, aes(x=label, y=temperature, color=label)) +
geom_boxplot()
crop2
# for Using brewer color palettes
crop2+scale_color_brewer(palette="Dark2")
输出
- 使用灰度: 为了使用灰度调色板,你需要使用scale_color_grey()函数,并在其中加入theme_classic()函数。
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
# Change box plot line colors by groups
crop2<-ggplot(ds, aes(x=label, y=temperature, color=label)) +
geom_boxplot()
# for using grey scale
crop2 + scale_color_grey() + theme_classic()
输出
用颜色填充boxplot
1) 默认情况: 如果要用你选择的颜色填充boxplot,你可以使用fill属性命令在geom_boxplot()函数中添加颜色。填充会在geom_boxplot( )下面,因为在这种情况下它是可变的。
语法
fill=’color’
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
# Now fill the boxplot with choice of your color
crop1=ggplot(data=ds, mapping=aes(x=label, y=temperature))+
geom_boxplot(fill='green')
crop1
输出
对于默认情况下填充boxplot的颜色,你只需要在ggplot()的aes()函数中加入fill属性。填充将在 ggplot( )下的 aes( )内,因为在这种情况下它是可变的。
语法
fill=label
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
# Change Colors of a ggplot2 Boxplot in R
crop3<-ggplot(ds, aes(x = label, y = temperature, fill = label)) +
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2)
输出
2) 手动: 如果你想手动改变boxplot的颜色,那么你可以根据你的选择使用scale_fill_manual(), scale_fill_brewer()和scale_fill_grey()三个函数。
- 使用自定义调色板: 要使用自定义调色板,可以使用scale_fill_manual()函数和颜色值作为参数。
语法
scale_fill_manual(values)
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv(
"c://crop//archive//Crop_recommendation.csv", header = TRUE)
crop3<-ggplot(ds, aes(x = label, y = temperature, fill = label)) +
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2)
crop3+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9",
"#999999","Red","green","yellow"))
输出
- 使用酿酒师调色板: 要使用来自RColorBrewer包的酿酒师调色板scale_fill_brewer()和调色板作为参数。
语法
scale_fill_brewer(palette)
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
crop3<-ggplot(ds, aes(x = label, y = temperature, fill = label)) +
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2)
crop3+scale_fill_brewer(palette="Dark1")
输出
- 使用灰度: 使用scale_fill_grey()和theme_classic()来为boxplots填充灰度的颜色。
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
crop3<-ggplot(ds, aes(x = label, y = temperature, fill = label)) +
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2)
# for using grey scale
crop3 + scale_fill_grey() + theme_classic()
输出
在boxplots中添加抖动器
抖动对于处理离散数据集引起的过度绘图问题非常有用。你也可以调整抖动的位置,为此你只需要在geom_jitter()中设置位置属性。你也可以通过使用ggplot jitter中的size和shape参数来改变点的形状和大小。
语法
geom_jitter(mapping = NULL, data = NULL, stat = "identity", position = "jitter", ..., width = NULL, height = NULL, na.rm = FALSE, show.legend = NA, inherit.ais = TRUE)
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
ggplot(ds, aes(x=label, y=temperature)) +
geom_boxplot()+
geom_jitter(position=position_jitter(0.2))
输出
缺口箱形图
对于添加缺口的boxplot,你只需要在geom_boxplot()中设置缺口属性为TRUE。
例子
library(ggplot2)
# loading data set and storing it in ds variable
ds <- read.csv("c://crop//archive//Crop_recommendation.csv", header = TRUE)
# Add notched box plot
ggplot(ds, aes(x=label, y=temperature)) +
geom_boxplot(notch = TRUE)+
geom_jitter(position=position_jitter(0.2))
输出