Re: [R] Superscript in legend without using expression function
On Feb 7, 2015, at 2:54 PM, Rolf Turner wrote: > On 08/02/15 10:57, jgui001 wrote: >> I am plotting three sets of data on a single graph, and doing around 100+ >> graphs. >> I can use the expression function to superscript the 2 but that seems to >> force me to manually put in the R squared values. Is there away around this? >> >> This code will show what it should look like this but with the 2 >> superscripted >> >> r1<-c(0.59,0.9,0.6) >> plot(1:6) >> legend("topleft", >> legend=c(paste("G1 r=",r1[1]), paste("G2 r=",r1[2]), paste("G3 r=",r1[3]))) > > One way of accomplishing this is: > > r1<-c(0.59,0.9,0.6) > l3 <- c(as.expression(bquote(G[1]~~ r^2 == .(r1[1]))), >as.expression(bquote(G[2]~~ r^2 == .(r1[2]))), >as.expression(bquote(G[3]~~ r^2 == .(r1[3] > plot(1:6) > legend("topleft",legend=l3) This might be a bit more compact: r1<-c(0.59,0.9,0.6) plot(1:6) legend("topleft", leg=as.expression(lapply(1:3, function(n) bquote( G*.(n)~r^2==.(r1[n]) I didn't see an indication that there were supposed to be subscripted numerals after the "G"'s Initialy I tried just: lapply(1:3, function(n) bquote( G*.(n)~r^2==.(r1[n]))) But this didn't have the proper mode, which was solved by wrapping in as.espression thus returning the correctly constructed expression vector: expression(G * 1L ~ r^2 == 0.59, G * 2L ~ r^2 == 0.9, G * 3L ~ r^2 == 0.6) This could also be delivered slightly less economically with this editing of your effort: as.expression(c( bquote(G[1]~~ r^2 == .(r1[1])), bquote(G[2]~~ r^2 == .(r1[2])), bquote(G[3]~~ r^2 == .(r1[3])) ) ) I'm not sure but I think that expressions can be vectors but I don't think that there are "call vectors", only call lists. > Don't ask me to explain how this works. I just hammered and hoped till the > desired results were produced. > > There are other ways, I think, some of which may be less prolix. Someone else > may chime in and suggest a better way, but I think that the foregoing does > what you want. > > cheers, > > Rolf Turner > > -- > Rolf Turner > Technical Editor ANZJS > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > Home phone: +64-9-480-4619 > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] how to draw paired mosaic plot?
Hi meng, A basic display of mosaic plots for all pairs of variables isn't too difficult, but you will probably want to make this a bit fancier. It only displays the unique plots, unlike the "pairs" plot. Keep in mind that "many" variables will mean many plots. chardf<-data.frame(v1=sample(LETTERS[1:3],20,TRUE), v2=sample(LETTERS[4:6],20,TRUE), v3=sample(LETTERS[7:9],20,TRUE), v4=sample(LETTERS[10:12],20,TRUE)) mosaic_pairs<-function(x,...) { if(!is.data.frame(x) && !is.matrix(x)) stop("x must be a 2D matrix or data frame") nvar<-dim(x)[2] paircomb<-combn(nvar,2) nplots<-dim(paircomb)[2] split.screen(figs=c(nvar-1,nvar-1)) for(i in 1:nplots) { screen((paircomb[2,i]-1)+(paircomb[1,i]-1)*(nvar-1)) maintitle<- paste(names(x)[paircomb[1,i]],"by",names(x)[paircomb[2,i]]) par(mar=c(1,1,3,1)) mosaicplot(table(x[[paircomb[2,i]]],x[[paircomb[1,i]]]), main=maintitle,...) } } mosaic_pairs(chardf) Jim On Sun, Feb 8, 2015 at 1:50 AM, meng wrote: > If there are many character variables,and I want to get the mosaic plot of > every pair of each variable,how to do then? > > If the variables are numeric, I can use pairs to get paired scatter plot. > But as to the character variables, how to get the "paired mosaic plot"? > > Many thanks. > > > > > -- > QQ: 1733768559 > > > > At 2015-02-07 17:04:26,"Jim Lemon" wrote: >>Hi meng, >>It's not too hard to get a mosaic plot of two character variables: >> >>x<-sample(LETTERS[1:3],20,TRUE) >>y<-sample(LETTERS[24:26],20,TRUE) >>mosaicplot(table(x,y)) >> >>If you could tell us how the above is not what you want, perhaps a >>better suggestion will appear. >> >>Jim >> >> >>On Sat, Feb 7, 2015 at 6:29 PM, meng wrote: >>> If both x and y are all character, paired scatter plot is a little bit >>> strange I think. >>> >>> >>> >>> >>> >>> >>> -- >>> QQ: 1733768559 >>> >>> >>> >>> >>> >>> At 2015-02-06 23:52:34,"Duncan Murdoch" wrote: On 06/02/2015 6:46 AM, meng wrote: > Hi all: > If there are two numeric variable:x,y, and I can get paired scatter > plot by function "pairs".But if x and y are character, and I want to get > paired mosaic plot,which function should be used then? Why not pairs, with a custom panel function? There are examples on the help page, though I don't think a mosaic plot is there. Duncan Murdoch > > > Many thanks! > My best. > > > > > > > -- > QQ: 1733768559 > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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 -- To UNSUBSCRIBE and more, see >>> 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 -- To UNSUBSCRIBE and more, see 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] Superscript in legend without using expression function
On 08/02/15 10:57, jgui001 wrote: I am plotting three sets of data on a single graph, and doing around 100+ graphs. I can use the expression function to superscript the 2 but that seems to force me to manually put in the R squared values. Is there away around this? This code will show what it should look like this but with the 2 superscripted r1<-c(0.59,0.9,0.6) plot(1:6) legend("topleft", legend=c(paste("G1 r=",r1[1]), paste("G2 r=",r1[2]), paste("G3 r=",r1[3]))) One way of accomplishing this is: r1<-c(0.59,0.9,0.6) l3 <- c(as.expression(bquote(G[1]~~ r^2 == .(r1[1]))), as.expression(bquote(G[2]~~ r^2 == .(r1[2]))), as.expression(bquote(G[3]~~ r^2 == .(r1[3] plot(1:6) legend("topleft",legend=l3) Don't ask me to explain how this works. I just hammered and hoped till the desired results were produced. There are other ways, I think, some of which may be less prolix. Someone else may chime in and suggest a better way, but I think that the foregoing does what you want. cheers, Rolf Turner -- Rolf Turner Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 Home phone: +64-9-480-4619 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Plot residuals against standard normal distribution
On Sat, 7 Feb 2015, Mike Miller wrote: res <- residuals( model ) resStd <- ( res - mean( res, na.rm=TRUE ) ) / sd( res, na.rm=TRUE ) Another issue is how to make the theoretical quantiles for the normal distribution. There are a few methods: https://www.statsdirect.com/help/data_preparation/normal_scores.htm I usually use Blom: r <- rank( resStd ) c <- 3/8 N <- sum( !is.na( resStd ) ) resNorm <- qnorm( ( r - c ) / ( N - 2*c + 1 ) ) resNorm[ is.nan( resNorm ) ] <- NA Then you could plot it directly: plot(resNorm, resStd) When we use qqnorm() in R, it looks like R is using a Blom method with c=1/2 instead of c=3/8. I believe Blom recommended 3/8 and programs that offer Blom normal scores use c=3/8. I don't know if that was off-track because the OP was asking about density, but he also was asking about checking the distribution of residuals, so maybe this is appropriate. I should add, if you don't mind using R's c=1/2, you can get the normal scores very quickly this way: resNorm <- qqnorm( residuals( model ), plot.it=FALSE )$x Apparently, 11 years ago R was using c=3/8 in qqnorm(), so I guess it changed. Nordheim, Clayton and Yandell wrote about it in this document dated September 9, 2003: https://www.stat.wisc.edu/~yandell/st571/R/append8.pdf It is definitely using c=1/2 today. I don't know where that is documented. When I do a QQ-plot of uniform p-values, I like to add a confidence region with these lmits: qb95 <- qbeta(.95,1:N,N+1-(1:N)) qb05 <- qbeta(.05,1:N,N+1-(1:N)) If we have N observations from a normal distribution with unknown mean and variance, can we create some kind of analogous region on a qqnorm() kind of plot? It seems like there should be a way to get at least an approximate result using beta and t distributions, probably building on the qbeta() code above. Mike __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] how to draw paired mosaic plot?
If there are many character variables,and I want to get the mosaic plot of every pair of each variable,how to do then? If the variables are numeric, I can use pairs to get paired scatter plot. But as to the character variables, how to get the "paired mosaic plot"? Many thanks. -- QQ: 1733768559 At 2015-02-07 17:04:26,"Jim Lemon" wrote: >Hi meng, >It's not too hard to get a mosaic plot of two character variables: > >x<-sample(LETTERS[1:3],20,TRUE) >y<-sample(LETTERS[24:26],20,TRUE) >mosaicplot(table(x,y)) > >If you could tell us how the above is not what you want, perhaps a >better suggestion will appear. > >Jim > > >On Sat, Feb 7, 2015 at 6:29 PM, meng wrote: >> If both x and y are all character, paired scatter plot is a little bit >> strange I think. >> >> >> >> >> >> >> -- >> QQ: 1733768559 >> >> >> >> >> >> At 2015-02-06 23:52:34,"Duncan Murdoch" wrote: >>>On 06/02/2015 6:46 AM, meng wrote: Hi all: If there are two numeric variable:x,y, and I can get paired scatter plot by function "pairs".But if x and y are character, and I want to get paired mosaic plot,which function should be used then? >>> >>>Why not pairs, with a custom panel function? There are examples on the >>>help page, though I don't think a mosaic plot is there. >>> >>>Duncan Murdoch Many thanks! My best. -- QQ: 1733768559 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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 -- To UNSUBSCRIBE and more, see >> 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 -- To UNSUBSCRIBE and more, see 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] Superscript in legend without using expression function
I am plotting three sets of data on a single graph, and doing around 100+ graphs. I can use the expression function to superscript the 2 but that seems to force me to manually put in the R squared values. Is there away around this? This code will show what it should look like this but with the 2 superscripted r1<-c(0.59,0.9,0.6) plot(1:6) legend("topleft", legend=c(paste("G1 r=",r1[1]), paste("G2 r=",r1[2]), paste("G3 r=",r1[3]))) -- View this message in context: http://r.789695.n4.nabble.com/Superscript-in-legend-without-using-expression-function-tp4702929.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] how to draw paired mosaic plot?
Hi meng, It's not too hard to get a mosaic plot of two character variables: x<-sample(LETTERS[1:3],20,TRUE) y<-sample(LETTERS[24:26],20,TRUE) mosaicplot(table(x,y)) If you could tell us how the above is not what you want, perhaps a better suggestion will appear. Jim On Sat, Feb 7, 2015 at 6:29 PM, meng wrote: > If both x and y are all character, paired scatter plot is a little bit > strange I think. > > > > > > > -- > QQ: 1733768559 > > > > > > At 2015-02-06 23:52:34,"Duncan Murdoch" wrote: >>On 06/02/2015 6:46 AM, meng wrote: >>> Hi all: >>> If there are two numeric variable:x,y, and I can get paired scatter plot by >>> function "pairs".But if x and y are character, and I want to get paired >>> mosaic plot,which function should be used then? >> >>Why not pairs, with a custom panel function? There are examples on the >>help page, though I don't think a mosaic plot is there. >> >>Duncan Murdoch >>> >>> >>> Many thanks! >>> My best. >>> >>> >>> >>> >>> >>> >>> -- >>> QQ: 1733768559 >>> >>> >>> [[alternative HTML version deleted]] >>> >>> __ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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 -- To UNSUBSCRIBE and more, see > 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 -- To UNSUBSCRIBE and more, see 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] how to draw paired mosaic plot?
If both x and y are all character, paired scatter plot is a little bit strange I think. -- QQ: 1733768559 At 2015-02-06 23:52:34,"Duncan Murdoch" wrote: >On 06/02/2015 6:46 AM, meng wrote: >> Hi all: >> If there are two numeric variable:x,y, and I can get paired scatter plot by >> function "pairs".But if x and y are character, and I want to get paired >> mosaic plot,which function should be used then? > >Why not pairs, with a custom panel function? There are examples on the >help page, though I don't think a mosaic plot is there. > >Duncan Murdoch >> >> >> Many thanks! >> My best. >> >> >> >> >> >> >> -- >> QQ: 1733768559 >> >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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 -- To UNSUBSCRIBE and more, see 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] Plot residuals against standard normal distribution
On Mon, 2 Feb 2015, Mikael Olai Milhøj wrote: I'm having trouble trying to plot the density of the residuals against the standard normal distribution N(0,1). (I'm trying to see if my residuals are well-behaved). I know hwo to calculate the standardized residuals (I guess that there may be a simple way using a R function) and then plot this by using the density function y<-(model$residuals-mean(model$residuals))/sd(model$residuals) plot(density(y)) But I don't know how to add the N(0,1) curve. Any suggestions? Thanks in advance This isn't good for you? qqnorm( residuals( model ) ) Residuals usually have a zero mean, but I guess you can center, anyway, and if there could be NAs, you will need to deal with them: res <- residuals( model ) resStd <- ( res - mean( res, na.rm=TRUE ) ) / sd( res, na.rm=TRUE ) qqnorm( resStd ) Another issue is how to make the theoretical quantiles for the normal distribution. There are a few methods: https://www.statsdirect.com/help/data_preparation/normal_scores.htm I usually use Blom: r <- rank( resStd ) c <- 3/8 N <- sum( !is.na( resStd ) ) resNorm <- qnorm( ( r - c ) / ( N - 2*c + 1 ) ) resNorm[ is.nan( resNorm ) ] <- NA Then you could plot it directly: plot(resNorm, resStd) When we use qqnorm() in R, it looks like R is using a Blom method with c=1/2 instead of c=3/8. I believe Blom recommended 3/8 and programs that offer Blom normal scores use c=3/8. Best, Mike -- Michael B. Miller, Ph.D. Minnesota Center for Twin and Family Research Department of Psychology University of Minnesota http://scholar.google.com/citations?user=EV_phq4J __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.