11
votes
$\begingroup$

I need to build a boxplot without any axes and add it to the current plot (ROC curve), but I need to add more text information to the boxplot: the labels for min and max. Current line of code is below (current graph also).

Thanks a lot for assistance.

boxplot(data, horizontal = TRUE, range = 0, axes=FALSE, col = "grey", add = TRUE)

The other solution is add the line from 0 to 1 (instead of x-axis), but I want it to go through the central line...for example like this graphic

enter image description here

$\endgroup$

4 Answers 4

9
votes
$\begingroup$

I think you will find this produces something like your hand-drawn diagram.

data    <- c(0.4, 0.7, 0.75, 0.82, 0.9)
endaxis <- c(0, 1)  # endpoints of axis
datamm  <- c(min(data), max(data))
boxplot(data, horizontal = TRUE, range = 0, ylim = endaxis,
                    axes = FALSE, col = "grey", add = FALSE)
arrows(endaxis, 1,  datamm, 1,  code = 1, angle = 90, length = 0.1)
valuelabels <- c(endaxis[1], round(fivenum(data)[2], digits = 2) ,
                 round(fivenum(data)[4], digits = 2), endaxis[2]  ) 
text(x = valuelabels, y = c(1.05, 1.25, 1.25, 1.05), labels = valuelabels)

R boxplot with value labels

There are probably better ways of doing it. You may need to adapt it to fit your ROC plot, including changing add = FALSE

$\endgroup$
1
  • 1
    $\begingroup$ Well, your response seems more close to what was asked by the OP (so I've +1). However, I feel like this isn't a boxplot anymore, or at least it loose its interest in spotting possible outlying value. Of note, you can customize a little bit more the boxplot (see the pars arguments to reduce its aspect ratio (boxwex) and the size of the whiskers (staplewex)). $\endgroup$
    – chl
    Mar 13, 2011 at 9:08
8
votes
$\begingroup$

Try something like this for a standalone version:

bxp <- boxplot(rnorm(100), horizontal=TRUE, axes=FALSE)
mtext(c("Min","Max"), side=3, at=bxp$stats[c(1,5)], line=-3)

Note that you can get some information when calling boxplot, in particular the "five numbers".

If you want it to be superimposed onto another graphic, use add=T but replace mtext by text; you will need to set a $y$ value (which depend on the way you plot the other graphic).

A more complete example was given by John Maindonald (code should be on his website):

enter image description here

$\endgroup$
3
votes
$\begingroup$

Fully customizable ggplot2 boxplot...

#bootstrap
data <- data.frame(value=rnorm(100,mean = 0.5, sd = 0.2),group=0)
#processing
metaData <- ddply(data,~group,summarise,
            mean=mean(data$value),
			sd=sd(data$value),
            min=min(data$value),
			max=max(data$value),
            median=median(data$value),
			Q1=0,Q3=0
			)
bps <- boxplot.stats(data$value,coef=1.5) 
metaData$min <- bps$stats[1] #lower wisker
metaData$max <- bps$stats[5] #upper wisker
metaData$Q1 <- bps$stats[2] # 1st Quartile
metaData$Q3 <- bps$stats[4] # 3rd Quartile

#adding outliers
out <- data.frame() #initialising storage for outliers
if(length(bps$out) > 0){
	for(n in 1:length(bps$out)){
		pt <-data.frame(value=bps$out[n],group=0) 
		out<-rbind(out,pt) 
	}
}
#adding labels
labels <-data.frame(value=metaData$max, label="Upper bound")
labels <-rbind(labels,data.frame(value=metaData$min, label="Lower bound"))
labels <-rbind(labels,data.frame(value=metaData$median, label="Median"))
labels <-rbind(labels,data.frame(value=metaData$Q1, label="First quartile"))
labels <-rbind(labels,data.frame(value=metaData$Q3, label="Third quartile"))

#drawing
library(ggplot2)
p <- ggplot(metaData,aes(x=group,y=mean))
p <- p + geom_segment(aes(x=c(0.1,0,0.1),y=c(0,0,1),xend=c(0,0,-0.1),yend=c(0,1,1)))
p <- p + geom_text(aes(y=c(0,1),label=c(0,1),x=0.2))
p <- p + geom_errorbar(aes(ymin=min,ymax=max),linetype = 1,width = 0.5) #main range
p <- p + geom_linerange(aes(ymin=min,ymax=max),linetype = 1,width = 0, color="white")# white line range
p <- p + geom_linerange(aes(ymin=min,ymax=max),linetype = 2)    #main range dotted
p <- p + geom_crossbar(aes(y=median,,ymin=Q1,ymax=Q3),linetype = 1,fill='white') #box
if(length(out) >0) p <- p + geom_point(data=out,aes(x=group,y=value),shape=4) # drawning outliers if any
p <- p + scale_x_discrete(breaks=c(0))
p <- p + scale_y_continuous(name= "Value")
p <- p + geom_text(data=labels,aes(x=0.5,y=value,label=round(value,2)),colour="black",angle=0,hjust=0.5, vjust=0.5,size=3)

p <- p + opts(panel.background = theme_rect(fill = "white",colour = NA)) 
p <- p + opts(panel.grid.minor = theme_blank(), panel.grid.major = theme_blank())
p <- p + opts(axis.title.x=theme_blank())
p <- p + opts(axis.text.x = theme_blank())
p <- p + opts(axis.title.y=theme_blank())
p <- p + opts(axis.text.y = theme_blank())

p + coord_flip()

Result:

enter image description here

...code maybe a bit ugly but works the right way.

$\endgroup$
0
2
votes
$\begingroup$

Here is mine implementation of your solutions. I've decided not to map the mean value, there is not to much space left. Also the line from 0 to 1 seems odd. Thank's a lot everyone.

data <- read.table("roc_average.txt")
bxp <- boxplot(data, horizontal = TRUE, range = 0, axes = FALSE, col = "grey", add = TRUE, at = 0.2, varwidth=FALSE, boxwex=0.3)
valuelabels <- c(round(fivenum(data)[2], digits = 2), round(fivenum(data)[4], digits = 2))
text(x = valuelabels, y = c(0.35, 0.35), labels = valuelabels, font = 2)
mtext(c(min(round(data, digits = 2)),max(round(data, digits = 2))), side=1, at=bxp$stats[c(1,5)], line=-3, font = 2)

roc curve with boxplot

$\endgroup$
5
  • $\begingroup$ The boxplot is supposed to summarize the AUC? If so, why is the min value = 0.5? $\endgroup$
    – chl
    Mar 17, 2011 at 22:22
  • $\begingroup$ yep, seems strange, because several ROC should be below 0.5. Digging up what's wrong... $\endgroup$ Mar 19, 2011 at 12:39
  • $\begingroup$ That is the point I have inverted the ROC AUC for values below 0.5, so the graphics should be remade. Thanks a lot! $\endgroup$ Apr 6, 2011 at 13:06
  • $\begingroup$ +1 for coming back, I'm waiting for your update. If you're working with different classifiers, you might have a look at ROCR. $\endgroup$
    – chl
    Apr 6, 2011 at 19:10
  • $\begingroup$ I did ROC curve with ROCR $\endgroup$ Apr 7, 2011 at 12:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.