简体   繁体   English

在R中叠加10个密度图,其颜色与重叠图的数量成比例

[英]Overlay 10 density plots in R with colour proportional to number of overlapping plots

I have a dataset with 224900 observations and 10 variables which are the result of different Taylor series back transformations to original data values. 我有一个包含224900个观测值和10个变量的数据集,它们是不同泰勒级数回归到原始数据值的结果。 I wish to overlay the density plots of each of these 10 variables to show the level of robustness of the Taylor series back transformation on the data estimates. 我希望覆盖这10个变量中每个变量的密度图,以显示泰勒级数反向变换对数据估计的鲁棒性水平。 Rather than having just 10 lines, I thought it would be good to have a colour apply, so that each density plot contributes 10% of a grey scale. 我不认为只有10行,而是应用颜色会很好,因此每个密度图都会产生10%的灰度。 Where there is data that just relates to one of the plots, there would be 10% grey, two plots would be twice as dark at 20%, up to where all the density plots overlap, which would be 100%. 如果有数据只与其中一个图相关,那么将有10%灰色,两个图表将是20%的两倍深度,直到所有密度图重叠的位置,这将是100%。

I have used melt to get a dataframe that is 2249000 rows long. 我使用了melt来获得长度为2249000行的数据帧。 There are three columns, the first is the person ID, the second is the grouping variable ( variable ), and the third is the value of daily kJ intake ( value ). 有三列,第一列是人物ID,第二列是分组变量( variable ),第三列是每日kJ摄入量( value )。

I have used the following code to overlay the density plots in ggplot2 but it uses different colours for the groups. 我使用以下代码覆盖ggplot2的密度图,但它为组使用不同的颜色。 How can I change this code to get my grey scale? 如何更改此代码以获得灰度? I want all 10 groups to have the same colour and colour density; 我希望所有10组具有相同的颜色和颜色密度; the purpose of the plot is simply to to visually show the amount of overlap on the density plot using greyscale. 该图的目的仅仅是使用灰度在视觉上显示密度图上的重叠量。

ggplot(Energy, aes(x=value, fill=variable)) + geom_density(alpha = 0.5)

Some test data to play with for those wishing to help, using 5 groups and not 10: 一些测试数据可供那些希望提供帮助的人使用5组而不是10组:

variable <- c(rep("A",100), rep("B",100), rep("C",100), rep("D",100), rep("E",100))
value <- c(rnorm(100,5000,200), rnorm(100,5050,210), rnorm(100,5100,215), 
           rnorm(100,5150,220), rnorm(100,5200,225))
MyData <- cbind.data.frame(value, variable)
ggplot(MyData, aes(x=value, fill=variable)) + geom_density(alpha = 0.5)

I think the answer may be associated with modifying scale_colour_grey and/or scale_manual but I don't understand enough to work this out myself. 我认为答案可能与修改scale_colour_grey和/或scale_manual但我不太了解自己解决这个问题。

This can be done by using a group aesthetic and making the fill for the density a light shade of grey like gray10 or gray20 : 这可以通过使用group美学并使密度fill为灰色的浅灰色gray10或灰色gray10 gray20

ggplot(MyData, aes(x=value, group=variable)) + geom_density(alpha = 0.5, fill="gray20")

Which will give you: 哪个会给你:

密度图

In your case you probably want gray10 , since you have 10 groups, rather than the 5 that are plotted here. 在你的情况下你可能想要gray10 ,因为你有10组,而不是这里绘制的5组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM