更改 R 中 ggplot2 图例的显示顺序
在本文中,我们将看到如何在 R 编程语言中更改 ggplot2 图例的显示顺序。
为此,首先我们应该使用 library()函数加载 ggplot2 包。加载或安装 ggplot2 包的语法如下。要安装 ggplot2 包,请将以下命令写入 R 控制台。
install.packages("ggplot2")
library("ggplot2")
要创建图,我们使用ggplot()函数,为了使其成为散点图,我们将geom_point()函数添加到 ggplot()函数。
R
# Load Package
library("ggplot2")
# Create a DataFrame
DF <- data.frame(Year = c(2011, 2012, 2013, 2014, 2015),
Points = c(30, 20, 15, 35, 50),
Users = c("User1", "User2", "User3", "User4", "User5"))
# Create a Scatter Plot
ggplot(DF,aes(Year, Points, color = Users))+
geom_point(size = 10)
R
# Load Package
library("ggplot2")
# Create a DataFrame
DF <- data.frame(Year = c(2011, 2012, 2013, 2014, 2015),
Points = c(30, 20, 15, 35, 50),
Users = c("User1", "User2", "User3",
"User4", "User5"))
# Copy Old dataframe to New DataFrame.
newDF <- DF
# Change the order of Users column of DataFrame
newDF$Users <- factor(newDF$Users,
levels = c("User4", "User1", "User5",
"User2", "User3"))
# Create ScatterPlot with new dataframe.
ggplot(newDF,aes(Year, Points, color = Users))+
geom_point(size = 10)
R
# Load Package
library("ggplot2")
# Create a DataFrame
DF <- data.frame(Year = c(2011, 2012, 2013, 2014, 2015),
Points = c(30, 20, 15, 35, 50),
Users = c("User1", "User2", "User3", "User4", "User5"))
# Copy Old dataframe to New DataFrame.
newDF <- DF
# Reverse the order of Users column of DataFrame
newDF$Users <- factor(newDF$Users,
levels = rev(DF$Users))
# Create ScatterPlot with new dataframe.
ggplot(newDF,aes(Year, Points, color = Users))+
geom_point(size = 10)
输出:
将 Legend 的 order 更改为Desired Order :
现在为了改变图例的顺序,我们必须创建新的数据框,让我们说newDF 。在这里,我们将旧数据帧 (DF) 复制到新数据帧 (newDF),因为我们只想更改图例的顺序。要将 DF 复制到 newDF,我们只需将 DF 分配给 newDF。
newDF <- DF
为了改变 wsers 的顺序,我们将使用factor()函数,并且在 factor 内部我们将使用levels参数来分配 Legend 的新顺序。
Syntax : factor(x, levels)
Parameter :
- x : A Data Object, which takes distinct values. here we want to change only Users column of dataframe. for that we will use newDF$Users as a first parameter (i.e. inplace of x)of factor
- levels : A Character string or column that determines the new order of x vector
Return : Encode a vector as a Factor.
newDF$Users <- factor(newDF$Users, levels = c(“User4”, “User1”, “User5”, “User2”, “User3”))
现在,我们将一如既往地使用 ggplot() 和 geom_point()函数简单地使用这个新数据帧创建一个 ScatterPlot。
电阻
# Load Package
library("ggplot2")
# Create a DataFrame
DF <- data.frame(Year = c(2011, 2012, 2013, 2014, 2015),
Points = c(30, 20, 15, 35, 50),
Users = c("User1", "User2", "User3",
"User4", "User5"))
# Copy Old dataframe to New DataFrame.
newDF <- DF
# Change the order of Users column of DataFrame
newDF$Users <- factor(newDF$Users,
levels = c("User4", "User1", "User5",
"User2", "User3"))
# Create ScatterPlot with new dataframe.
ggplot(newDF,aes(Year, Points, color = Users))+
geom_point(size = 10)
输出:
颠倒传奇的顺序
为了反转图例的顺序,我们简单地使用rev()函数作为因子函数的级别参数的值,在 rev()函数内部,我们指定要反转的值的顺序。
Syntax : rev(x)
Parameter :
- x : rev function has only one parameter, which represents the vector or other Data Object that we want to reverse its order, DF$Users in our example.
Return : Reversed order of its argument.
newDF$Users <- factor(newDF$Users, levels = rev(DF$Users))
电阻
# Load Package
library("ggplot2")
# Create a DataFrame
DF <- data.frame(Year = c(2011, 2012, 2013, 2014, 2015),
Points = c(30, 20, 15, 35, 50),
Users = c("User1", "User2", "User3", "User4", "User5"))
# Copy Old dataframe to New DataFrame.
newDF <- DF
# Reverse the order of Users column of DataFrame
newDF$Users <- factor(newDF$Users,
levels = rev(DF$Users))
# Create ScatterPlot with new dataframe.
ggplot(newDF,aes(Year, Points, color = Users))+
geom_point(size = 10)
输出: