所以,我有一个相当大的数据集(Dropbox:csv 文件)我正在尝试使用geom_boxplot
。以下产生了看似合理的情节:
require(reshape2)
require(ggplot2)
require(scales)
require(grid)
require(gridExtra)
df <- read.csv("\\Downloads\\boxplot.csv", na.strings = "*")
df$year <- factor(df$year, levels = c(2010,2011,2012,2013,2014), labels = c(2010,2011,2012,2013,2014))
d <- ggplot(data = df, aes(x = year, y = value)) +
geom_boxplot(aes(fill = station)) +
facet_grid(station~.) +
scale_y_continuous(limits = c(0, 15)) +
theme(legend.position = "none"))
d
然而,当你深入挖掘时,问题就会悄然出现,让我感到害怕。当我用它们的值标记箱线图中位数时,会产生以下结果。
df.m <- aggregate(value~year+station, data = df, FUN = function(x) median(x))
d <- d + geom_text(data = df.m, aes(x = year, y = value, label = value))
d
geom_boxplot 绘制的中位数根本不在中位数处。标签绘制在正确的 y 轴值处,但箱线图的中间铰链绝对不在中值处。我已经被这个问题难住了好几天了。
这是什么原因呢?如何使用正确的中位数来生成这种类型的显示?如何调试或诊断该图?