我的程序偶尔会遇到数千个数据集 那些只包含相同值的那些,我需要ggplot2 绘制与基数R相同的绘图:简单地绘制正确的中位数 值。 ggplot2有没有办法做到这一点?
dput(df)
structure(list(station = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "x", class = "factor"), value = c(1e-04,
1e-04, 1e-04, 1e-04, 1e-04, 1e-04, 1e-04, 1e-04, 1e-04, 1e-04
)), .Names = c("station", "value"), class = "data.frame", row.names = c(NA,
-10L))
station value
x 1.00E-04
x 1.00E-04
x 1.00E-04
x 1.00E-04
x 1.00E-04
x 1.00E-04
x 1.00E-04
x 1.00E-04
x 1.00E-04
x 1.00E-04
# via base R:
boxplot(df$value ~ df$station,
main="base R: correct median, \ncorrect data range")
# via ggplot2:
library(ggplot2)
ggplot(df, aes(factor(df$station), df$value)) +
geom_boxplot() +
ggtitle("ggplot2: incorrect median, \nincorrect data range")
答案 0 :(得分:0)
ggplot中的中位数是正确的。只需使用coord_cartesian
“放大”并查看:
ggplot(df, aes(factor(station), value)) +
geom_boxplot() +
coord_cartesian(ylim = c(0.00005, 0.00015))