R语言基础绘图之调整坐标轴(以Barplot为例)
基本图形绘制
首先我们需要一个基本的图形以供我们实现后续的调整:
# 基础代码
count <- c(420, 756, 10553, 22991, 20364, 45161, 8150, 12232, 15754, 17332, 1561, 6440, 1244)
names <- c("None or prefssdbf", "Age 1-3", "Age 4-10", "Age 11-15", "Age 12 withrtrt dipfsdfs or unclear", "High school dipfsdfs or equivalent", "Associate's degree", "5 years of collegesds", "Some college bder no dhdfhdde", "Bachelor's degree", "Professional school dfasfae", "Master's dfdsgsde", "Doctorate dasfafae")
barplot(count, xlab = "Education Level", ylab = "Number of People", main = "Education of Penn", names.arg = names)
由此我们能够绘制出一个初级的柱状图:
显然这里的x轴坐标轴显示不完全,为了解决这个问题,可以考虑旋转x轴标签的角度(45度/60度/90度)以使其显示完全,关于调整的方法有很多种,以下提供两种方法来供大家使用。
x轴标签旋转90度
我们可以通过在barplot函数中添加一个las=2参数使所有(注意!是所有,不只是x轴)坐标轴标签都旋转90度。
count <- c(420, 756, 10553, 22991, 20364, 45161, 8150, 12232, 15754, 17332, 1561, 6440, 1244)
names <- c("None or prefssdbf", "Age 1-3", "Age 4-10", "Age 11-15", "Age 12 withrtrt dipfsdfs or unclear", "High school dipfsdfs or equivalent", "Associate's degree", "5 years of collegesds", "Some college bder no dhdfhdde", "Bachelor's degree", "Professional school dfasfae", "Master's dfdsgsde", "Doctorate dasfafae")
barplot(count, xlab = "Education Level", ylab = "Number of People", main = "Education of Penn", names.arg = names,las=2)
但是实际操作后我们发现在其余参数不进行调整的情况下,旋转后的x轴标签显示不完全,而由于x轴和y轴标签的旋转,使其覆盖了x轴和y轴标题,所以我们需要对该图形的页边距进行调整使x轴标签显示完全,并且由于基础绘图函数中无法直接对轴标题的位置进行调整,因此需要单独给定轴标题位置。
# 旋转90度
count <- c(420, 756, 10553, 22991, 20364, 45161, 8150, 12232, 15754, 17332, 1561, 6440, 1244)
names <- c("None or prefssdbf", "Age 1-3", "Age 4-10", "Age 11-15", "Age 12 withrtrt dipfsdfs or unclear", "High school dipfsdfs or equivalent", "Associate's degree", "5 years of collegesds", "Some college bder no dhdfhdde", "Bachelor's degree", "Professional school dfasfae", "Master's dfdsgsde", "Doctorate dasfafae")
par(omi=c(0.1,0,0,0), # 调整图形外边距
mar=c(16,5,4,3)) # 调整图形内边距
barplot(count, xlab = "", ylab = "",
main = "Education of Penn", names.arg = names, las=2)
text(x=-2,y=25000,srt = 90, xpd = TRUE, labels = "Number of People") # 添加y轴标题
text(x=8, y=-95000,xpd=TRUE, labels="Education Level") # 添加x轴标题
x轴标签旋转45度/60度/90度
以上方法的限制在于我们只能将坐标轴标签角度调整90度,而大多数情况下,我们比较希望可以把标签调整为45度,以使图形更为美观,所以我们可以考虑在barplot中不添加x轴标签,改为用text单独添加标签;在该方法中同样需要考虑到旋转后的轴标签会覆盖轴标题的情况,因此与上文中的方法类似,需要重新给一个轴标题。
# 旋转45度
count <- c(420, 756, 10553, 22991, 20364, 45161, 8150, 12232, 15754, 17332, 1561, 6440, 1244)
names <- c("None or prefssdbf", "Age 1-3", "Age 4-10", "Age 11-15", "Age 12 withrtrt dipfsdfs or unclear", "High school dipfsdfs or equivalent", "Associate's degree", "5 years of collegesds", "Some college bder no dhdfhdde", "Bachelor's degree", "Professional school dfasfae", "Master's dfdsgsde", "Doctorate dasfafae")
par(omi=c(0.1,0,0,0),mar=c(9.5,5,4,3))
k=barplot(count, xlab = "", ylab = "Number of People",
main = "Education of Penn", names.arg = names, xaxt="n", space=1)
text(x=k,y=-1000,srt = 45, adj= 1, xpd = TRUE, labels = names)
text(x=13.5, y=-31000,xpd=TRUE, labels="Education Level")
以上是以barplot为例进行轴标签和标题的一系列调整,该方法能够应用于基础绘图系统中的大多数绘图函数。
欢迎各位留言讨论~