0

在成千上万的数据集中,我的程序偶尔会遇到那些只包含相同值的数据集,为此我需要 ggplot2 绘制与base 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")
4

1 回答 1

0

ggplot 中的中位数是正确的。只需“放大”coord_cartesian并查看:

ggplot(df, aes(factor(station), value)) + 
  geom_boxplot() +
  coord_cartesian(ylim = c(0.00005, 0.00015))
于 2013-09-04T19:54:58.427 回答