📜  更改 R 中 GGPLOT2 分面图的标签

📅  最后修改于: 2022-05-13 01:55:29.265000             🧑  作者: Mango

更改 R 中 GGPLOT2 分面图的标签

在本文中,我们将看到如何在 R 编程语言中更改 ggplot2 Facet Plot 的标签。

要创建 ggplot2 图,我们必须加载 ggplot2 包。 library()函数用于此目的。然后创建或加载数据帧。创建一个带有刻面的规则图。默认情况下添加标签。

例子:

R
library("ggplot2") 
    
DF <- data.frame(X = rnorm(20),                                   
                 Y = rnorm(20),
                 LBLs = c("Label 1", "Label 2", 
                          "Label 3", "Label 4"))
  
ggplot(DF, aes(X, Y)) +                                     
  geom_point(size = 7, fill = "green", color = "black", shape = 21) +
  facet_grid(. ~ LBLs)


R
library("ggplot2") 
    
DF <- data.frame(X = rnorm(20),                                   
                 Y = rnorm(20),
                 LBLs = c("Label 1", "Label 2",
                          "Label 3", "Label 4"))
  
ggplot(DF, aes(X, Y)) +                                     
  geom_point(size = 7, fill = "green", color = "black", shape = 21) +
  facet_grid(. ~ LBLs, labeller = label_both)


R
library("ggplot2") 
    
DF <- data.frame(X = rnorm(20),                                   
                 Y = rnorm(20),
                 LBLs = c("Label 1", "Label 2", 
                          "Label 3", "Label 4"))
    
newDF <- DF
levels(newDF$LBLs) <- c("New Label 1", "New Label 2", 
                        "New Label 3", "New Label 4")
  
ggplot(newDF, aes(X, Y)) +             
  geom_point(size = 7, fill = "green", color = "black", shape = 21) +
  facet_grid(. ~ levels(LBLs))


R
library("ggplot2") 
    
DF <- data.frame(X = rnorm(20),                                   
                 Y = rnorm(20),
                 LBLs = c("Label 1", "Label 2",
                          "Label 3", "Label 4"))
  
new <- c("labeller 1", "labeller 2",
         "labeller 3", "labeller 4")
names(new) <- c("Label 1", "Label 2", 
                "Label 3", "Label 4")
  
ggplot(DF, aes(X, Y)) +             
  geom_point(size = 7, fill = "green", color = "black", shape = 21) +
  facet_grid(. ~ LBLs, labeller = labeller(LBLs = new))


输出 :



分面图

使用 ggplot2 的 FacetPlot

现在让我们探索改变标签的不同方法。

方法一:结合标签变量和分面标签

如果我们想标签变量(LBLs)到小关节标签(LBLs的值)相结合,那么我们只需要facet_grid的使用贴标机参数(),并设置label_both到它的价值。

示例 1:

电阻

library("ggplot2") 
    
DF <- data.frame(X = rnorm(20),                                   
                 Y = rnorm(20),
                 LBLs = c("Label 1", "Label 2",
                          "Label 3", "Label 4"))
  
ggplot(DF, aes(X, Y)) +                                     
  geom_point(size = 7, fill = "green", color = "black", shape = 21) +
  facet_grid(. ~ LBLs, labeller = label_both)

输出 :

将标签变量与 Facet 标签相结合

将标签变量与 Facet 标签相结合

方法 2:使用 levels()

正如我们之前讨论的,DataFrame 的 LBLs 变量负责 Facet 的标签,所以现在我们将创建一个新的 DataFrame 并为其分配旧的 DataFrame DF。然后只需更改新 DataFrame 的标签值变量即可。这里我们使用levels()函数以一种新的方式设置变量的属性。

示例 2:

电阻

library("ggplot2") 
    
DF <- data.frame(X = rnorm(20),                                   
                 Y = rnorm(20),
                 LBLs = c("Label 1", "Label 2", 
                          "Label 3", "Label 4"))
    
newDF <- DF
levels(newDF$LBLs) <- c("New Label 1", "New Label 2", 
                        "New Label 3", "New Label 4")
  
ggplot(newDF, aes(X, Y)) +             
  geom_point(size = 7, fill = "green", color = "black", shape = 21) +
  facet_grid(. ~ levels(LBLs))

输出 :

使用 level() 更改了 Facet 标签

使用 level() 更改了 Facet 标签

方法 3:使用 labeller 参数

labeller 可以是一个函数,也可以是一个命名的字符向量(即参数),它可以是值或变量。 labeller()函数,当分配给 facet_grid() 的 labeller 参数时,采用“variable = value”形式的命名参数并手动更改标签。

示例 3:

电阻

library("ggplot2") 
    
DF <- data.frame(X = rnorm(20),                                   
                 Y = rnorm(20),
                 LBLs = c("Label 1", "Label 2",
                          "Label 3", "Label 4"))
  
new <- c("labeller 1", "labeller 2",
         "labeller 3", "labeller 4")
names(new) <- c("Label 1", "Label 2", 
                "Label 3", "Label 4")
  
ggplot(DF, aes(X, Y)) +             
  geom_point(size = 7, fill = "green", color = "black", shape = 21) +
  facet_grid(. ~ LBLs, labeller = labeller(LBLs = new))

输出:

使用 labeller 更改了 Facet 标签

使用 labeller 更改了 Facet 标签