Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use R ggplot stat_summary to plot median and quartiles?

how to change the lower and upper point in this stat summary plot to 25% quartile and 75% quartile?

ggplot(data = diamonds) + stat_summary(
  mapping = aes(x = cut, y = depth),
  fun.ymin = min,
  fun.ymax = max,
  fun.y = median
)
like image 669
santoku Avatar asked Dec 10 '16 15:12

santoku


People also ask

What does Stat_summary do in R?

Description. stat_summary allows for tremendous flexibilty in the specification of summary functions. The summary function can either operate on a data frame (with argument name fun. data ) or on a vector ( fun.

What is Stat_summary?

stat_summary() operates on unique x or y ; stat_summary_bin() operates on binned x or y . They are more flexible versions of stat_bin() : instead of just counting, they can compute any aggregate.

What is fun args in R?

fun.args. A list of extra arguments to pass to fun. na.rm. If FALSE , the default, missing values are removed with a warning. If TRUE , missing values are silently removed.


2 Answers

ggplot(data = diamonds) + stat_summary(
  mapping = aes(x = cut, y = depth),
  fun.min = function(z) { quantile(z,0.25) },
  fun.max = function(z) { quantile(z,0.75) },
  fun = median)

Diamonds

like image 112
G5W Avatar answered Oct 02 '22 23:10

G5W


Rewriting G5W's solution, using the geom function instead of the stat function:

ggplot(data = diamonds) +
  geom_pointrange(mapping = aes(x = cut, y = depth),
                  stat = "summary",
                  fun.min = function(z) {quantile(z,0.25)},
                  fun.max = function(z) {quantile(z,0.75)},
                  fun = median)

enter image description here

like image 40
mpalanco Avatar answered Oct 03 '22 01:10

mpalanco