简体   繁体   English

如何使用 ggplot 进行 plot 多个分布?

[英]How to plot multiple distributions with ggplot?

ggplot 图像

I plotted this trying to understand how to plot the distribution of each singular feature of my dataframe.我绘制这个试图了解如何 plot 我的 dataframe 的每个奇异特征的分布。 So, trying to understand if my procedure was correct I implemented this code to plot rapidly two features.因此,试图了解我的程序是否正确,我将此代码快速实现为 plot 两个功能。

New <- c(Carm[,3],Carm[,4])
Names <-names(Carm)
Label <-c(Names[1],Names[2])
dat <- data.frame(New)

names(dat)[1] <- Label[1]
names(dat)[2] <- Label[2]
dat <- stack(dat) #I built a new smaller db taking only two features
# Now I use ggplot
ggplot(dat, aes(x=values)) + 

geom_histogram(binwidth = 0.5, color = "black",fill ="white")  +

geom_density(aes(group=ind, colour=ind, fill=ind), alpha=0.2)    +

facet_wrap( ~ ind, ncol=2)

So, my question is: why the densities are so small if compared to the histograms?所以,我的问题是:与直方图相比,为什么密度如此之小? How can I fix it?我该如何解决?

To plot a density histogram, it needs to be told not to plot counts.给 plot 一个密度直方图,需要告诉它不要对 plot 计数。 This is done mapping the aesthetic y =..density.. .这是通过映射美学y =..density..来完成的。 See section Computed variables in help('geom_histogram') .请参阅help('geom_histogram')中的计算变量部分。 I will use built-in data set iris as the example data set.我将使用内置数据集iris作为示例数据集。

library(ggplot2)

ggplot(dat, aes(values)) +
  geom_histogram(aes(y = ..density..), bins = 20, color = "black", fill ="white") +
  geom_density(aes(fill = ind), alpha = 0.2) +
  facet_wrap(~ ind)

在此处输入图像描述

Data数据

library(dplyr)
library(tidyr)

iris[iris$Species == "virginica", 3:4] %>% 
  pivot_longer(everything(), 
               names_to = "ind", 
               values_to = "values") -> dat

This is because while geom_histogram plots counts per bin, while geom density scales the data and represents what proportion of the data is per bin这是因为 geom_histogram 绘制每个 bin 的计数,而 geom density 缩放数据并表示每个 bin 的数据比例

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

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