我正在绘制图形,但ggplot忽略了我指定的因子水平的顺序以及函数scale_fill_manual()。
数据集是德国信用数据集,可以在UCI机器学习库中找到,代码如下:
german_fct_train$Status_of_existing_account <- factor(german_fct_train$Status_of_existing_account ,
levels = c("No_Account" , "less_than_0" , "between_0_and_200" , "greater_equal_to_200"))
german_fct_train$Class <- factor(german_fct_train$Class, levels = c("1" , "0"))
ggplot( german_fct_train, aes( F_Credit_history, F_Status_of_existing_account)) +
geom_jitter(aes(color = Class), size = 3, alpha = 0.5) + theme_economist() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle("Status of existing checking account
and Credit History by Class") +
xlab("Credit History") + ylab("Status of existing account") +
labs(fill="Class") +
scale_fill_manual(limits= c("1", "0"), labels = c("Bad Customers" , "Good Customers"), values = c("red", 'blue'))
您的建议将不胜感激。
答案 0 :(得分:0)
在您的示例中,我建议将ggplot外的Class
分解,然后应用scale_fill_manual
而不添加最有可能导致问题的标签。这是mtcars的一个例子
data("mtcars")
library(ggplot)
ggplot(mtcars, aes(factor(carb), factor(gear))) +
geom_jitter(aes(color = factor(gear)), size = 3, alpha = 0.5) +
scale_fill_manual(values = c("red", 'blue', 'darkgoldenrod'))+
xlab("Credit History") + ylab("Status of existing account") +
labs(x="Credit History", y="Status of existing account",
title = "Status of existing checking account and Credit History by Class") +
theme_light() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
此外,我还建议您使用labs
来电代替xlab
,ylab
和ggtitle
。最后,ggplot在调用结束时倾向于theme
,这也可能导致一些错误。