我正在尝试为class赋值,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))
您的建议将不胜感激。