labs()命令用于设置ggplot忽略的图例标题

时间:2017-06-01 04:30:42

标签: r ggplot2 legend

我正在使用以下代码,但我的命令实验室(fill =“Class”)应将图例标题设置为“Class”,将被忽略:

    ggplot(data_Test, aes(x = predictions_plain, fill = as.factor(income_Class), 
color = as.factor(income_Class))) +
      geom_histogram(aes(y=..density..), position="identity",alpha = 0.4) + 
guides(color = FALSE)
      geom_density (alpha = 0.5)+ 
     labs(title = "Predicted Probabilities per Class in the Test Dataset", 
      x = "Predicted Probability of being in Class1", y = "Count") + 
      labs(fill = "Class")

enter image description here

您的建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

编辑:我认为2 labs()和其他语法错误是您的问题。

ggplot(data_Test, aes(x = predictions_plain)) +
  geom_histogram(aes(y=..density.., fill = as.factor(income_Class)),
                 position = "identity", alpha = 0.4) + 
  geom_density(alpha = 0.5, aes(color = as.factor(income_Class))) + 
  guides(color = FALSE) +
  labs(title = "Predicted Probabilities per Class in the Test Dataset", 
       x = "Predicted Probability of being in Class1",
       y = "Count",
       fill = "Class")

这是另一种方式......

ggplot(data_Test, aes(x = predictions_plain)) +
  geom_histogram(aes(y=..density.., fill = as.factor(income_Class)),
                 position = "identity", alpha = 0.4) + 
  geom_density(alpha = 0.5, aes(color = as.factor(income_Class))) + 
  guides(color = FALSE) +
  labs(title = "Predicted Probabilities per Class in the Test Dataset", 
       x = "Predicted Probability of being in Class1",
       y = "Count") +
  scale_fill_discrete(name = "Class")