R: ggplot2 pointrange example

2019-03-19 02:34发布

I am currently reading R for Data Science by Hadley Wickham. In that, there is the following example:

library(tidyverse)

ggplot(data = diamonds) + 
stat_summary(
    mapping = aes(x = cut, y = depth),
    fun.ymin = min,
    fun.ymax = max,
    fun.y = median
)

Now, there is a question as how to create the same plot by using appropriate geom_ function. I looked at the default geom for stat_summary and it is pointrange.

So I tried the following:

ggplot(data = diamonds) + geom_pointrange(mapping = aes(x = cut, y = depth), stat = "summary")

But I do not get the min and max points on the plot.

How do I get the exact plot by using geom_pointrange?

2条回答
Explosion°爆炸
2楼-- · 2019-03-19 02:56

The easy way I can think of is just using geom_line and stat_summary

 ggplot(data = diamonds, mapping = aes(x = cut, y = depth)) +
     geom_line() +
     stat_summary(fun.y = "median", geom = "point", size = 3)

This will give very similar plot. enter image description here

If I really want to use geom_pointrange, I would make small dataset first.

data = diamonds %>%
    group_by(cut) %>%
    summarise(min = min(depth), max = max(depth), 
        median = median(depth))

ggplot(data, aes(x = cut, y = median, ymin = min, ymax = max)) + 
    geom_linerange() + 
    geom_pointrange()

This would generate the exact same plot. Hope this helps! enter image description here

查看更多
Luminary・发光体
3楼-- · 2019-03-19 02:58

geom_pointrange does not automatically compute the ymin or ymax values. You can do this with stat = "summary" while still using geom_pointrange:

ggplot(data = diamonds) +
  geom_pointrange(mapping = aes(x = cut, y = depth),
                  stat = "summary",
                  fun.ymin = min,
                  fun.ymax = max,
                  fun.y = median)
查看更多
登录 后发表回答