ggplot2中的密度直方图:标签栏高度 [英] density histogram in ggplot2: label bar height

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

问题描述

我有数据告诉我需要多少分钟才​​能完成任务:

  dat = data.frame(a = c(5.5,7,4,20,4.75,6,5,8.5,10,10.5,13.5,14,11))

我用ggplot2软件包绘制了数据的密度直方图:

  p = ggplot dat,aes(x = a))+ geom_histogram(aes(y = .. density ..),breaks = seq(4,20,by = 2))+ xlab(Required Solving Time)

现在我想添加每个密度条的顶部标签。我试图通过添加 + geom_text(label = .. density ..)来达到这个

这将返回错误


object'..density ..'找不到

但是,blockquote>

。有没有人知道 geom_text()函数的输入是否有
来获取这些标签?



没有 geom_text()的解决方案也可以,但我宁愿使用
来保留ggplot2包。

$ b $你可以用 ggplot_build()来完成:

  library(ggplot2)
dat = data.frame(a = c(5.5,7,4,20,4.75,6,5,8.5,10, (aes(y = .. density ..),breaks = seq(4,20))(
p = ggplot(dat,aes(x = a))+
geom_histogram ,by = 2))+ xlab(Required Solving Time)

ggplot_build(p)$ data
#[[1]]
#y count x xmin xmax density ncount ndensity PANEL group ymin ymax color fill size linetype alpha
#1 0.19230769 5 5 4 6 0.19230769 1.0 26.0 1 -1 0 0.19230769 NA grey35 0.5 1 NA
#2 0.03846154 1 7 6 8 0.03846154 0.2 5.2 1 -1 0 0.03846154 NA grey35 0.5 1 NA
#3 0.07692308 2 9 8 10 0.07692308 0.4 10.4 1 -1 0 0.07692308 NA grey35 0.5 1 NA
#4 0.07692308 2 11 10 12 0.07692308 0.4 10.4 1 -1 0 0.07692308 NA grey35 0.5 1 NA
#5 0.07692308 2 13 12 14 0.07692308 0.4 10.4 1 -1 0 0.07692308 NA grey35 0.5 1 NA
#6 0.00000000 0 15 14 16 0.00000000 0.0 0.0 1 -1 0 0.00000000 NA grey35 0.5 1 NA
#7 0.00000000 0 17 16 18 0.00000000 0.0 0.0 1 -1 0 0.00000000 NA grey35 0.5 1 NA
#8 0.03846154 1 19 18 20 0.03846154 0.2 5.2 1 -1 0 0.03846154 NA grey35 0.5 1 NA


p + geom_text(data = as.data.frame(ggplot_build(p)$ data),
aes(x = x,y = density,label = round(density, 2)),
nudge_y = 0.005)


I have data that tells me how many minutes were required to solve a task:

dat = data.frame(a = c(5.5,7,4,20,4.75,6,5,8.5,10,10.5,13.5,14,11))

I plotted a density histogram of the data with the ggplot2 package:

p=ggplot(dat, aes(x=a)) + geom_histogram(aes(y=..density..),breaks = seq(4,20,by=2))+xlab("Required Solving Time")

Now I would like to add labels of the height of every density bar on top of it. I tried to reach this by adding +geom_text(label=..density..). This returns the error

object '..density..' not found

however. Does anyone know what the input of the geom_text() function has to be in my case to get those labels?

A solution without geom_text() is fine too but I would rather prefer to stay within the ggplot2 package.

解决方案

You can do it with ggplot_build():

library(ggplot2)
dat = data.frame(a = c(5.5,7,4,20,4.75,6,5,8.5,10,10.5,13.5,14,11))
p=ggplot(dat, aes(x=a)) + 
   geom_histogram(aes(y=..density..),breaks = seq(4,20,by=2))+xlab("Required Solving Time")

ggplot_build(p)$data
#[[1]]
#          y count  x xmin xmax    density ncount ndensity PANEL group ymin       ymax colour   fill size linetype alpha
#1 0.19230769     5  5    4    6 0.19230769    1.0     26.0     1    -1    0 0.19230769     NA grey35  0.5        1    NA
#2 0.03846154     1  7    6    8 0.03846154    0.2      5.2     1    -1    0 0.03846154     NA grey35  0.5        1    NA
#3 0.07692308     2  9    8   10 0.07692308    0.4     10.4     1    -1    0 0.07692308     NA grey35  0.5        1    NA
#4 0.07692308     2 11   10   12 0.07692308    0.4     10.4     1    -1    0 0.07692308     NA grey35  0.5        1    NA
#5 0.07692308     2 13   12   14 0.07692308    0.4     10.4     1    -1    0 0.07692308     NA grey35  0.5        1    NA
#6 0.00000000     0 15   14   16 0.00000000    0.0      0.0     1    -1    0 0.00000000     NA grey35  0.5        1    NA
#7 0.00000000     0 17   16   18 0.00000000    0.0      0.0     1    -1    0 0.00000000     NA grey35  0.5        1    NA
#8 0.03846154     1 19   18   20 0.03846154    0.2      5.2     1    -1    0 0.03846154     NA grey35  0.5        1    NA


p + geom_text(data = as.data.frame(ggplot_build(p)$data), 
              aes(x=x, y= density , label = round(density,2)), 
              nudge_y = 0.005)

这篇关于ggplot2中的密度直方图:标签栏高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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