I'm trying to label the points of an ECDF plot with another column from my data field.
Currently I'm using this:
untouched = read.table("results-untouched.tsv", sep="\t")
plot.ecdf(untouched$V4, xlim=c(0.75,1.25), ylim=c(0,1), col='green', verticals=T)
which plots allright, but I'm then unable to add the labels to the points. The labels would be in untouched$V1.
Any idea on how to do this?
To add labels, you can use the text function. For example, we generate some data
x = sort(rnorm(10))
then create the ecdf object (plot.ecdf does this automatically),
m = ecdf(x)
and plot m
plot(m)
To add labels, we use the text function. The x coordinates are the data, the y coordinates are the output from the ecdf function (with an additional 0.03 to avoid over-plotting):
text(x, m(x) + 0.03, LETTERS[1:10])
Related
Let say i have a data like this
M<- matrix(rnorm(20),20,5)
x <- as.matrix(sort(runif(20, 5.0, 7.5)))
The M has 5 columns with the same values which I want to plot it but I don't want to plot them on each other. I want to show them with a space. What I do is like below
plot(x, M[,1], ylim=range(M), ann=FALSE, axes=T,type="l")
Colm <- 2:ncol(M)
lapply(seq_along(Colm),function(i){
lines(x, M[,i], col=Colm[i])
})
Is there any way to make a distance between each line in plot ?
You can add a small change to each y value to shift each line slightly.
set.seed(595)
M <- matrix(rnorm(20),20,5)
x <- as.matrix(sort(runif(20, 5.0, 7.5)))
plot(NA, ylim=range(M), xlim=range(x), ann=FALSE, axes=T, type="l")
# Amount by which to shift each y value
eps = seq(-0.1, 0.1, length.out=ncol(M))
lapply(1:ncol(M), function(i){
lines(x, M[,i] + eps[i], col=i)
})
UPDATE: In answer to your comment, I think the following is probably what's happening: In your sample code, x is a matrix, which behaves essentially the same as a vector when the matrix has only one column. Thus, x will return a data vector, so you can just use the object x directly in the lines function. However, if you're importing x as a data frame (for example, using x=read.table("x.txt", header=TRUE), then you need to use lines(x[,1], M[,i] + eps[i], col=i) in your code in order to get the vector of data in the first column of the data frame x.
If you use ggplot you can do this easily using the alpha command as indicated in this post:
Overlapping Lines in ggplot2
and you could also jitter the lines using code from this post..
How to jitter lines in ggplot2
I am trying to figure out how to add both points and lines (in color) to a legend in R.
What I want to plot is something like this:
x = 1:10
y = sin(x)
z = cos(x)
plot(x,y,type='l',lty=2,col="blue")
points(x,z,pch=19,col="red")
#I know this legend command doesn't work, but I just want to explain what I want
legend("topleft",c("x","y"),pch=19,lty=2,col=c("red","blue")
So as you can see, I would like to be able to have both points and lines (with the correct type and coloring) in the legend.
You NA values as other argument for pch= and lty=.
legend("topleft",c("x","y"),pch=c(19,NA),lty=c(NA,2),col=c("red","blue"))
I am trying to plot several points with error bars, with two y axes.
However at every call of the plotCI or errbar functions, a new plot is initialized - with or without par(new=TRUE) calls -.
require(plotrix)
x <- 1:10
y1 <- x + rnorm(10)
y2<-x+rnorm(10)
delta <- runif(10)
plotCI(x,y=y1,uiw=delta,xaxt="n",gap=0)
axis(side=1,at=c(1:10),labels=rep("a",10),cex=0.7)
par(new=TRUE)
axis(4)
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0)
I have also tried the twoord.plot function from plotrix, but I don't think it's possible to add the error bars.
With ggplot2 I have only managed to plot in two different panels with the same Y axis.
Is there a way to do this?
Use add=TRUE,
If FALSE (default), create a new plot; if TRUE, add error bars to an
existing plot.
For example the last line becomes:
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0,add=TRUE)
PS: hard to do this with ggplot2. take a look at this hadley code
EDIT
The user coordinate system is now redefined by specifying a new user setting. Here I do it manually.
plotCI(x,y=y1,uiw=delta,xaxt="n",gap=0)
axis(side=1,at=c(1:10),labels=rep("a",10),cex=0.7)
usr <- par("usr")
par(usr=c(usr[1:2], -1, 20))
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0,add=TRUE,col='red')
axis(4,col.ticks ='red')
I need to add Y value labels next to my plot and I tried text function but no luck here - it only prints my X values.
Any sugestions? Isn't such functionality achievable as an argument to plot function?
My second smaller question is how to print all values on X axis not only every fifth as I have now
Thanks In advance!
Try this:
set.seed(1)
y<-rnorm(10)
set.seed(2)
x<-rnorm(10)
plot(x,y)
text(x, y, labels=round(y,2),pos=3)
For your second question, you can use ?axis function:
plot(x,y,xaxt="n") #don't draw x-axis
#draw x-axis by spesifying the ticks using argument at for function axis
axis(side=1,at=x,labels=round(x,2)) #this makes ticks only for values in x
text(x, y, labels=round(y,2),pos=4)
This is probably a simple question, but I´m not able to find the solution for this.
I have the following plot (I´m using plot CI since I´m not able to fill the points with plot()).
leg<-c("1","2","3","4","5","6","7","8")
Col.rar1<-c(rgb(1,0,0,0.7), rgb(0,0,1,0.7), rgb(0,1,1,0.7),rgb(0.6,0,0.8,0.7),rgb(1,0.8,0,0.7),rgb(0.4,0.5,0.6,0.7),rgb(0.2,0.3,0.2,0.7),rgb(1,0.3,0,0.7))
library(plotrix)
plotCI(test$size,test$Mean,
pch=c(21), pt.bg=Col.rar1,xlab="",ylab="", ui=test$Mean,li= test$Mean)
legend(4200,400,legend=leg,pch=c(21),pt.bg=Col.rar1, bty="n", cex=1)
I want to creat the same effect but with lines, instead of points (continue line)
Any suggestion?
You have 2 solutions :
Use The lines() function draws lines between (x, y) locations.
Use plot with type = "l" like line
hard to show it without a reproducible example , but you can do for example:
Col.rar1<-c(rgb(1,0,0,0.7), rgb(0,0,1,0.7), rgb(0,1,1,0.7),rgb(0.6,0,0.8,0.7),rgb(1,0.8,0,0.7),rgb(0.4,0.5,0.6,0.7),rgb(0.2,0.3,0.2,0.7),rgb(1,0.3,0,0.7))
x <- seq(0, 5000, length.out=10)
y <- matrix(sort(rnorm(10*length(Col.rar1))), ncol=length(Col.rar1))
plot(x, y[,1], ylim=range(y), ann=FALSE, axes=T,type="l", col=Col.rar1[1])
lapply(seq_along(Col.rar1),function(i){
lines(x, y[,i], col=Col.rar1[i])
points(x, y[,i]) # this is optional
})
When it comes to generating plots where you want lines connected according to some grouping variable, you want to get away from base-R plots and check out lattice and ggplot2. Base-R plots don't have a simple concept of 'groups' in an xy plot.
A simple lattice example:
library( lattice )
dat <- data.frame( x=rep(1:5, times=4), y=rnorm(20), gp=rep(1:4,each=5) )
xyplot( y ~ x, dat, group=gp, type='b' )
You should be able to use something like this if you have a variable in test similar to the color vector you define.