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)
Related
I am trying to draw a barplot in R
I have 2 vectors
x <- c(1,2,3,4)
y <- c(200,400,4000,255)
A <- rbind(x,y) # to make it into a matrix
barplot(A, ylim= c(0,5000))
I want to put at the base of each plot 1,2,3,4 on the x axis.
How can I do that
Thanks
barplot(A, ylim= c(0,5000),names.arg=1:4)
This is how you do it.
My suggestion is that you should check the help manual/doc for each function carefully. R graphic functions usually have lots of arguments for various purposes.
Function "barplot" returns the x-axis value where each bar is centred. We can use these values as a reference to add legend on top of each bar, or any where else (but less straightforward).
To add on the top
x.axis <-barplot(A, ylim= c(0,5000),names.arg=1:4)
text(x.axis, y, adj = c(0.5, 0)) ## you have defined "y"
I'm building a plot in R and I have used the plot() function, with log="y" parameter.
Does that mean that ONLY the y-axis labels will be converted in log scale OR that also the y-coordinates of my data will be converted in log-scale?
Thank you
When using log = "y" it plots the log-transformed y-values with the labels on the original scale -- the opposite of what you seem to suggest.
Compare these three plots:
x <- rnorm(50)
y <- 2*exp(x) + rexp(50)
plot(x, y) # y-scale, y-scale-labels
plot(x, y, log = "y") # log-y-scale, y-scale-labels
plot(x, log(y)) # log-y-scale, log-y-scale labels
Notice that the last two plots only differs in the y-axis labels. Both are still correct as the axis titles are also different.
i have this data and plot
mydata <- data.frame(a=c(1:5),b=c(6:10),c=c(11:15),e=c(16:20))
plot <- stripchart(mydata, method="jitter", vertical=T,main='plot',pch=19)
I would like to subset the x axis into two labels named 'a+b' and 'c+d' labels
Thanks in advance
In your case you can simply use mtext on side 1:
mydata <- data.frame(a=c(1:5),b=c(6:10),c=c(11:15),d=c(16:20))
plot <- stripchart(mydata, method="jitter", vertical=T,main='plot',pch=19)
mtext(c('a+b','c+d'),side=1,line=3,at=c(1.5,3.5))
Argument line is to set up the vertical position and at the position on the x-axis.
Edit: To add a distance between the two groups, you can do like this (there may be a cleaner way to do that but that's the only one I can think of from the top of my head):
mydata <- data.frame(a=c(1:5),b=c(6:10),c=c(11:15),d=c(16:20))
plot <- stripchart(mydata, method="jitter", vertical=T, main='plot',pch=19,
at=c(1,2,4,5),xlim=c(0,6))
mtext(c('a+b','c+d'),1,line=3,at=c(1.5,4.5))
Argument at of stripchart is the one to fiddle with, but you then have to modify the plot limits (xlim) and the x-value at which you write the axis label (in mtext).
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'm using the rgl package in r to plot some data. As done here:
http://www.r-bloggers.com/creating-3d-geographical-plots-in-r-using-rgl/
For some reason the scale does not align with the graph.
I changed the scale of the X and Z axis to increase the relief, which I initially thought was causing the issue, but in the example below, even if I change 0.02 to 1 the issue occurs.
library(rgl)
rdata <- c(0.8926,0.8986,0.9478,0.9672,0.916,0.912,0.9324,0.9532,0.9488,0.9376,0.921,0.927,0.9728,0.956,0.9318,0.9202)
labs <-c(100,200,500,1000)
rmatrix <- matrix(rdata, nrow=4,ncol=4,)
dimnames(rmatrix) <- list(labs,labs)
y <- as.matrix(rmatrix)
x <- 0.02*(1:nrow(y))
z <- 0.02*(1:ncol(y))
rgl.surface(x, z, y, color="red", back="lines")
axis3d('x--', labels=row.names(rmatrix),color="black")
Why is this happening?
Thanks for your help!
Mat
Without supplying a value to the labels argument in axis3d, I get an axis with six tick marks. Since you supply a vector with only four values to the labels argument, it looks like axis3d recycles those values to cover all the tick marks.
Tell axis3d at what data values you'd like to place the tick marks by supplying a value to the at argument:
axis3d('x--', at = x, labels=row.names(rmatrix),color="black")
p.s. I had to add the following line before rgl.surface() to avoid getting a segfault
rgl.open()