0

我正在尝试为 facet_grid() 使用“labeller =”参数分割绘图的类分配名称,但当我在特定数据集中使用它时会忽略它。该语法适用于以下使用来自基础 R 的 mtcars 数据集的可重现示例:

    labels <- c( "1" = "One", "0" = "Zero")
ggplot(mtcars, aes(x = hp) )+
  geom_histogram(aes(y=..density..), position="identity",alpha = 0.8, fill = "red") +
  ggtitle("hp histogram)") + 
  xlab("Hp") +  theme_economist()  + facet_grid( am ~ .,  labeller=labeller(am = labels)) +
  theme(strip.text.y = element_text(size=12, face="bold", colour = "white"),
        strip.background = element_rect(fill="cornflowerblue"))

在此处输入图像描述

当我对德国信用数据集(可以在 UCI 机器学习存储库中找到)使用相同的语法时,该参数被忽略:

labels <- c( "1" = "Bad Credit", "0" = "Good Credit")


plot_data <- german_fct_train %>% group_by(Class,F_Savings_account_bonds  ) %>% summarise(count = n())
plot_data <- plot_data %>% group_by(Class) %>% mutate(freq = count/sum(count))

ggplot(plot_data, aes(x = F_Savings_account_bonds, y = freq ) )+
  geom_bar(stat="identity", alpha = 0.8, fill = "red") +
  ggtitle("Savings/Bonds Account \n (Bad Credit = 1, Good Credit = 0)") + 
  theme_economist()  + 
  scale_x_discrete(name = "Status of Savings/Bonds Account") + 
  scale_y_continuous(name = "Relative Frequency" , labels = percent, breaks = seq(0, 0.8, 0.1)) +
  facet_grid( as.factor(Class) ~ . , labeller=labeller(Class = labels)) +
  theme(strip.text.y = element_text(size=12, face="bold", colour = "white"),
        strip.background = element_rect(fill="cornflowerblue")) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

在此处输入图像描述

您的建议将不胜感激。

4

0 回答 0