图最小,最大值,每个x值的中位数

时间:2021-09-29 22:47:51

So I know the better way to approach this is to use the stat_summary() function, but this is to address a question presented in Hadley's R for Data Science book mostly for my own curiosity. It asks how to convert code for an example plot made using stat_summary() to make the same plot with geom_pointrange(). The example is:

因此,我知道更好的方法是使用stat_summary()函数,但这是为了解决Hadley的数据科学书籍中出现的一个问题,主要是出于我自己的好奇心。它询问如何将使用stat_summary()的示例图的代码转换为与地_pointrange()相同的图。的例子是:

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

And the plot should look like this:

情节应该是这样的:

pointrange plot http://r4ds.had.co.nz/visualize_files/figure-html/unnamed-chunk-35-1.png

pointrange情节http://r4ds.had.co.nz/visualize_files/figure - html/unnamed块- 35 - 1. png

I've attempted with code such as:

我曾尝试使用以下代码:

ggplot(data = diamonds, mapping = aes(x = cut, y = depth)) +
  geom_pointrange(mapping = aes(ymin = min(depth), ymax = max(depth)))

图最小,最大值,每个x值的中位数

However, this plots the min and max for all depth values across each cut category (i.e., all ymin's and ymax's are the same). I also tried passing a vector of mins and maxs, but ymin only takes single values as far as I can tell. It's probably something simple, but I think people mostly use stat_summary() as I've found very few examples of geom_pointrange() usage via Google.

然而,这将绘制每个切割类别的所有深度值的最小值和最大值(即。,所有的ymin和ymax都是一样的)。我还尝试传递一个min和maxs向量,但就我所知,ymin只接受单个值。这可能是一个简单的问题,但是我认为大多数人都使用stat_summary(),因为我在谷歌中找到了很少的一些例子来使用ge_pointrange()。

2 个解决方案

#1


3  

I think you need to do the summary outside the plot function to use geom_pointrange:

我认为你需要在plot函数之外做总结,使用geom_pointrange:

library(dplyr)
library(ggplot2)
summary_diamonds <- diamonds %>% 
    group_by(cut) %>% 
    summarise(lower = min(depth), upper = max(depth), p = median(depth))

ggplot(data = summary_diamonds, mapping = aes(x = cut, y = p)) +
    geom_pointrange(mapping = aes(ymin = lower, ymax = upper))

图最小,最大值,每个x值的中位数

#2


0  

geom_pointrange includes a stat argument, so you can do the statistical transformation inline https://*.com/a/41865061

geom_pointrange包含一个stat参数,因此您可以内联进行统计转换https://*.com/a/41865061

#1


3  

I think you need to do the summary outside the plot function to use geom_pointrange:

我认为你需要在plot函数之外做总结,使用geom_pointrange:

library(dplyr)
library(ggplot2)
summary_diamonds <- diamonds %>% 
    group_by(cut) %>% 
    summarise(lower = min(depth), upper = max(depth), p = median(depth))

ggplot(data = summary_diamonds, mapping = aes(x = cut, y = p)) +
    geom_pointrange(mapping = aes(ymin = lower, ymax = upper))

图最小,最大值,每个x值的中位数

#2


0  

geom_pointrange includes a stat argument, so you can do the statistical transformation inline https://*.com/a/41865061

geom_pointrange包含一个stat参数,因此您可以内联进行统计转换https://*.com/a/41865061