Reputation: 3106
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:
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)
What can I do to make the width of the boxplots bigger?
Upvotes: 1
Views: 4425
Reputation: 206242
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)
See how much easier this is with a reproducible example!
Upvotes: 1