将图例添加到geom_density R [英] Add legend to geom_density R
问题描述
我正在使用Prosper Loan数据集,并且试图在使用geom_density的同一个图中显示两个变量。
问题是,当我试图包含lengend以显示粉红色区域中的变量名称和黑暗区域中的变量名称时,它不起作用。
library(ggplot2)
pre>
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 = (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 =估计损失和实际收益率百分比)+
ggtitle('估计损失和有效收益率的百分比密度')
A m我做错了什么?
解决方案理想情况下,您的数据应该是每行一个观察值(又名长数据)利用
ggplot2
。以下是首先使用tidyr :: gather
转换数据的示例。一个图例会自动添加一个fill
或color
唯美。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 =Estimated loss and effective yield in percentage )+
ggtitle('估计损失和有效收益率的百分比密度')
I'm working with the Prosper Loan dataset and I'm trying to show two variable in the same plot using geom_density. The problem, when I try to include the lengend to show the variable name from the pink area and the variable name from the dark area, it doesn't work.
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')
Am I doing anything wrong?
解决方案Ideally, your data should be one observation per row (aka "long" data) to properly take advantage of
ggplot2
. Here's an example of first transforming the data usingtidyr::gather
. A legend will automatically be added with afill
orcolor
aesthetic.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')
这篇关于将图例添加到geom_density R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!