ggplot2 boxplot medians aren't plotting as expected

前端 未结 1 1290
你的背包
你的背包 2021-01-02 05:59

So, I have a fairly large dataset (Dropbox: csv file) that I\'m trying to plot using geom_boxplot. The following produces what appears to be a reasonable plot:<

1条回答
  •  -上瘾入骨i
    2021-01-02 06:34

    The solution to this question is in the application of scale_y_continuous. ggplot2 will perform operations in the following order:

    1. Scale Transformations
    2. Statistical Computations
    3. Coordinate Transformations

    In this case, because a scale transformation is invoked, ggplot2 excludes data outside the scale limits for the statistical computation of the boxplot hinges. The medians calculated by the aggregate function and used in the geom_text instruction will use the entire dataset, however. This can result in different median hinges and text labels.

    The solution is to omit the scale_y_continuous instruction and instead use:

    d <- ggplot(data = df, aes(x = year, y = value)) +
    geom_boxplot(aes(fill = station)) + 
    facet_grid(station~.) +
    theme(legend.position = "none")) +
    coord_cartesian(y = c(0,15))
    

    This allows ggplot2 to calculate the boxplot hinge stats using the entire dataset, while limiting the plot size of the figure.

    0 讨论(0)
提交回复
热议问题