When I make any plot in R it always results with a thicker axis line along the numbered portion of the surrounding box. It's a nuisance as I must always turn the axes off in plot(), and then redraw them with zero thickness. I've noticed other labmates' computers don't seem to have this problem which makes me wonder where the problem is coming from.
Plot 1 generates my unwanted thick axes lines, Plot 2 works as a solution:
x<-c(1:10)
y<-c(1:10)
# Plot 1
plot(x,y, main="Plot 1")
# Plot 2
plot(x, y, xaxt='n', yaxt='n', main="Plot 2")
axis(1, lwd=0, lwd.ticks=1)
axis(2, lwd=0, lwd.ticks=1)
I'm running RStudio 0.98.501 on Mac OSX 10.9.4
Any ideas on root of the error much appreciated. Thanks.
You could create a new plot function, something along the lines of this should work:
plot = function(x, y, ...){
plot(x, y, xaxt='n', yaxt='n')
axis(1, lwd=0, lwd.ticks=1)
axis(2, lwd=0, lwd.ticks=1)
}
Related
I have a barplot and would like to have the label showing different colours and the bars to be grey.
I tried:
col_lab <- c("red","green","grey","red","red","blue")
barplot(1:6,names.arg=1:6,main="barplot",las=1,horiz=TRUE,col="grey",xaxt="n",col.names=col_lab)
but I got an error.
Thank you for your help.
Use yaxt="n" and do an mtext. To get the right positions, use the coordinates barplot throws invisibly.
b <- barplot(1:6, names.arg=1:6, main="barplot", las=1, horiz=TRUE, col="grey",
xaxt="n", yaxt="n")
mtext(1:6, 2, .5, at=b, las=2, col=col_lab, font=2)
I have a following "beeswarm" (a single-dimensional scatterplot)
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE)
Here is the resulting plot:
How can I get rid of the axes and the box around the plot, so that I can reintroduce only the X axis and nothing else around it?
If you create an empty plot first
plot(rnorm(10), type="n", axes=FALSE, xlim=c(0, 200), ylim=c(0.4, 1.6),
xlab="", ylab="")
Then you can use the add argument to get what you want
beeswarm(breast$time_survival,horizontal=TRUE, add=TRUE)
You can use the "axes" argument (described in ?plot.default).
beeswarm(breast$time_survival, horizontal=TRUE, axes = FALSE)
Newbie Question!
I'm trying to plot a line graph, but the tick-labels on the x-axis have to be non-numeric. How can I do this? So far I have this:
plot(disparitybin$Variance[1,], type="l", xlim=c(0,5.0), ylim=c(0, 0.05), frame=FALSE, xlab="Time", xaxt='n', yaxt='n', ylab="Dispairty (Sum of Variance)", main="Dispairty Through Time", cex.lab=1.25, cex.main=1.75)
axis(1, pos=0, las=0)
axis(2, pos=0, las=2)
I'm trying to specify the names of each tick mark along the x-axis: Eocene, Oligocene, Miocene, Pliocene. How do I get the labels control for the axis() command to accept this info?
Thanks for your help!
Use axis as follows:
axis.labels <- c("Eocene", "Oligocene", "Miocene", "Pliocene")
axis(1, labels=axis.labels, pos=0, las=0)
This assumes that you are plotting exactly four points of data. Also, you were correct to make the call to axis after the call to plot.
I am trying to generate a bar plot with a categorical X axis and two different y axis. I am trying to use twoord.plot to generate the bar plot as follows:
x <- c("A","B","C","D","E")
ry <- c(0.1,0.2,0.3,0.4,0.5)
ly <- c(0.15,0.25,0.35,0.45,0.55)
library(plotrix)
twoord.plot(x,ry,x,ly,
xlab="xLabel",
ylab="yLabel",
rylab="ryLabel",
main="Main",
type=c("bar","l"),lcol=rainbow(length(x)),rcol=4)
However, I am getting an error "Error in plot.window(...) : invalid 'xlim' value".
Is there a way to work with categorical/character variables as x-axis? Also, is there a way to rotate the X-axis labels so that they show up at 45 degrees?
I have been able to get this code to work with the following changes:
xNumeric <- seq(1:length(x))
twoord.plot(xNumeric,ly,xNumeric,ry,
xlab="xLabel",
ylab="yLabel",
rylab="ryLabel",
main="Main",
type=c("bar","o"),lcol=rainbow(length(x)),rcol = 4,xticklab = x)
However, I still need to figure out how to rotate the X-axis labels as well as adding a legend to differentiate between which is the box plot and which is the line plot. Any help on this would be appreciated
Thank you.
This isn't in plotrix, but...
ry <- c(0.1,0.2,0.3,0.4,0.5)
ly <- c(15,35,65,75,80)
x <- 1:5
xlabs <- c("A","B","C","D","E")
barplot(ly, xaxt="n", yaxt="n", xlab="xLabel", ylab="lyLabel", ylim=c(0,100))
axis(2, seq(0,100,by=5), seq(0,100,by=5), las=2) # you can adjust positions of ly labels
par(new=TRUE)
plot(ry~x, xaxt="n", yaxt="n", xlab="", ylab="", ylim=c(0,1))
axis(1, 1:5, xlabs)
axis(4, 1:10/10, 1:10/10, las=2) # you can adjust positions of ry labels
mtext("ryLabel", 4, line=2)
And you would obviously need to edit a bit to get the colors, etc. that you seem to be going for.
I would like to make a plot with 4 axes in R so that it looks similar to this plot:
I've looked at the Quick-R website for advice and modified one of their examples (called A Silly Axis Example):
# specify the data
x <- c(1:5); y <- x/2;
w <- c(2:4)
z <- c(1:5)
# create extra margin room on the right for an axis
par(mar=c(5, 4, 4, 8) + 0.1)
# plot x vs. y
plot(x, y,type="b", pch=21, col="red",
yaxt="n", lty=3, xlab="", ylab="")
# add x vs. 1/x
lines(x, z, type="b", pch=22, col="blue", lty=2)
# draw an axis on the left
axis(2, at=x,labels=x, col.axis="red", las=2)
# draw an axis on the right, with smaller text and ticks
axis(4, at=w,labels=round(w,digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
# draw an axis on the top
axis(3, at=z,labels=round(z,digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
# add a title for the right axis
mtext("L", side=3, line=3, cex.lab=1,las=2, col="blue")
# add a title for the right axis
mtext("OSR", side=4, line=3, cex.lab=1,las=2, col="red")
# add a main title and bottom and left axis labels
title("", xlab="GSI", ylab="FSI")
This code produces the following plot:
I'm having difficulty figuring out how different axes can have different scales. For example, I want the top axis L, to go from 5 - 13, but if I set z <-c(5:13) it will not set the axis to these values. However, I can overwrite what the labels are:
axis(3, at=z,labels=round(c(9:13),digits=2), col.axis="blue",
las=2, cex.axis=0.7, tck=-.01)
but then if I want to plot a point with these four parameters, the point will not show up in the correct place. How should I do this?
One (perhaps cumbersome) option would be to write conversion functions that transform values between your two scales. Assuming you know the data ranges for both the top and bottom axes ahead of time, you could write a function like this:
convertScaleToBottom <- function(x,botRange,topRange){
temp <- (x - topRange[1]) / (topRange[2] - topRange[1])
return(botRange[1] + (temp * (botRange[2] - botRange[1])))
}
that takes a set of values, x, in the top axis scale and converts them to the bottom axis scale. Then you can plot the converted values and retain the originals as the labels:
z1 <- 5:13
z1Adj <- convertScaleToBottom(z1,range(x),range(z1))
# draw an axis on the top
axis(3, at=z1Adj,labels=round(z1,digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
This method is easily modified to reverse the order of the top axis:
convertScaleToBottomRev <- function(x,botRange,topRange){
temp <- (x - topRange[1]) / (topRange[2] - topRange[1])
return(botRange[2] - (temp * (botRange[2] - botRange[1])))
}