在ggplot2中创建一个密度直方图? [英] Creating a density histogram in ggplot2?

查看:2083
本文介绍了在ggplot2中创建一个密度直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 ggplot2 创建下一个直方图密度图。以正常方式(基本包)非常简单:

  set.seed(46)
vector< ; - rnorm(500)
中断< - 分位数(向量,seq(0,1,by = 0.1))
标签= 1 :(长度(中断)-1)
den =密度(向量)
hist(df $向量,
休息=休息,
col =彩虹(长度(休息)),
概率= TRUE)
行(den)


使用ggplot我到目前为止:

 seg < -  cut(vector,break,
labels = labels,
include.lowest = TRUE,right = TRUE)
df = data。 frame(vector = vector,seg = seg)

ggplot(df)+
geom_histogram(breaks = break,
aes(x = vector,
y = ..)密度..,
fill = seg))+
geom_density(aes(x = vector,
y = .. density ..))

但是y尺度有错误的尺寸。我已经注意到下一次运行会得到y比例。

  ggplot(df)+ 
geom_histogram( break = break,
aes(x = vector,
y = .. density ..,
fill = seg))+
geom_density(aes(x = vector,
y = .. density ..))

我只是不理解它。 y = .. density .. 在那里,那应该是高度。那么为什么地球上的规模在我尝试填充时会被修改?



我确实需要颜色。我只想要一个直方图,根据默认的ggplot填充颜色对每个块的中断和颜色进行定向设置。

解决方案

手动添加颜色到百分比栏。看看这是否适合你。

  library(ggplot2)

ggplot(df,aes(x = vector))+
geom_histogram(break = break,aes(y = .. density ..),color =black,fill = c(red,orange,yellow,lightgreen,green, (aes(y = .. density ..))+
scale_x_continuous(breaks = c( - )) 3,-2,-1,0,1,2,3))+
ylab(Density)+ xlab(df $ vector)+ ggtitle(df $ vector的直方图)+
theme_bw()+ theme(plot.title = element_text(size = 20),
axis.title.y = element_text(size = 16,vjust = + 0.2),
axis.title。 x = element_text(size = 16,vjust = -0.2),
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())


I want to create the next histogram density plot with ggplot2. In the "normal" way (base packages) is really easy:

set.seed(46)
vector <- rnorm(500)  
breaks <- quantile(vector,seq(0,1,by=0.1))
labels = 1:(length(breaks)-1)
den = density(vector)
hist(df$vector,
     breaks=breaks,
     col=rainbow(length(breaks)),
     probability=TRUE)
lines(den)

With ggplot I have reached this so far:

seg <- cut(vector,breaks,
           labels=labels,
           include.lowest = TRUE, right = TRUE)
df = data.frame(vector=vector,seg=seg)

ggplot(df) + 
     geom_histogram(breaks=breaks,
                    aes(x=vector,
                        y=..density..,
                        fill=seg)) + 
     geom_density(aes(x=vector,
                      y=..density..))

But the "y" scale has the wrong dimension. I have noted that the next run gets the "y" scale right.

 ggplot(df) + 
     geom_histogram(breaks=breaks,
                    aes(x=vector,
                    y=..density..,
                    fill=seg)) + 
     geom_density(aes(x=vector,
                      y=..density..))

I just do not understand it. y=..density.. is there, that should be the height. So why on earth my scale gets modified when I try to fill it?

I do need the colours. I just want a histogram where the breaks and the colours of each block are directionally set according to the default ggplot fill colours.

解决方案

Manually, I added colors to your percentile bars. See if this works for you.

library(ggplot2)

ggplot(df, aes(x=vector)) +   
   geom_histogram(breaks=breaks,aes(y=..density..),colour="black",fill=c("red","orange","yellow","lightgreen","green","darkgreen","blue","darkblue","purple","pink")) + 
   geom_density(aes(y=..density..)) +
   scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3)) +
   ylab("Density") + xlab("df$vector") + ggtitle("Histogram of df$vector") +
   theme_bw() + theme(plot.title=element_text(size=20),
                      axis.title.y=element_text(size = 16, vjust=+0.2),
                      axis.title.x=element_text(size = 16, vjust=-0.2),
                      axis.text.y=element_text(size = 14),
                      axis.text.x=element_text(size = 14),
                      panel.grid.major = element_blank(),
                      panel.grid.minor = element_blank())

这篇关于在ggplot2中创建一个密度直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆