can't delete y axis label in plot.gbm - r

Run-on question following this problem setting axis widths in gbm.plot; I'm now using plot.gbm directly and don't seem to be able to remove the y axis label, which seems to be set within the plot.gbm function code.
png(filename="name.png",width=4*480, height=4*480, units="px", pointsize=80, bg="white", res=NA, family="", type="cairo-png")
par(mar=c(2.6,2,0.4,0.5), fig=c(0,1,0.1,1), las=1, lwd=8, bty="n", mgp=c(1.6,0.5,0))
plot.gbm(my_gbm_model,1,return.grid=FALSE, write.title=F,lwd=8, ylab=F, axes=F, ylabel=FALSE, ylabel="")
axis(1, lwd.ticks=8, lwd=8, labels=FALSE)
axis(2, lwd.ticks=8, lwd=8, labels=NA, ylab=FALSE,ylabel=FALSE)
dev.off()
Result:
The y axis label is still there despite all my atempts to remove it through par and plot and axis. I could try burrowing into the function and changing this (and similar) lines:
print(stripplot(X1 ~ temp | X2 * X3, data = X.new,
xlab = x$var.names[i.var[i[1]]],
ylab = paste("f(", paste(x$var.names[i.var[1:3]], collapse = ","), ")", sep = ""),
...))
...but I've been advised against such practices. Any thoughts why this might be working? Simply that the function overrides the setting?
Reproducibility:
#core data csv: https://drive.google.com/file/d/0B6LsdZetdypkWnBJVDJ5U3l4UFU
#(I've tried to make these data reproducible before and I can't work out how to do so)
library(dismo)
samples <- read.csv("data.csv", header = TRUE, row.names=NULL)
my_gbm_model <- gbm.step(data=samples, gbm.x=1:6, gbm.y=7, family = "bernoulli",
tree.complexity = 2, learning.rate = 0.01, bag.fraction = 0.5)

The problem is that plot.gbm just isn't a very R-like function. Since anyone can submit a package to CRAN it's not required that they follow traditional R patterns and that looks like what happened here. If you step though plot.gbm with your sample data, you see that ultimately the plotting is done with
plot(X$X1, X$y, type = "l", xlab = x$var.names[i.var], ylab = ylabel)
and the ylabel is set immediately before with no option to disable it. The authors simply provided no standard way to suppress the ylab for this particular plotting function.
In this case the easiest way might just be to reduce the left margin so the label prints off the plot. Seems like
par("mar"=c(5,2.2,4,2)+.1, fig=c(0,1,0.1,1), las=1, lwd=8, bty="n")
plot.gbm(my_gbm_model,1,return.grid=FALSE, write.title=F,lwd=8, ylab="", axes=F)
axis(1, lwd.ticks=8, lwd=8, labels=FALSE)
axis(2, lwd.ticks=8, lwd=8, labels=FALSE)

Related

Plotting line graph with factors in R

I am working on this graph in R:
However as you see I am getting these solid dashes instead of nice lines. Here is the code I used to make this graph:
par(mar = c(5,5,2,5))
with(bedtimes, plot(trap, funestus, type="l", col="red3",
ylab=expression(italic(p))),
ylim=c(0,3))
par(new = T)
with(bedtimes, plot(trap, bed, pch=16, axes=F, xlab=NA, ylab=NA, cex=1.2))
axis(side = 4)
mtext(side = 4, line = 3, 'Proportion in bed')
polygon(bedtimes$bed,col=transp("gray", 0.3), border = NA)
And here is the dput of the data I am using:
(removed)
I realise that this is occurring because my x axis is a factor and not numeric. However, trying to change this (e.g. using as.POSIXct(paste0("2016-07-12",bedtimes$trap)) is causing me all sorts of problems, as I had to ake sure R plotted these factors in the correct order originally by using bedtimes$trap <- factor(bedtimes$trap, levels = bedtimes$trap)
How can I produce this same graph but with lines instead of these dashes?
I eventually want a graph that looks similar to this, to give you an idea (though not exactly the same):
Thank you!
As I said in comments, you can try to use the levels of your factor variable:
par(mar = c(5,5,2,5))
with(bedtimes, plot(as.numeric(trap), funestus, type="l", col="red3",
ylab=expression(italic(p))),
ylim=c(0,3))
par(new = T)
with(bedtimes, plot(as.numeric(trap), bed, type="l", axes=F, xlab="", ylab=NA, cex=1.2))
axis(4)
axis(side = 1, at=1:length(levels(bedtimes$trap)), levels(bedtimes$trap))
mtext(side = 4, line = 3, 'Proportion in bed')
polygon(bedtimes$bed,col="gray", border = NA)

R symbol background for custom symbols

I have an R plot where I use the values as symbols. The points also have error bars:
The problem is, obviously, that the error bars (I use arrows for that) cross through the numbers and that just looks ugly and makes them hard to read.
This is my code, any ideas?
x = c(45.58333, 89.83333, 114.03333,138.65000,161.50000,185.15000,191.50000)
y_mean = c(3.350000,6.450000,7.200000,7.033333,8.400000,7.083333,6.750000)
y_sd = c(0.1802776,0.1732051,0.2500000,0.2020726,0.3500000,0.2020726,0.1000000)
values = data.frame(x, y_mean, y_sd)
plot(values$x, values$y_mean, type="n")
arrows(values$x, values$y_mean - values$y_sd,
values$x, values$y_mean + values$y_sd,
length=0.05, angle=90,
code=3, col="red")
lines(values$x, values$y_mean, type="b",
pch=" ",
col="red", bg="white")
text(values$x, values$y_mean, label=round(values$y_mean), col="red")
EDIT:
I executed the exact code shown above as asked:
I would play with the horizontal justification and add small points to keep track of the original position
points(values$x, values$y_mean, pch=19, col="red", cex=0.5)
text(values$x, values$y_mean, label=round(values$y_mean), col="red", adj = -0.2)
One idea is to white out the plot content where the text will be drawn, before drawing the text. This can be done with rect(). Although you risk whiting out the error bars entirely with this approach.
We can use strwidth() and strheight() to get the appropriate sizes for the whiteout rectangles.
x <- c(45.58333, 89.83333, 114.03333,138.65000,161.50000,185.15000,191.50000);
y_mean <- c(3.350000,6.450000,7.200000,7.033333,8.400000,7.083333,6.750000);
y_sd <- c(0.1802776,0.1732051,0.2500000,0.2020726,0.3500000,0.2020726,0.1000000);
xlim <- range(x);
ylim <- c(min(y_mean-y_sd),max(y_mean+y_sd));
plot(NA,xlim=xlim,ylim=ylim,xlab='x',ylab='y');
arrows(x,y_mean-y_sd,x,y_mean+y_sd,length=0.05,angle=90,code=3,col='red');
lines(x,y_mean,type='b',pch=' ',col='red',bg='white');
ls <- as.character(round(y_mean));
ex <- 0.4; ## whiteout expansion factor
lsw <- strwidth(ls); w <- lsw/2*(1+ex);
lsh <- strheight(ls); h <- lsh/2*(1+ex);
rect(x-w,y_mean-h,x+w,y_mean+h,col='white',border=NA);
text(x,y_mean,ls,col='red');
Just apply these changes:
plot(values$x, values$y_mean, type="n",
xlim = c(min(values$x), max(values$x) + 20),
ylim = c(min(values$y_mean)-1, max(values$y_mean)+1))
text(values$x, values$y_mean, label=round(values$y_mean), col="blue", pos = 3)

Barplot: Greek letters on y axis in R

This is a follow-up question to my other question on barplots:
Tornado plot in R
I realized the question about getting greek letters on the y-axis needed to be asked as an own question.
The question is:
I have the following barplot and need to change the y-axis to respective greek letters (and a V with a bar over).
I use the following code:
# Tornado plot
data <- matrix(c(-0.02,0.02,-0.01,0.01,-0.03,0.02,-0.01,0.04), ncol = 4)
rownames(data) <- c('+10%','-10%')
colnames(data) <- c('V_bar', 'alpha', 'rho','xi')
x <- seq(-0.04,0.04, length=10)
barplot(data[1,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab='',
beside=T, col=c('springgreen'))
barplot(data[2,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n',
yaxt='n', #To prevent double printing of y-labels.
beside=T, col=c('indianred2'), add = TRUE)
axis(1, at=pretty(x), lab=paste0(pretty(x) * 100," %"), las=TRUE)
To get the greek letters I have tried the following:
barplot(data[2,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n',
yaxt= c(expression(bar(V)), expression(alpha),expression(rho), expression(xi)),
beside=T, col=c('indianred2'), add = TRUE))
and
axis(2, at=c(1:4), lab = expression(xi ~ rho ~ alpha ~ bar(V)), las=T)
or
axis(2, at=pretty(x), lab = paste0(expression(xi ~ rho ~ alpha ~ bar(V)), las=T))
But no success. Anyone now the trick?
Note. I have seen this question: Adding greek character to axis title
But it focuses on the labels, not the y-axis "values".
Also, I have tried something like Putting greek letters in column names
with no success.
There's no need to call axis for labeling of the bars if you provide the argument names.arg to barplot:
barplot(data[1,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab='',
beside=T, col=c('springgreen'),
names.arg=c(expression(xi),expression(rho), expression(alpha), expression(bar(V))))
You just need to pass lab in axis as a vector of expressions.
axis(2, at=c(1:4), lab = c(expression(xi),
expression(rho), expression(alpha), expression(bar(V))), las=T)
Then you can play with the settings of the axis as needed.

How do I hide axes in a plot of means?

I want to omit the x-axis labels for my plot of means, but I just fail (I didn't use packages such as ggplot2):
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="")) # normal code
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",frame=F)) # which gives no response
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",axes=F))
This gives the following error:
Error in plot.default(c(1, n.levs), yrange, type = "n", xlab = xlab, ylab = ylab, :
formal argument "axes" matched by multiple actual arguments
The below code temporarily sets the color of axis tick labels (i.e., for both the x and y-axis) to "transparent" using par, thus preventing the automatically drawn tick labels from being visible. The y-axis tick labels can easily be inserted afterwards by reverting the par-related modifications.
## sample data
library(Rcmdr)
data(Moore)
## create plot with transparent (i.e., non-visible) tick labels
par(col.axis = "transparent")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
## add y-axis tick labels
par(col.axis = "black")
axis(side = 2, at = seq(0, 25, 5), tick = FALSE)
Remember that this is a rather undesired behavior of plotMeans since the help pages explicitly say that ... are arguments to be passed on to plot. Therefore, if your target is to entirely disable the drawing of the frame and axes when using plotMeans, e.g. similar to the visual output of plot(1:10, 1:10, axes = FALSE), you should probably file a bug report.
Update
In case I understood it wrongly and you actually wanted to remove both the tick labels and the ticks, you could simply use
par(xaxt = "n")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
See also ?par for further possible modifications of R base plots.

Plot line of a function in R

I have the following script:
FGM = function (n,r,z){
x = r*sqrt(n)/(2*z)
Px = 1-pnorm(x)
}
re = 10000
data = data.frame(abs(rnorm(re,0,1)), abs(rnorm(re,0,1)), abs(rnorm(re,0,1)))
colnames(data) = c("n","r","z")
data$Px = FGM(data$n,data$r,data$z)
data$x = data$r*sqrt(data$n)/(2*data$z)
par(mar=c(4.5,4.5,1,1))
plot(data$x,data$Px, xlim = c(0,3), pch = 19, cex = 0.1, xaxs="i", yaxs="i",
xlab = expression(paste("Standardized mutational size (",italic(x), ")")),
ylab = expression(paste("P"[a],"(",italic(x),")")))
Which is a recreation of the graph found here (box 2). You can see in this script that I do this by just plotting 10000 small black points with various values of n,z, and r. This seems like an ugly work around, I think I should just be able to give R my function
FGM = function (n,r,z){
x = r*sqrt(n)/(2*z)
Px = 1-pnorm(x)
}
and have it plot a line on a graph. However, a few hours of scouring the web has been unproductive, and I tried a few ways with abline and lines but nothing worked, is there a way of doing it with these functions or another function?
Tried this...
plot(data$x,data$Px, xlim = c(0,3), ylim = c(0,0.5), xaxs="i", yaxs="i",
xlab = expression(paste("Standardized mutational size (",italic(x), ")")),
ylab = expression(paste("P"[a],"(",italic(x),")")), type = "n")
curve(1-pnorm(r*sqrt(n)/(2*z)), add=T)
>Error in curve(1 - pnorm(r * sqrt(n)/(2 * z)), add = T) :
'expr' must be a function, or a call or an expression containing 'x'
>
#PaulRegular offered this solution but it still plots based on data, not the formula itself. I'm looking for a solution which can produce the curve properly without large values of "re" - using the following but with "re" set to 10 you can see what I mean...
data <- data[order(data$x),]
lines(data$x, data$Px, lwd=1)
You can pass a function of just one variable to plot. I guess that you are looking for:
plot(function(x) 1-pnorm(x),0,3)
Try sorting your data by x, then add the line:
data <- data[order(data$x),]
lines(data$x, data$Px, lwd=2)

Resources