I am working on a forest plot in R using the metafor package and am trying to shift the whole x-axis (alim) to the right to accommodate ilab columns.
Am still not allowed to post images so my current plot now appears as something like this where the text and x-axis overlaps:
|ilab text| |mean [ci.lb, ci.ub]|
|---measure values + ci---|
And I want something like this
|ilab text| |mean [ci.lb, ci.ub]|
|---measure values + ci---|
Although the forestplot package seemed to allow this with its graph.pos function, I couldn't seem to find a similar function in metafor.
I have two questions:
1) Is the x-axis position set on default in metafor?
2) Can this default be overwritten, and if so, how?
Thanks!
Wen
Found the answer: the key is to adjust the xlim, alim and ilab.xpos parameters in relation to 0 (the start of the x-axis) as a reference point.
For example, if this code gives you an overlap,
forest(x, ci.lb = lower, ci.ub = upper,
xlim = c(-350, 170), xlab = "Proportion (%)", at = c(0, 20, 40, 60, 80, 100),
alim = c(0, 100),
ilab = cbind(period, population), ilab.xpos = c(-275, -175), ilab.pos = c(4, 4), cex = 0.75)
You can adjust ilab text further to the left of the x axis by adjusting ilab.xpos() values further away from 0 (e.g. from -175 in the above code to -200). This has to be within the limits of your xlim.
Related
In creating a forest plot using metafor in R, I am having trouble making some aesthetics changes to the plot as I have limited knowledge of this package. Thanks!
I'd like to remove the overall estimate at the bottom of the plot, boxed in red. This is the code I'm using and res is a rma.uni object.
forest(res, at=c(-0.5, -1, 0, 0.5, 1), xlim=c(-16,6), atransf = mytransf,
ilab=cbind(Z0, Z1, ZD0, ZD1), ilab.xpos=c(-9.5,-8,-6,-4.5),
cex=.75,
header="Subgroups",
mlab="",
xlab = 'ORR Relative Difference',
overall=FALSE, overall.hetstat = FALSE)
I would like to make the scale at the bottom wider and still spanning from -100 to 100. Right now it looks very narrow.
You need to set the ylim manually and start by 0
forest(res, at=c(-0.5, -1, 0, 0.5, 1), xlim=c(-16,6), atransf = mytransf,
ilab=cbind(Z0, Z1, ZD0, ZD1), ilab.xpos=c(-9.5,-8,-6,-4.5),
cex=.75, ylim=c(0,22),
header="Subgroups",
mlab="",
xlab = 'ORR Relative Difference',
overall=FALSE, overall.hetstat = FALSE)
to get a wider scale you have to experiment with the xlim
I want to change the line width for different groups plotted in the same figure using ggsurvplot. Something like this:
plot = ggsurvplot(curve_fitted_open,
fun = 'event',
conf.int = FALSE,
xlim = c(0, 365),
ylim = c(0, 1),
linetype = c('solid', 'dashed'),
size=c(1, 0.5))
I.e. I would like to set the line width (see the last line in the example) in a similar ways as the line type but this does not seems to work. Is there another way to do this?
How is it possible that I can change a normal histogram in a way that the x-axis indicates not absolute numbers but the relative numbers, the density?
This is what my histogram looks like now:
hist(df$rent, xlim = c(0, 36), ylim = c(0, 300), breaks = 30)
To add the argument
freq = FALSE
is the solution
Using this example:
x<-mtcars;
barplot(x$mpg);
you get a graph that is a lot of barplots from (0 - 30).
My question is how can you adjust it so that the y axis is (10-30) with a split at the bottom indicating that there was data below the cut off?
Specifically, I want to do this in base R program using only the barplot function and not functions from plotrix (unlike the suggests already provided). Is this possible?
This is not recommended. It is generally considered bad practice to chop off the bottoms of bars. However, if you look at ?barplot, it has a ylim argument which can be combined with xpd = FALSE (which turns on "clipping") to chop off the bottom of the bars.
barplot(mtcars$mpg, ylim = c(10, 30), xpd = FALSE)
Also note that you should be careful here. I followed your question and used 0 and 30 as the y-bounds, but the maximum mpg is 33.9, so I also clipped the top of the 4 bars that have values > 30.
The only way I know of to make a "split" in an axis is using plotrix. So, based on
Specifically, I want to do this in base R program using only the barplot function and not functions from plotrix (unlike the suggests already provided). Is this possible?
the answer is "no, this is not possible" in the sense that I think you mean. plotrix certainly does it, and it uses base R functions, so you could do it however they do it, but then you might as well use plotrix.
You can plot on top of your barplot, perhaps a horizontal dashed line (like below) could help indicate that you're breaking the commonly accepted rules of what barplots should be:
abline(h = 10.2, col = "white", lwd = 2, lty = 2)
The resulting image is below:
Edit: You could use segments to spoof an axis break, something like this:
barplot(mtcars$mpg, ylim = c(10, 30), xpd = FALSE)
xbase = -1.5
xoff = 0.5
ybase = c(10.3, 10.7)
yoff = 0
segments(x0 = xbase - xoff, x1 = xbase + xoff,
y0 = ybase-yoff, y1 = ybase + yoff, xpd = T, lwd = 2)
abline(h = mean(ybase), lwd = 2, lty = 2, col = "white")
As-is, this is pretty fragile, the xbase was adjusted by hand as it will depend on the range of your data. You could switch the barplot to xaxs = "i" and set xbase = 0 for more predictability, but why not just use plotrix which has already done all this work for you?!
ggplot In comments you said you don't like the look of ggplot. This is easily customized, e.g.:
library(ggplot2)
ggplot(x, aes(y = mpg, x = id)) +
geom_bar(stat = "identity", color = "black", fill = "gray80", width = 0.8) +
theme_classic()
The data for some of these types graphs that I'm graphing in R,
http://graphpad.com/faq/images/1352-1(1).gif
has outliers that are way out of range and I can't just exclude them. I attempted to use the axis.break() function from plotrix but the function doesn't rescale the y axis. It just places a break mark on the axis. The purpose of doing this is to be able to show the medians for both groups, as well as the data points, and the outliers all in one plot frame. Essentially, the data points that are far apart from the majority is taking up a chunk of space and the majority of points are being squished, not displaying much differences. Here is the code:
https://gist.github.com/9bfb05dcecac3ecb7491
Any suggestions would be helpful.
Thanks
Unfortunately the code you link to isn't self-contained, but possibly the code you have for gap.plot() there doesn't work as you expect because you are setting ylim to cover the full data range rather than the plotted sections only. Consider the following plot:
As you can see, the y axis has tickmarks for every 50 pg/ml, but there is a gap between 175 and 425. So the data range (to the nearest 50) is c(0, 500) but the range of the y axis is c(0, 250) - it's just that the tickmarks for 200 and 250 are being treated as those for 450 and 500.
This plot was produced using the following modified version of your code:
## made up data
GRO.Controls <- c(25, 40:50, 60, 150)
GRO.Breast <- c(70, 80:90, 110, 500)
##Scatter plot for both groups
library(plotrix)
gap.plot(jitter(rep(0,length(GRO.Controls)),amount = 0.2), GRO.Controls,
gap = c(175,425), xtics = -2, # no xtics visible
ytics = seq(0, 500, by = 50),
xlim = c(-0.5, 1.5), ylim = c(0, 250),
xlab = "", ylab = "Concentrations (pg/ml)", main = "GRO(P=0.0010)")
gap.plot(jitter(rep(1,length(GRO.Breast)),amount = 0.2), GRO.Breast,
gap = c(175, 425), col = "blue", add = TRUE)
##Adds x- variable (groups) labels
mtext("Controls", side = 1, at= 0.0)
mtext("Breast Cancer", side = 1, at= 1.0)
##Adds median lines for each group
segments(-0.25, median(GRO.Controls), 0.25, median(GRO.Controls), lwd = 2.0)
segments(0.75, median(GRO.Breast), 1.25, median(GRO.Breast), lwd = 2.0,
col = "blue")
You could be using gap.plot() which is easily found by following the link on the axis.break help page. There is a worked example there.