I'm trying to get rid of the double labels in the x axis when using the plot function in R with my data.
Sample data can be created like this:
Data=read.table(textConnection("
Geslacht y x1 x2 x3 x4 x5 x6 x7 x8 x9
M 0.00 5 6 16 9 5 13 14 5 7
M 0.25 6 7 17 9 5 13 17 5 7
M 0.67 12 12 28 14 7 21 24 13 13
"),head=TRUE)
I've used the following code so far:
Data <- read.table(Data, header=TRUE, sep="\t", na.string="NA", strip.white=TRUE)
par(new=T)
plot(Data$y, Data$x1, xlab="Chronological Age", ylab="%DNA methylation", cex = 0.9, col="black", pch=20, xlim=c(0, 100), ylim=c(0, 100), font.lab=2)
par(new=T)
plot(Data$y, Data$x2, pch=20, axes=F, ylab="", cex= 0.9, col="blue", xlim=c(0, 100), ylim=c(0, 100))
par(new=T)
plot(Data$y, Data$x3, pch=20, axes=F, ylab="", cex= 0.9, col="red", xlim=c(0, 100), ylim=c(0, 100))
par(new=T)
plot(Data$y, Data$x4, pch=20, axes=F, ylab="", cex= 0.9, col="green", xlim=c(0, 100), ylim=c(0, 100))
par(new=T)
plot(Data$y, Data$x5, pch=20, axes=F, ylab="", cex= 0.9, col="coral4", xlim=c(0, 100), ylim=c(0, 100))
par(new=T)
plot(Data$y, Data$x6, pch=20, axes=F, ylab="", cex= 0.9, col="darkgrey", xlim=c(0, 100), ylim=c(0, 100))
par(new=T)
plot(Data$y, Data$x7, pch=20, axes=F, ylab="", cex= 0.9, col="darkorchid1", xlim=c(0, 100), ylim=c(0, 100))
par(new=T)
plot(Data$y, Data$x8, pch=20, axes=F, ylab="", cex= 0.9, col="darkgoldenrod1", xlim=c(0, 100), ylim=c(0, 100))
par(new=T)
plot(Data$y, Data$x9, pch=20, axes=F, ylab="", cex= 0.9, col="forestgreen", xlim=c(0, 100), ylim=c(0, 100))
legend("bottomright", title="CpG regions", cex=.8, c("CpG1 ELOVL2","CpG2 ELOVL2","CpG3 ELOVL2", "CpG4 ELOVL2", "CpG5 ELOVL2", "CpG6 ELOVL2", "CpG7 ELOVL2", "CpG8 ELOVL2", "CpG9 ELOVL2"),
pch=c(20,20,20,20,20,20,20,20,20),col=c("black","blue", "red", "green", "coral4", "darkgrey","darkorchid1","darkgoldenrod1","forestgreen",horiz=FALSE))
I can't post the picture due to being a newbie...
Does anyone know why I get the double label in the x axis? The second label is "Data$y" even though I specify the xlabel in code line 3.
What are you trying to do here? Looks like you are trying to plot several sets of points in different colours on the same axes in one graph.
There are better ways...
Use
plot
to set the axis limits with xlim and ylim. Plot nothing, but name your axes:plot(NA,xlab="my x",ylab="my y",xlim=c(1,23),ylim=c(99,1000))
Then use
points(x,y,col=....)
to add points to the current plot. Don't usepar(new=T)
!Or
matplot
Or
plot(d$x, d$y, col=d$col)
will do most of the job in one line.Or
ggplot2
and make a pretty plot.Here's how to do that rearrangement using
reshape2
package:md
is now a "long" data frame. Now convert thex1
variable to numeric:define your colour palette:
set the point colour by explicit lookup:
and plot. Tweak axis labels, limits, title, etc to taste, simmer for 20 minutes and stir in a
legend
: