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.
Related
I'm trying to reproduce the plot of the image using this code in R:
N = 1:100
r = 1
K = 1
r1 = list(r*N*(1 - (N/K)))
plot(N, r1[[1]])
but negative values appear on the graph. What am I doing wrong or how can I graph the image?
Thanks in advance
You could use the curve function, which is designed for drawing function curves. In this way, you avoid the detour of generating values in advance.
For the basic curve you just need to code your varying variable N as x:
curve(expr=r*x*(1 - (x/K)), from=1, to=100)
To completely reproduce the plot, we open the R graphics toolbox a little further.
op <- par(mar=c(4, 8, 2, 5)) ## set margins
curve(r*x*(1 - (x/K)), 1, 100,
xlab="", ylab="", xaxt="n", yaxt="n",
axes=FALSE, xaxs="i", yaxs="i",
ylim=c(-8e3, 3e3), lwd=2)
axis(2, labels=FALSE, lwd.ticks=0)
abline(h=-5e3)
text(max(N), -5e3*1.05, "N", font=8, xpd=TRUE)
mtext("r", 2, .5, at=0, las=1, font=8)
mtext("Growth rate", 2, .5, at=2e3, las=1, font=6, cex=1.5)
## for the "K" tick and label in the plot, we need to solve the equation
## to get the intersect with our abitrary x axis at -5e3
f <- function(x, y) r*x*(1 - (x/K)) - y
x.val <- uniroot(f, y=-5e3, lower=0, upper=1000)$root
## and insert the solution as x.value
axis(1, x.val, labels=FALSE, pos=-5e3)
text(x.val, -5e3*1.1, "K", font=8, xpd=TRUE)
par(op) ## reset margins
Result
If you have a look at r1, you'll see that the data are plotted correctly. The values begin at zero and decrease.
If you simply wanted to shift the data for a quick visualization, you can add a scale factor:
#add a scale factor - all values positive
r2<-r1[[1]]+10000
plot(N, r2)
or
#add a scale factor - span y = 0
r3<-r1[[1]]+5000
plot(N, r3)
Add annotation to the plot:
abline(h=0, col="black") #add line at zero
text(65, -600, "K", cex=1.5, col="black") #add text
I would like to increase the font size of axis annotations in a hexbinplot.
library(hexbin)
df <- data.frame(x=rnorm(1000),y=rnorm(1000))
hb <- hexbin(x=df$x, df$y)
myPlot <- plot(hb, xlab="", ylab="", legend=FALSE)
I would like the -3, ..., 2 and the -2, ..., 3 on the axes to be larger.
This earlier thread already helped me with axis labels, but the suggestion about how to change the annotations ("use grid.ls()" - how?) is a little too cryptic for me. I am more fluent in base graphics than in lattice.
Try this.
library(grid)
myPlot <- plot(hb, xlab="", ylab="", legend=FALSE)
grid.ls()
# GRID.rect.250
# GRID.xaxis.251
# GRID.yaxis.252
# GRID.polygon.253
grid.edit("GRID.xaxis.251", gp=gpar(fontsize=20))
grid.edit("GRID.yaxis.252", gp=gpar(fontsize=20))
The grid.ls() function shows the parts of the graph. The axis labels are GRID.xaxis.251 and GRID.yaxis.252. The name labels should be the same but the numbers will be different so you will have to modify the grid.edit() lines to match the output from grid.ls().
I am finishing with my script, but I cannot set an angle for x labels. I would like to use it for my data in specific index position:
INPUT:
xlabel <- (0,100,200,250,336)
xlabel.popis <- ("TATA","MAMA","OND","KOKO","LOLO")
OUTPUT:
Will be plotted xlabel.popis on specific xlabel postion on x axis (x axis is index line (0..500)) and xlabel.popis will have vertical rotation.
I tried:
plot(read.table(files2[i],header=F,sep="\t")$V7,main=file_bez2[i], axes=FALSE)
xlabel <- (0,100,200,250,336)
xlabel.popis <- ("TATA","MAMA","OND","KOKO","LOLO")
axis(1, at=seq_along(xlabel),labels=as.character(xlabel.popis, las=2, cex.label=90))
or I tried no axis but mtext(as.character(xlabel.popis),side=1,line=1.1,at=xlabel,srt=90)
Nothing worked, could you help me, It will be better for me with axis definition.And par() definiton did not work too.
Thank you so much
I believe you need to add labels with the text function after a call to axis.
example:
xlabel <- c(0,100,200,250,336)
xlabel.popis <- c("TATA","MAMA","OND","KOKO","LOLO")
plot(range(xlabel), c(1,1), t="l", xaxt="n", xlab="")
axis(1, at=xlabel, labels=FALSE)
text(x=xlabel, y=par()$usr[3]-0.1*(par()$usr[4]-par()$usr[3]),
labels=xlabel.popis, srt=45, adj=1, xpd=TRUE)
If you just want 90° rotation, consider the las argument:
plot(range(xlabel), c(1,1), t="l", xaxt="n", xlab="")
axis(1, at=xlabel, labels=xlabel.popis, las=2)
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])))
}
I was just wondering if there is a way to get rid of axis values, either the x-axis or y-axis respectively, in an r-plot graph.
I know that axes = false will get rid of the entire axis, but I would only like to get rid of the numbering.
Remove numbering on x-axis or y-axis:
plot(1:10, xaxt='n')
plot(1:10, yaxt='n')
If you want to remove the labels as well:
plot(1:10, xaxt='n', ann=FALSE)
plot(1:10, yaxt='n', ann=FALSE)
Using base graphics, the standard way to do this is to use axes=FALSE, then create your own axes using Axis (or axis). For example,
x <- 1:20
y <- runif(20)
plot(x, y, axes=FALSE, frame.plot=TRUE)
Axis(side=1, labels=FALSE)
Axis(side=2, labels=FALSE)
The lattice equivalent is
library(lattice)
xyplot(y ~ x, scales=list(alternating=0))
#Richie Cotton has a pretty good answer above. I can only add that this page provides some examples. Try the following:
x <- 1:20
y <- runif(20)
plot(x,y,xaxt = "n")
axis(side = 1, at = x, labels = FALSE, tck = -0.01)
you can also put labels inside plot:
plot(spline(sub$day, sub$counts), type ='l', labels = FALSE)
you'll get a warning. i think this is because labels is actually a parameter that's being passed down to a subroutine that plot runs (axes?). the warning will pop up because it wasn't directly a parameter of the plot function.
Change the axis_colour to match the background and if you are modifying the background dynamically you will need to update the axis_colour simultaneously.
* The shared picture shows the graph/plot example using mock data ()
### Main Plotting Function ###
plotXY <- function(time, value){
### Plot Style Settings ###
### default bg is white, set it the same as the axis-colour
background <- "white"
### default col.axis is black, set it the same as the background to match
axis_colour <- "white"
plot_title <- "Graph it!"
xlabel <- "Time"
ylabel <- "Value"
label_colour <- "black"
label_scale <- 2
axis_scale <- 2
symbol_scale <- 2
title_scale <- 2
subtitle_scale <- 2
# point style 16 is a black dot
point <- 16
# p - points, l - line, b - both
plot_type <- "b"
plot(time, value, main=plot_title, cex=symbol_scale, cex.lab=label_scale, cex.axis=axis_scale, cex.main=title_scale, cex.sub=subtitle_scale, xlab=xlabel, ylab=ylabel, col.lab=label_colour, col.axis=axis_colour, bg=background, pch=point, type=plot_type)
}
plotXY(time, value)