[R] dotchart vs. dotplot ... groups

Brian Diggs diggsb at ohsu.edu
Thu Aug 18 21:06:16 CEST 2011


On 8/17/2011 11:29 AM, mkzodet at comcast.net wrote:
> I'm trying to create a dotplot with some grouping.
>
> I've been able to create what I want using dotchart (basic
> graphics), but can't quite get it using dotplot (lattice). I prefer
> to use lattice (or ggplot2) because I think it's a bit easier to
> control some other aspects of the plot appearance.
>
> Basically, w/ lattice I've not been able to get the y-axis label to
> include the group variable.
>
> I'd like...
>
> A
> 1
> 2
> B
> 1
> 2
> 3
> 4
> C
> 1
> 2
> 3
>
> I'm getting...
> 1
> 2
> 1
> 2
> 3
> 4
> 1
> 2
> 3
>
> The following example code illustrates....
>
> set.seed(18)
> dta<- data.frame(var1=factor(c("A", "A", "B", "B", "B", "B", "C", "C", "C")),
> var2=c(1,2,1,2,3,4,1,2,3),
> var3=round(runif(9,1,10),1),
> plotorder=9:1)
>
> dta
>
> windows(3,3)
> dotchart(dta$var3[order(dta$var1, -dta$var2)], groups=dta$var1,
> labels=dta$var2[order(dta$var1, -dta$var2)], cex=.75,
> gcolor=c("blue", "red", "dark green"),
> col=c(rep("blue",2), rep("red",4), rep("dark green",3)),
> axes=NULL)
>
>
> windows(3,3)
> dotplot(data=dta, plotorder~var3, groups=var1, col=c("blue", "red", "dark green"),
> scales=list(y=list(labels=dta$var2[order(dta$plotorder)])))

Something similar (though still not what you wanted) using ggplot would be:

dev.new(width=3,height=3)
ggplot(dta, aes(x=var3, y=var2)) +
	geom_point() +
	scale_y_reverse(breaks=1:4, expand=c(0,0.5)) +
	facet_grid(var1~., scales="free", space="free") +
	theme_bw()


> Thanks.
>
> Marc
> R 2.13.0 (2011-04-13)
> Windows XP
>
>
> 	[[alternative HTML version deleted]]
>


-- 
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University



More information about the R-help mailing list