Reputation: 6874
I want to plot a boxplot within another plot
So far I have:
require(ggplot2)
require(grid)
plot(unlist(cbind6),type="p",cex=1.5,xlab="Sample", ylab="CNI", pch=21,bg="red", main= "CNAs")
pp<-boxplot(combined,main="CNAs", xlab="Samples", ylab="CNVs",pch=20, outline=FALSE,col=c("red","green","black"))
print(pp, vp=viewport(.8, .75, .2, .2))
However when I try to run this I line that runs plot(unlist(cbind6).... runs fine but the boxplot does not get put into the left hand corner as its supposed to and instead I get a an output called $stats and $n and conf etc which I assume are boxplot stats. How do I get the plot as I want and why cant I get the boxplot to print?
Upvotes: 0
Views: 78
Reputation: 93811
boxplot
is a base graphic, as noted by @KonradRudolph. When you try to assign a base boxplot to an object, you get a list of data used to generate the plot, rather than a plot object:
pp = boxplot(mpg ~ carb, data=mtcars)
pp
$stats
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 18.10 15.2 15.20 10.40 19.7 15
[2,] 21.45 18.7 15.80 13.30 19.7 15
[3,] 22.80 22.1 16.40 15.25 19.7 15
[4,] 29.85 26.0 16.85 19.20 19.7 15
[5,] 33.90 30.4 17.30 21.00 19.7 15
$n
[1] 7 10 3 10 1 1
$conf
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 17.78366 18.45263 15.44218 12.30212 19.7 15
[2,] 27.81634 25.74737 17.35782 18.19788 19.7 15
$out
numeric(0)
$group
numeric(0)
$names
[1] "1" "2" "3" "4" "6" "8"
There are ways to save plots generated in base graphics as objects (see here, for example), but you might find it easier to use ggplot2.
pp=ggplot(mtcars, aes(x=factor(carb), y=mpg)) +
geom_boxplot()
pp
print(pp, vp=viewport(.8, .75, .2, .2))
Upvotes: 1