[R] Changing x-axis on boxplot
Dear List, I am creating a boxplot with two subsets, very similar to the example by Roger Bivand at ?boxplot (reproduced below). I am trying to change the labels on the x-axis to have one number to cover both subsets. I can do this in other plots by using axis=FALSE followed by a separate axis() command. I have also tried variations in the names= argument but can't get it to work. Ideally I would like tickmarks on either side of each factor with the number for the level centered between the two tick marks. Any suggestions? Thanks, Tim Example: boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == VC, col = yellow, main = Guinea Pigs' Tooth Growth, xlab = Vitamin C dose mg, ylab = tooth length, xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = i) boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == OJ, col = orange) legend(2, 9, c(Ascorbic acid, Orange juice), fill = c(yellow, orange)) Tim Clark Marine Ecologist National Park of American Samoa __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Changing x-axis on boxplot
Hi, to hide the axis generated by bxp you have to set axes=FALSE and frame.plot=FALSE and then you can plot the x-axis by using the axis() function example: boxplot(len ~ dose, data = ToothGrowth, frame.plot=FALSE,axes=FALSE, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == VC, col = yellow, main = Guinea Pigs' Tooth Growth, xlab = Vitamin C dose mg, ylab = tooth length, xlim = c(0.5, 3.5), ylim = c(0, 35)) boxplot(len ~ dose, data = ToothGrowth, add = TRUE, frame.plot=FALSE,axes=FALSE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == OJ, col = orange) axis(1,at=0:4) axis(2,at=(0:17)*2) legend(2, 9, c(Ascorbic acid, Orange juice), fill = c(yellow, orange)) Theresa Tim Clark wrote: Dear List, I am creating a boxplot with two subsets, very similar to the example by Roger Bivand at ?boxplot (reproduced below). I am trying to change the labels on the x-axis to have one number to cover both subsets. I can do this in other plots by using axis=FALSE followed by a separate axis() command. I have also tried variations in the names= argument but can't get it to work. Ideally I would like tickmarks on either side of each factor with the number for the level centered between the two tick marks. Any suggestions? Thanks, Tim Example: boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == VC, col = yellow, main = Guinea Pigs' Tooth Growth, xlab = Vitamin C dose mg, ylab = tooth length, xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = i) boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == OJ, col = orange) legend(2, 9, c(Ascorbic acid, Orange juice), fill = c(yellow, orange)) Tim Clark Marine Ecologist National Park of American Samoa __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Changing x-axis on boxplot
Hi: Here are a couple of ways: (1) Base graphics: add argument axes = FALSE to both plots, then add axes boxplot(len ~ dose, data = subset(ToothGrowth, supp == 'VC'), boxwex = 0.25, at = 1:3 - 0.2, col = yellow, main = Guinea Pigs' Tooth Growth, axes = FALSE, xlab = Vitamin C dose mg, ylab = Tooth length, xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = i) boxplot(len ~ dose, data = subset(ToothGrowth, supp == 'OJ'), add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, axes = FALSE, col = orange) axis(1, at = 1:3, labels = c(0.5, 1, 2)) axis(2) legend(2, 9, c(Ascorbic acid, Orange juice), fill = c(yellow, orange)) box() (2) ggplot2 (just for fun :) library(ggplot2) g - ggplot(ToothGrowth, aes(x = factor(dose), y = len)) g + geom_boxplot(aes(fill = supp), position = position_dodge(width = 0.9)) + scale_fill_manual('', breaks = c('OJ', 'VC'), values = c('orange', 'yellow'), labels = c('Orange juice', 'Ascorbic acid')) + theme_bw() + opts(legend.position = c(0.8, 0.2), legend.text = theme_text(size = 12)) + opts(panel.grid.major = theme_blank()) + xlab('Vitamin C dose (mg)') + ylab('Tooth length') + opts(title = Guinea pigs' tooth growth) + opts(plot.title = theme_text(size = 16)) HTH, Dennis On Sat, Sep 25, 2010 at 11:13 PM, Tim Clark mudiver1...@yahoo.com wrote: Dear List, I am creating a boxplot with two subsets, very similar to the example by Roger Bivand at ?boxplot (reproduced below). I am trying to change the labels on the x-axis to have one number to cover both subsets. I can do this in other plots by using axis=FALSE followed by a separate axis() command. I have also tried variations in the names= argument but can't get it to work. Ideally I would like tickmarks on either side of each factor with the number for the level centered between the two tick marks. Any suggestions? Thanks, Tim Example: boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == VC, col = yellow, main = Guinea Pigs' Tooth Growth, xlab = Vitamin C dose mg, ylab = tooth length, xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = i) boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == OJ, col = orange) legend(2, 9, c(Ascorbic acid, Orange juice), fill = c(yellow, orange)) Tim Clark Marine Ecologist National Park of American Samoa __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Changing x-axis on boxplot
Tim, Boxplots are nice, but I find that they can be somewhat misleading when applied to small groups, especially in the suggestion of spread differences. (Your real data may well be more extensive than the ToothGrowth data and so the following may be moot.) I prefer stripcharts for small groups. Compare the boxplot figure to the following (using Dennis's code as basis): stripchart(len ~ dose, data = subset(ToothGrowth, supp == 'VC'), method = jitter, jitter = .03, vertical = TRUE, at = 1:3 - 0.1, pch = 21, bg = yellow, cex = 1.5, main = Guinea Pigs' Tooth Growth, axes = FALSE, xlab = Vitamin C dose mg, ylab = Tooth length, xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = i) stripchart(len ~ dose, data = subset(ToothGrowth, supp == 'OJ'), add = TRUE, method = jitter, jitter = .03, vertical = TRUE, at = 1:3 + 0.1, axes = FALSE, pch = 21, bg = orange, cex = 1.5) axis(1, at = 1:3, labels = c(0.5, 1, 2)) axis(2) box() legend(2, 9, c(Ascorbic acid, Orange juice), pch = 21, pt.bg = c(yellow, orange), pt.cex = 1.5) -Peter Ehlers On 2010-09-26 4:48, Dennis Murphy wrote: Hi: Here are a couple of ways: (1) Base graphics: add argument axes = FALSE to both plots, then add axes boxplot(len ~ dose, data = subset(ToothGrowth, supp == 'VC'), boxwex = 0.25, at = 1:3 - 0.2, col = yellow, main = Guinea Pigs' Tooth Growth, axes = FALSE, xlab = Vitamin C dose mg, ylab = Tooth length, xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = i) boxplot(len ~ dose, data = subset(ToothGrowth, supp == 'OJ'), add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, axes = FALSE, col = orange) axis(1, at = 1:3, labels = c(0.5, 1, 2)) axis(2) legend(2, 9, c(Ascorbic acid, Orange juice), fill = c(yellow, orange)) box() (2) ggplot2 (just for fun :) library(ggplot2) g- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) g + geom_boxplot(aes(fill = supp), position = position_dodge(width = 0.9)) + scale_fill_manual('', breaks = c('OJ', 'VC'), values = c('orange', 'yellow'), labels = c('Orange juice', 'Ascorbic acid')) + theme_bw() + opts(legend.position = c(0.8, 0.2), legend.text = theme_text(size = 12)) + opts(panel.grid.major = theme_blank()) + xlab('Vitamin C dose (mg)') + ylab('Tooth length') + opts(title = Guinea pigs' tooth growth) + opts(plot.title = theme_text(size = 16)) HTH, Dennis On Sat, Sep 25, 2010 at 11:13 PM, Tim Clarkmudiver1...@yahoo.com wrote: Dear List, I am creating a boxplot with two subsets, very similar to the example by Roger Bivand at ?boxplot (reproduced below). I am trying to change the labels on the x-axis to have one number to cover both subsets. I can do this in other plots by using axis=FALSE followed by a separate axis() command. I have also tried variations in the names= argument but can't get it to work. Ideally I would like tickmarks on either side of each factor with the number for the level centered between the two tick marks. Any suggestions? Thanks, Tim Example: boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == VC, col = yellow, main = Guinea Pigs' Tooth Growth, xlab = Vitamin C dose mg, ylab = tooth length, xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = i) boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == OJ, col = orange) legend(2, 9, c(Ascorbic acid, Orange juice), fill = c(yellow, orange)) Tim Clark Marine Ecologist National Park of American Samoa __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.