1

I am drawing a scatter plot in R and want to add boxplots. The x-axis are dates, the y-axis weights. I want to visualize the weight over time and so for each week, there should be a boxplot. However, the width of the boxplots are too small. This is what I currently have: Rplot

The boxplots are drawn like this:

data <- read.table("weight2.txt", header=TRUE)
data$datetime <- strptime(paste(data$Date, data$Time), "%d.%m.%Y %H:%M")
data$week <- strftime(data$datetime,format="%W")
data$timestamp <- as.numeric(data$datetime)

plot(data$datetime, data$Weight, xlab="Date",
    ylab="Weight", ylim=c(61,68))

library(plyr)
dt <- data.table(data)

aggrWeek <- ddply(dt,~week,summarise,
              min=min(Weight),
              firstQ=quantile(Weight,0.25),
              mean=mean(Weight),
              thirdQ=quantile(Weight,0.75),
              max=max(Weight),
              timestamp=mean(timestamp))
aggrWeek$datetime <- as.POSIXct(aggrWeek$timestamp, origin="1970-01-01")

boxplotData <- t(
    data.frame(aggrWeek$min, aggrWeek$firstQ, aggrWeek$mean, aggrWeek$thirdQ, aggrWeek$max))

bxp(list(
  stats=data.matrix(boxplotData), n=rep(1,ncol(boxplotData))), 
    add=TRUE, at=aggrWeek$datetime, show.names=FALSE)

weight2.txt

What can I do to make the width of the boxplots bigger?

r0f1
  • 2,717
  • 3
  • 26
  • 39
  • Use the `width` parameter to `bxp()`? – meuleman Aug 19 '14 at 18:56
  • 1
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) which includes sample data. My guess is that `width=` and `boxwex=` are working but you are setting them far too small if your date values are POSIXct – MrFlick Aug 19 '14 at 19:03
  • Updated post accordingly. – r0f1 Aug 19 '14 at 19:23

1 Answers1

1

Just as I figured, your x-values are POSIXt dates which are stored as a number of seconds since Jan-1-1970. So if you have a width of 1, you are essentially drawing a box plot that's "1 second" wide which in this range is very small. If you want a box plot that's about "1 day" wide, try

bxp(list(
  stats=data.matrix(boxplotData), n=rep(1,ncol(boxplotData))), 
    add=TRUE, at=aggrWeek$datetime, show.names=FALSE, boxwex=60*60*24)

enter image description here

See how much easier this is with a reproducible example!

MrFlick
  • 195,160
  • 17
  • 277
  • 295