R语言 增加ggplot2绘图的边界线厚度
在这篇文章中,我们将看到如何使用R编程语言中的ggplot2条形图改变边界线厚度。
首先,如果之前没有在R Studio中安装ggplot2软件包,你需要安装它。为了安装和加载,在R控制台中写下以下命令。
install.packages("ggplot2")
library(ggplot2)
为了创建一个简单的柱状图,我们将使用函数 geom_bar( )
语法
geom_bar(stat, fill, color, width)
参数 :
- stat : 设置stat参数以确定模式。
- fill : 代表条形图内部的颜色。
- color : 代表条形图轮廓的颜色。
- width : 代表条形图的宽度。
让我们来看看一个有两个向量 “match “和 “run “的数据框,并将其存储在一个变量 “run “中。
# Inserting data
runs <- data.frame(match=c("M-1","M-2","M-3","M-4"),
run=c(33, 45, 66, 50))
head(runs)
输出
条形图
# Bar Plot
library(ggplot2)
IPL <-ggplot(data=runs, aes(x=match, y=run)) +
geom_bar(stat="identity",fill="white",color="black")+
theme_classic()
IPL
输出
增加边框厚度
在函数geom_bar( )中,使用关键字 size 并指定一个值来改变边框的厚度。在我们的例子中,我们给大小分配了一个2的值。我们可以观察到,边框线的厚度增加了。
# Changing the border thickness
library(ggplot2)
IPL <-ggplot(data=runs, aes(x=match, y=run)) +
geom_bar(stat="identity",fill="white",color="black",size=2)+
theme_classic()
IPL
输出
例2: 考虑一个数据框,它由学生在考试中获得的分数信息组成。
# Inserting data
marks <- data.frame(student=c("S-1","S-2","S-3","S-4","S-5"),
mark=c(90, 85, 75, 78, 98))
# Changing the border thickness
library(ggplot2)
RESULT <-ggplot(data=marks, aes(x=student, y=mark)) +
geom_bar(stat="identity",fill="yellow",color="navy",size=4,alpha=0.1)+
theme_classic()
RESULT
输出