Adding a factor legend to a scatterplot matrix in R? - r

I have made a scatterplot matrix which is conditioned on two factors, one with different colours and one with different shapes. I want to add a legend to the right-middle of the plot showing the labels for just the factor with the shapes (Scenario), is this possible? I have looked online but cannot figure it out! Here is my code, I would be grateful for any help. Thank you.
pairs(Data[,1:3], col=Data$Physician, pch=Data$Scenario, main="Scatterplot Matrix")

Related

viewing rownames in a heatmap in R

My aim is creating a heatmap with a continuous legend. I tried a few paths and ended up with pheatmap. Legend is great, names of columns are ok, but the names of the rows don't show. I'd also like plot as squares not rectagles. Any ideas?

ggplot2: Can I add the strip labels to every plot with facet_grid()?

Here is a typical case for using multiple variables in the rows or columns in facet_grid().
My question is how can I add the strip labels to every plot? For instance, the second row only contains the strip label "f" in the y-axis. I need all six plots all get two strip labels in X and Y.
I am new to ggplot2 and R, I hope you can help me. Thank you all so much.

How to add continuous geom_vline to multiple facets in ggplot2

data <- data.frame(x=c(1,2,3,4,5,6,7,8,9,10),y1=c(-2,-7,-12,-17,-22,-27,-32,-37,-42,-47),y2=c(1003,2003,3003,4003,5003,6003,7003,8003,9003,10003),y3=c(-3,-6,-9,-12,-15,-18,-21,-24,-27,-30))
I want to draw three scatter plots in ggplot2, with the same independent variable(x) and three different dependent variables(y1,y2,y3). Then add continuous geom_vline(geom_vline(xintercept=3.2)) to them like the following picture, but I don't know how to do it. Is there anyone can help me?

Legend with rainbow colours R

I have been trying to create a legend for an R plot with the rainbow option but I am facing some difficulties.
I plot
plot(test$a,test$b, col = rainbow(length(test$s))[rank(test$s)])
with the colour assigned according to test$s. The problem is that test$s is equal for many values of the data frame test so if than I write
legend('topright',legend=test.sub$s,col=rainbow(length(test.sub$s))
[rank(test.sub$s)])
I get in the legend all duplicates of test$s but the colours are correct. Since I don't want the duplicates I wrote
legend('topright',legend=unique(test.sub$s),col=rainbow(length(test.sub$s))
[rank(test.sub$s])
but then all the colours are messed up!
Thanks in advance
You're problem is that unique(test.sub$s) is not the same length as rainbow(length(test.sub$s))[rank(test.sub$s]. My solution would be to do:
col=rainbow(length(test.sub$s))[rank(test.sub$s)[!duplicated(test.sub$s)]]

R - logistic curve plot with aggregate points

Let's say I have the following dataset
bodysize=rnorm(20,30,2)
bodysize=sort(bodysize)
survive=c(0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1)
dat=as.data.frame(cbind(bodysize,survive))
I'm aware that the glm plot function has several nice plots to show you the fit,
but I'd nevertheless like to create an initial plot with:
1)raw data points
2)the loigistic curve and both
3)Predicted points
4)and aggregate points for a number of predictor levels
library(Hmisc)
plot(bodysize,survive,xlab="Body size",ylab="Probability of survival")
g=glm(survive~bodysize,family=binomial,dat)
curve(predict(g,data.frame(bodysize=x),type="resp"),add=TRUE)
points(bodysize,fitted(g),pch=20)
All fine up to here.
Now I want to plot the real data survival rates for a given levels of x1
dat$bd<-cut2(dat$bodysize,g=5,levels.mean=T)
AggBd<-aggregate(dat$survive,by=list(dat$bd),data=dat,FUN=mean)
plot(AggBd,add=TRUE)
#Doesn't work
I've tried to match AggBd to the dataset used for the model and all sort of other things but I simply can't plot the two together. Is there a way around this?
I basically want to overimpose the last plot along the same axes.
Besides this specific task I often wonder how to overimpose different plots that plot different variables but have similar scale/range on two-dimensional plots. I would really appreciate your help.
The first column of AggBd is a factor, you need to convert the levels to numeric before you can add the points to the plot.
AggBd$size <- as.numeric (levels (AggBd$Group.1))[AggBd$Group.1]
to add the points to the exisiting plot, use points
points (AggBd$size, AggBd$x, pch = 3)
You are best specifying your y-axis. Also maybe using par(new=TRUE)
plot(bodysize,survive,xlab="Body size",ylab="Probability of survival")
g=glm(survive~bodysize,family=binomial,dat)
curve(predict(g,data.frame(bodysize=x),type="resp"),add=TRUE)
points(bodysize,fitted(g),pch=20)
#then
par(new=TRUE)
#
plot(AggBd$Group.1,AggBd$x,pch=30)
obviously remove or change the axis ticks to prevent overlap e.g.
plot(AggBd$Group.1,AggBd$x,pch=30,xaxt="n",yaxt="n",xlab="",ylab="")
giving:

Resources