将图例添加到geom_density R.

时间:2018-03-07 11:54:49

标签: r ggplot2

我正在使用Prosper Loan数据集,并且我试图使用geom_density在同一个图中显示两个变量。 问题是,当我尝试包含lengend以显示粉红色区域中的变量名称和来自暗区域的变量名称时,它不起作用。

library(ggplot2)
EstimatedLoss <-  c(0.5, 0.2,0.3,0.4,0.8,0.5, 0.2,0.3,0.4,0.8)
EstimatedEffectiveYield <- c(0.10, 0.15,0.18,0.20,0.8,0.15, 0.13,0.22,0.22,0.25)
prosper_loan <- data.frame(EstimatedLoss,EstimatedEffectiveYield)
ggplot(data = prosper_loan)
geom_density(aes(EstimatedLoss * 100), color = '#e1b582', fill = '#e1b582', alpha = 0.5, show.legend = TRUE ) +
geom_density(aes(EstimatedEffectiveYield * 100), color = '#a2b285',fill = '#a2b285', alpha = 0.7, linetype = 3, size = 1, show.legend = TRUE) +
scale_y_continuous(name = "Density")+
scale_x_continuous(name = "Estimate loss and effective yield in percentage") +
ggtitle('Density from the Estimated loss and effective yield in percentage') 

我做错了吗?

1 个答案:

答案 0 :(得分:3)

理想情况下,您的数据应该是每行一次观察(也称为“长”数据)才能正确利用ggplot2。以下是使用tidyr::gather首先转换数据的示例。图例会自动添加fillcolor美学。

library(ggplot2)
library(tidyr)
library(magrittr)

EstimatedLoss <-  c(0.5, 0.2,0.3,0.4,0.8,0.5, 0.2,0.3,0.4,0.8)
EstimatedEffectiveYield <- c(0.10, 0.15,0.18,0.20,0.8,0.15, 0.13,0.22,0.22,0.25)

prosper_loan <- data.frame(EstimatedLoss, EstimatedEffectiveYield) %>% 
  gather(key, value, EstimatedLoss:EstimatedEffectiveYield)

ggplot(data = prosper_loan) +
  geom_density(aes(value * 100, fill = key, color = key), alpha = 0.5) +
  scale_fill_manual(values = c('#e1b582', '#a2b285')) + 
  scale_color_manual(values = c('#e1b582', '#a2b285')) +
  scale_y_continuous(name = "Density")+
  scale_x_continuous(name = "Estimate loss and effective yield in percentage") +
  ggtitle('Density from the Estimated loss and effective yield in percentage') 

enter image description here