Question

Among many thousands of data sets my program occasionally encounters those containing only identical values, for which I need ggplot2 to plot the same as base R does: simply plot the median at its correct value. Is there a way for ggplot2 to do this?

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")
Was it helpful?

Solution

The median is correct in ggplot. Just 'zoom in' using coord_cartesian and have a look:

ggplot(df, aes(factor(station), value)) + 
  geom_boxplot() +
  coord_cartesian(ylim = c(0.00005, 0.00015))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top