Sean Zhang
2009-Jan-15 05:09 UTC
[R] misalignment of x-axis when overlaying two plots using latticeExtra
Dear R-helpers: I am an entry-level R user and have a question related to overlaying a barchart and and a xyplot using latticeExtra. My problem is that when I overlay them I fail to align their x-axes. I show my problem below through an example. #the example data frame is provided below vec <-c(1,5.056656,0.5977967,0.06126587,0.08557778, 2,4.601049,0.5995989,0.05002188,0.11410027, 3,4.932008,0.5502283,0.06727938,0.12531825, 4,4.763798,0.5499489,0.06473846,0.10752641, 5,4.944967,0.5328129,0.05445327,0.13663951, 6,5.063504,0.5267245,0.06477738,0.12380332, 7,4.735251,0.5528205,0.06851714,0.12196075, 8,5.141733,0.5304151,0.07965567,0.15123277, 9,5.215678,0.5219224,0.06694207,0.16476356, 10,4.930439,0.5712519,0.08591549,0.09710933, 11,5.075990,0.5615573,0.05778996,0.15361845, 12,4.909847,0.5683740,0.08711699,0.11189277, 13,4.863164,0.5652511,0.07222227,0.12071060, 14,5.173818,0.5564918,0.09830620,0.11831926, 15,4.762325,0.5345888,0.08792658,0.11738642, 16,5.046225,0.5268459,0.09574746,0.13254236, 17,4.902188,0.5370394,0.07194955,0.13164327, 18,4.865935,0.5446562,0.06894994,0.12645103, 19,5.204060,0.5650887,0.06726925,0.09242551, 20,5.208138,0.5765187,0.09282935,0.11053842) df<-as.data.frame( t(matrix(vec,nrow=5,ncol=20))) names(df)<-c("group","outcome","proportion_1","proportion_2","proportion_3") library(latticeExtra) library(lattice) #First generate barchart to plot the 3 proportions prop.data <-subset(df,select=c(proportion_1,proportion_2,proportion_3)) prop.tab <- as.table(as.matrix(prop.data)) barchart.obj<-barchart(prop.tab, stack=TRUE, horizontal = FALSE) #Second, generate the dots of outcome (I could have used type="l" but using type="p" makes the #misalignment of x-axis more obvious. dot.outcome <- xyplot(outcome~group,df,type="p", col="blue") #Last, overlay the two plots barchart.obj+ as.layer(dot.outcome,style=2,axes=c("y"), outside=TRUE) #Now, you should be able to see the x-axis of the two plots are not matching. #i.e., a dot is not at the center of its correspoding bar. How can I fix this? Your help will be highly appreciated. Many thanks in advance. -Sean [[alternative HTML version deleted]]
Felix Andrews
2009-Jan-15 12:55 UTC
[R] misalignment of x-axis when overlaying two plots using latticeExtra
Hi Sean, as.layer() does not do what you think it does: it does not attempt to plot things on a common scale, it simply draws the panels of two lattice plots in the same space. Actually, it is not very useful on its own. What you want is doubleYScale() in the development version (0.5-5) of latticeExtra. You can install it with install.packages("latticeExtra", repos="http://R-Forge.R-project.org") then doubleYScale(barchart.obj, dot.outcome) By the way, wise persons say plotting on multiple scales is often a bad idea. I would at least make sure both axes start from zero. -Felix 2009/1/15 Sean Zhang <seanecon at gmail.com>:> Dear R-helpers: > I am an entry-level R user and have a question related to overlaying a > barchart and and a xyplot using latticeExtra. > My problem is that when I overlay them I fail to align their x-axes. > I show my problem below through an example. > > #the example data frame is provided below > > vec <-c(1,5.056656,0.5977967,0.06126587,0.08557778, > 2,4.601049,0.5995989,0.05002188,0.11410027, > 3,4.932008,0.5502283,0.06727938,0.12531825, > 4,4.763798,0.5499489,0.06473846,0.10752641, > 5,4.944967,0.5328129,0.05445327,0.13663951, > 6,5.063504,0.5267245,0.06477738,0.12380332, > 7,4.735251,0.5528205,0.06851714,0.12196075, > 8,5.141733,0.5304151,0.07965567,0.15123277, > 9,5.215678,0.5219224,0.06694207,0.16476356, > 10,4.930439,0.5712519,0.08591549,0.09710933, > 11,5.075990,0.5615573,0.05778996,0.15361845, > 12,4.909847,0.5683740,0.08711699,0.11189277, > 13,4.863164,0.5652511,0.07222227,0.12071060, > 14,5.173818,0.5564918,0.09830620,0.11831926, > 15,4.762325,0.5345888,0.08792658,0.11738642, > 16,5.046225,0.5268459,0.09574746,0.13254236, > 17,4.902188,0.5370394,0.07194955,0.13164327, > 18,4.865935,0.5446562,0.06894994,0.12645103, > 19,5.204060,0.5650887,0.06726925,0.09242551, > 20,5.208138,0.5765187,0.09282935,0.11053842) > > df<-as.data.frame( t(matrix(vec,nrow=5,ncol=20))) > names(df)<-c("group","outcome","proportion_1","proportion_2","proportion_3") > > library(latticeExtra) > library(lattice) > > #First generate barchart to plot the 3 proportions > prop.data <-subset(df,select=c(proportion_1,proportion_2,proportion_3)) > prop.tab <- as.table(as.matrix(prop.data)) > barchart.obj<-barchart(prop.tab, stack=TRUE, horizontal = FALSE) > #Second, generate the dots of outcome (I could have used type="l" but using > type="p" makes the > #misalignment of x-axis more obvious. > dot.outcome <- xyplot(outcome~group,df,type="p", col="blue") > > #Last, overlay the two plots > barchart.obj+ as.layer(dot.outcome,style=2,axes=c("y"), outside=TRUE) > #Now, you should be able to see the x-axis of the two plots are not > matching. > #i.e., a dot is not at the center of its correspoding bar. > > How can I fix this? > > Your help will be highly appreciated. Many thanks in advance. > > -Sean > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Felix Andrews / ??? http://www.neurofractal.org/felix/ 3358 543D AAC6 22C2 D336 80D9 360B 72DD 3E4C F5D8