Add stripplot points to bwplot (Lattice graphics in R) - r

##Example data to illustrate problem:
type=c("grp1","grp2","grp1","grp3","grp3","grp3","grp4")
num=c(1,1,2,4,3,5,1)
cols=c(rep("red",5),"green","red")
library(lattice)
bwplot(num~type)
par(new=T)
stripplot(num~type,col=cols)
I like the additional information displayed by the box plot but I need the information conveyed by the coloured points in the strip chart. Obviously par(new=T) doesn't work, so how can I overlay the points onto the box plot?

You can define a panel function like this :
library(lattice)
bwplot(num~type,panel=function(x,y,...){
panel.bwplot(x,y,...)
panel.stripplot(x,y,col=cols,...)
})

the answer by agstudy is quite good, though as I pointed out in a comment, any outliers plotted by bwplot will also be plotted by stripplot, which could get confusing.
Note this is only an issue if you were using jitter.data=TRUE as an option to stripplot, otherwise the duplicate points would plot directly over top of one another and you'd never know they were there. If the data had outliers, and you used jitter.data=TRUE, you'd see twice as many outliers as you should.
the following approach suppresses the outliers from bwplot to avoid this issue:
bwstrip <- function(x,y,...){
panel.stripplot(x,y,col=cols,do.out=FALSE,jitter.data=TRUE,...)
panel.bwplot(x,y,...)
}
bwplot(lobe.depths,panel=bwstrip)

Related

How to determine x, y coordinates when adding text in metafor

I’m using the metafor package to create Forest plots in R. I’d like to add text to my plots to create labels using the text() function. I’m wondering what the simplest way is to determine the x,y coordinates of where I want my text to go. Currently I just guess and see how it looks and then edit as necessary. Is there a way to overlay a grid over my plot or something to guide me (and then remove it after)?
Thank you!
Start by saving what forest() returns:
x <- forest(res)
And then take a look at x. Among other things, it contains xlim and ylim, which are the x-axis and y-axis limits. Try this:
abline(v=x$xlim)
abline(h=x$ylim)
Also useful:
text(x$xlim[1], x$rows, x$rows, xpd=NA)
I hope this helps.

Ternary Plot detail/clipping in R

I have used triax.plot to create a ternary plot.
require(plotrix)
data <- read.csv("~/R/datasets/data.csv")
triax.plot(data[1:18721,],main="ternary plot", tick.labels=list(b=seq(10,90,by=10),l=seq(10,90,by=10),r=seq(10,90,by=10)), pch=4)
However most of the data forms a cluster that I want to display in more detail. I would like to edit the axis range (not the labels, not the ticks) to display only the relevant area of the original plot
highlighted in this image. The resulting clipping would also be a isosceles triangle.
Apparently triax.plot has no option for this. Or am I missing something? Any suggestions for alternative approaches are appreciated. Thanks in advance!

Hiding Data Point on qplot

I have downloaded the directlabels package in R to further enhance the ggplot2 experience, however I would like to delete the data point of a scatterplot once I have added the labels to them. Is there any way to hide these? My code goes something like this:
q<-qplot(x,y)+geom_point(aes(colour=z))
direct.label(q,list(cex=0.75,fontface="bold",bumpup))
But I'm not sure where the command to hide the data point would be. I would use first.qp but in this case I get the error
Error in order.labels(d) : labels are not aligned
so is there a better way of doing this?
You example is not reproducible. So I will just answer this question:
"I would like to delete the data point of a scatterplot once I
have added the labels to them."
You can easily for example remove a layer from the gg object. First I create a ggplot2 example and I decorate it using direct.label.
library(directlabels)
scatter <- qplot(jitter(hwy),jitter(cty),data=mpg,colour=class,
main="Fuel efficiency depends on car size")
scatter <- direct.label(scatter,list(cex=0.7,bumpup))
I remaove the first layer now(the geom_point layer)
scatter$layers[1] <- NULL
Then you get this plot , as you see I had only labels without points:
scatter

how to know what category corresponds to each point in a plot in R?

I just wanted how know what category matches with everypoint when I do this:
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group)
I know how to make a legend and put text with an arbitrary vector c('A','B',C'), but is there a more "automatic" way for doing this? This is an easy example but I need to do it with residuals or survival functions plot
Thank you in advance.
The traditional graphics system provides the legend function for adding a
legend or key to a plot. But It should be noted that it is entirely the responsibility of the user to ensure that the legend corresponds to the plot. There is no automatic checking that
data symbols in the legend match those in the plot. It is simpler to do it using lattice or ggplot2. for example:
library(lattice)
xyplot(y~x,groups=group,auto.key=T)
if you want absolutly to use base graphics, you can do this :
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group,pch=as.numeric(group))
legend(2, 500, c('A','B','C'),
cex=1.5, pch=1:3,col=1:3)

R - Scatter plots, how to plot points in differnt lines to overlapping?

I want to plot several lists of points, each list has distance (decimal) and error_no (1-8). So far I am using the following:
plot(b1$dist1, b1$e1, col="blue",type="p", pch=20, cex=.5)
points(b1$dist2, b1$e2, col="blue", pch=22)
to add them both to the same plot. (I will add legends, etc later on).
The problem I have is that points overlap, and even when changing the character using for plotting, it covers up previous points. Since I am planning on plotting a lot more than just 2 this will be a big problem.
I found some ways in:
http://www.rensenieuwenhuis.nl/r-sessions-13-overlapping-data-points/
But I would rather do something that would space the points along the y axis, one way would be to add .1, then .2, and so on, but I was wondering if there was any package to do that for me.
Cheers
M
ps: if I missed something, please let me know.
As noted in the very first point in the link you posted, jitter will slightly move all your points. If you just want to move the points on the y-axis:
plot(b1$dist1, b1$e1, col="blue",type="p", pch=20, cex=.5)
points(b1$dist2, jitter(b1$e2), col="blue", pch=22)
Depends a lot on what information you wish to impart to the reader of your chart. A common solution is to use the transparency quality of R's color specification. Instead of calling a color "blue" for example, set the color to #0000FF44 (Apologies if I just set it to red or green) The final two bytes define the transparency, from 00 to FF, so overlapping data points will appear darker than standalone points.
Look at the spread.labs function in the TeachingDemos package, particularly the example. It may be that you can use that function to create your plot (the examples deal with labels, but could just as easily be applied to the points themselves). The key is that you will need to find the new locations based on the combined data, then plot. If the function as is does not do what you want, you could still look at the code and use the ideas to spread out your points.
Another approach would be to restructure your data and use the ggplot2 package with "dodging". Other approaches rather than using points several times would be the matplot function, using the col argument to plot with a vector, or lattice or ggplot2 plots. You will probably need to restructure the data for any of these.

Resources