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
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 at 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.
>
>
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 <mudiver1200@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]]