如何更改 R 中条形图中条形的顺序?
在本文中,我们将讨论如何在 R 编程语言中更改条形图中条形图的顺序。我们可以使用两个图 ggplot 和 barplot 来更改条形图的顺序。
方法一:Ggplot重新排序
首先创建一个示例数据集并绘制图形手册。现在让我们相应地重新排序它们。
使用中的数据集:
ggplot 中的重新排序是使用 theme()函数的。在此,我们使用带有适当值的axis.text.x 来相应地重新排序。默认情况下,geom_bar 使用 stat=”bin”。这使得每个条的高度等于每组中的案例数,如果您希望条的高度代表数据中的值,请使用 stat="identity" 并将值映射到 y 美学。
- geom_bar() :用于条形图
句法:
geom_bar( mapping = NULL, data = NULL, stat = “count”, color=’blue’; fill=’yellow’ width = NULL, na.rm = FALSE, orientation = NA, show.legend = NA,…,)
- 主题:主题可用于为绘图提供一致的自定义外观。
Syntax:
theme( line, rect, text, title, aspect.ratio, axis.text.x, axis.text.x.top, axis.text.x.bottom,)
Parameter:
- line : all line elements (element_line())
- rect :all rectangular elements (element_rect())
- text : all text elements (element_text())
- title :all title elements: plot, axes, legends (element_text(); inherits from text)
- aspect.ratio :aspect ratio of the panel.
- axis.text.x : labels along with axes (element_text()). Specify all axis labels (axis.text), labels
手动订购
要手动重新排序条形,您必须在 geom_bar()函数传递 stat="identity" 。
例子:
R
library(ggplot2)
# Create the data frame.
gfg.data <- data.frame(
GFG_Id = c (1:7),
GFG_Name = c("Damon","Joe","Jen","Ryan","Bonnie","Stefan","William"),
GFG_Sal = c(6200,5152,6110,7290,8485,7654,2341))
print(gfg.data)
# GGPLOT
x <- ggplot(gfg.data, aes(x = GFG_Name, y = GFG_Sal))
x <- x + geom_bar(stat="identity", color='lightgreen',fill='lightgreen')
x <- x + theme(axis.text.x=element_text(angle=45, hjust=0.9))
x
R
library(ggplot2)
# Create the data frame.
gfg.data <- data.frame(
GFG_Id = c (1:7),
GFG_Name = c("Damon","Joe","Jen","Ryan","Bonnie","Stefan","William"),
GFG_Sal = c(6200,5152,6110,7290,8485,7654,2341))
print(gfg.data)
# GGPLOT
x <- ggplot(gfg.data, aes(x = reorder(GFG_Name, +GFG_Sal), y = GFG_Sal))
x <- x + geom_bar(stat="identity", color='red',fill='red')
x <- x + theme(axis.text.x=element_text(angle=45, hjust=0.9))
x
R
library(ggplot2)
# Create the data frame.
gfg.data <- data.frame(
GFG_Id = c (1:7),
GFG_Name = c("Damon","Joe","Jen","Ryan","Bonnie","Stefan","William"),
GFG_Sal = c(6200,5152,6110,7290,8485,7654,2341))
print(gfg.data)
# GGPLOT
x <- ggplot(gfg.data, aes(x = reorder(GFG_Name, -GFG_Sal), y = GFG_Sal))
x <- x + geom_bar(stat="identity", color='violet',fill='violet')
x <- x + theme(axis.text.x=element_text(angle=45, hjust=0.9))
x
R
# Create the data for the chart
GFG_ID <- c(1:7)
GFG_Sal <- c(6200,5152,6110,7290,8485,7654,2341)
GFG_Name <- c("Dia","Joe","Rex","Ryan","Bex","Stef","Max")
X <- data.frame(GFG_ID,GFG_Name,GFG_Sal)
X
# Give the chart file a name
png(file = "barchart_months_salary.png")
# Plot the bar chart
barplot(GFG_Sal,names.arg=GFG_Name,xlab="Employee Name",
ylab="Salary Range",col="black",
main="Salary chart",border="Red")
# Save the file
dev.off()
R
GFG_ID <- (LETTERS[1:7])
GFG_Sal <- c(6200,5152,6110,7290,8485,7654,2341)
data <- data.frame(GFG_Sal, GFG_ID)
barplot(data[order(data[,1],decreasing=FALSE),
][,1],names.arg=data[order(data[,1],decreasing=FALSE),]
[,2],col="blue",xlab="Employee Name Initial Letter",
ylab="Salary Range",main="Salary chart",border="black")
R
GFG_ID <- (LETTERS[1:7])
GFG_Sal <- c(6200,5152,6110,7290,8485,7654,2341)
data <- data.frame(GFG_Sal, GFG_ID)
barplot(data[order(data[,1],decreasing=TRUE),
][,1],names.arg=data[order(data[,1],decreasing=TRUE),
][,2],col="blue",xlab="Employee Name Initial Letter",
ylab="Salary Range",main="Salary chart",border="black")
输出:
按升序和降序重新排序
此处使用重新排序函数来更改图形的顺序。
句法 :
ggplot(dataframe name, aes(x=reorder(column1,±column2),y=column2)
在这里,如果您想要升序,那么您将使用“+”加号,如果您想要降序,那么您应该使用“-”减号。
注意: Column2 必须是包含数字数据的列。
示例:让我们首先按升序显示相同的条形图。
电阻
library(ggplot2)
# Create the data frame.
gfg.data <- data.frame(
GFG_Id = c (1:7),
GFG_Name = c("Damon","Joe","Jen","Ryan","Bonnie","Stefan","William"),
GFG_Sal = c(6200,5152,6110,7290,8485,7654,2341))
print(gfg.data)
# GGPLOT
x <- ggplot(gfg.data, aes(x = reorder(GFG_Name, +GFG_Sal), y = GFG_Sal))
x <- x + geom_bar(stat="identity", color='red',fill='red')
x <- x + theme(axis.text.x=element_text(angle=45, hjust=0.9))
x
输出:
示例:现在让我们看看降序图
电阻
library(ggplot2)
# Create the data frame.
gfg.data <- data.frame(
GFG_Id = c (1:7),
GFG_Name = c("Damon","Joe","Jen","Ryan","Bonnie","Stefan","William"),
GFG_Sal = c(6200,5152,6110,7290,8485,7654,2341))
print(gfg.data)
# GGPLOT
x <- ggplot(gfg.data, aes(x = reorder(GFG_Name, -GFG_Sal), y = GFG_Sal))
x <- x + geom_bar(stat="identity", color='violet',fill='violet')
x <- x + theme(axis.text.x=element_text(angle=45, hjust=0.9))
x
输出:
方法 2:使用 Barplot 重新排序
首先创建一个示例数据集并绘制图形。现在让我们看看条形的重新排序。
使用中的数据框:
在 R 中,barplot()函数
Syntax:
barplot(height, name.args = NULL, col = NULL, main = NULL)
Parameter:
- height: You can specify either a Vector or a Matrix of values.
- name.args :a Vector of names you want to plot below each bar or group of bars in an R bar chart.
手动订购
对于手动排序,在创建数据帧时将订单传递到 x 轴。
例子:
电阻
# Create the data for the chart
GFG_ID <- c(1:7)
GFG_Sal <- c(6200,5152,6110,7290,8485,7654,2341)
GFG_Name <- c("Dia","Joe","Rex","Ryan","Bex","Stef","Max")
X <- data.frame(GFG_ID,GFG_Name,GFG_Sal)
X
# Give the chart file a name
png(file = "barchart_months_salary.png")
# Plot the bar chart
barplot(GFG_Sal,names.arg=GFG_Name,xlab="Employee Name",
ylab="Salary Range",col="black",
main="Salary chart",border="Red")
# Save the file
dev.off()
输出
按升序和降序重新排列图形
这里使用的函数是order (order 返回一个排列,将其第一个参数重新排列为升序或降序)
Syntax –
order(…, na.last = TRUE, decreasing = FALSE)
Parameter:
na.last: for controlling the treatment of NAs
我们先来看升序图。
例子:
电阻
GFG_ID <- (LETTERS[1:7])
GFG_Sal <- c(6200,5152,6110,7290,8485,7654,2341)
data <- data.frame(GFG_Sal, GFG_ID)
barplot(data[order(data[,1],decreasing=FALSE),
][,1],names.arg=data[order(data[,1],decreasing=FALSE),]
[,2],col="blue",xlab="Employee Name Initial Letter",
ylab="Salary Range",main="Salary chart",border="black")
输出
现在让我们看看降序图。
例子:
电阻
GFG_ID <- (LETTERS[1:7])
GFG_Sal <- c(6200,5152,6110,7290,8485,7654,2341)
data <- data.frame(GFG_Sal, GFG_ID)
barplot(data[order(data[,1],decreasing=TRUE),
][,1],names.arg=data[order(data[,1],decreasing=TRUE),
][,2],col="blue",xlab="Employee Name Initial Letter",
ylab="Salary Range",main="Salary chart",border="black")
输出