最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Adding point on the top of each bar in geom_histogram ggplot - Stack Overflow

programmeradmin1浏览0评论

Consider this code

ggplot(mpg, aes(x = displ)) + 
  geom_histogram(aes(y = after_stat(density)), 
                 color = 'lightblue', size = .8)+
  geom_freqpoly(aes(y = after_stat(density)),
                color = 'red', size = .8)

I want to add points on the top of each bar, however using this

ggplot(mpg, aes(x = displ)) + 
  geom_histogram(aes(y = after_stat(density)),
                 color = 'lightblue', size = .8)+
  geom_freqpoly(aes(y = after_stat(density)),
                color = 'red', size = .8)+
  geom_point(aes(y = after_stat(density)))

produces the following error

Error in `geom_point()`:
! Problem while mapping stat to aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `map_statistic()`:
! Aesthetics must be valid computed stats.
✖ The following aesthetics are invalid:
✖ `y = after_stat(density)`
ℹ Did you map your stat in the wrong layer?

I would like to add the point adding only geom_point() is it possible?

Consider this code

ggplot(mpg, aes(x = displ)) + 
  geom_histogram(aes(y = after_stat(density)), 
                 color = 'lightblue', size = .8)+
  geom_freqpoly(aes(y = after_stat(density)),
                color = 'red', size = .8)

I want to add points on the top of each bar, however using this

ggplot(mpg, aes(x = displ)) + 
  geom_histogram(aes(y = after_stat(density)),
                 color = 'lightblue', size = .8)+
  geom_freqpoly(aes(y = after_stat(density)),
                color = 'red', size = .8)+
  geom_point(aes(y = after_stat(density)))

produces the following error

Error in `geom_point()`:
! Problem while mapping stat to aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `map_statistic()`:
! Aesthetics must be valid computed stats.
✖ The following aesthetics are invalid:
✖ `y = after_stat(density)`
ℹ Did you map your stat in the wrong layer?

I would like to add the point adding only geom_point() is it possible?

Share Improve this question edited Mar 19 at 15:13 M-- 29.5k10 gold badges69 silver badges106 bronze badges Recognized by R Language Collective asked Mar 19 at 14:49 LauraLaura 63117 silver badges43 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Use

ggplot(mpg, aes(x = displ)) + 
  geom_histogram(aes(y = after_stat(density)),color = 'lightblue', size = .8)+
  geom_freqpoly(aes(y = after_stat(density)),color = 'red', size = .8)+
  geom_point(aes(y = after_stat(density)), stat = "bin")

If you look at the help pages for ?geom_histogram and ?geom_freqpoly you'll see they have default stat="bin" while ?geom_point has the default stat="identity". You need to use the right stat= parameter that provides the correct after_stat you are trying to use in that layer.

We can use stat_bin:

library(ggplot2)

ggplot(mpg, aes(x = displ, y = after_stat(density))) + 
  geom_histogram(color = 'lightblue', size = .8)+
  geom_freqpoly(color = 'red', size = .8) +
  stat_bin(geom = "point")

发布评论

评论列表(0)

  1. 暂无评论