I'm about to plot odds ratios with R/ggplot2 and I want to add two arrows underneath or next to the X-axis label. One pointing to the left, one pointing to the right, showing de-/increasing influence. I've tried lots of things, like geom_path, geom_line, without great success.
This is my code:
forest <- function(d, xlab="Odds Ratio", ylab="Influencing variables"){
require(ggplot2)
p <- ggplot(d, aes(x=ordered(x, levels=rev(x)), y=y, ymin=ylo, ymax=yhi)) +
geom_pointrange() +
geom_segment(aes(x = 0, xend = 0, y= 1, yend= 2)) +
coord_flip() +
geom_hline(aes(yintercept=1), lty=2) +
ylab(xlab) +
xlab(ylab) +
scale_y_log10()
return(p)
}
##Test Data
data <- data.frame( x = c("A","B","C","D","E","F","G","H","I"),
y = c(1.782,0.136,0.978,0.645,0.518,1.474,0.855,0.673,0.369))
data <- transform(data, ylo = (0.719,0.046,0.945,0.295,0.188,0.577,0.407,0.310,0.145),
yhi = c(4.420,0.398,1.012,1.411,1.424,3.768,1.798,1.460,0.940))
forest(data)
Adding a line like
geom_line(aes(x=1), arrow=arrow(length=unit(0.15,"cm")), colour="black", size=1)
brings some arrows but they collide with my original data.
Thanks in advance for your solution, help or hint!
Marc
R offers a wide variety of statistics-related libraries and provides a favorable environment for statistical computing and design. In addition, the R programming language gets used by many quantitative analysts as a programming tool since it's useful for data importing and cleaning.
If you're passionate about the statistical calculation and data visualization portions of data analysis, R could be a good fit for you. If, on the other hand, you're interested in becoming a data scientist and working with big data, artificial intelligence, and deep learning algorithms, Python would be the better fit.
R is the most popular language in the world of Data Science. It is heavily used in analyzing data that is both structured and unstructured. This has made R, the standard language for performing statistical operations. R allows various features that set it apart from other Data Science languages.
R is known for being hard to learn. This is in large part because R is so different from many programming languages. The syntax of R, unlike languages like Python, is very difficult to read. Basic operations like selecting, naming, and renaming variables are more confusing in R than they are in other languages.
This can easily be done using mathematical annotation in R expression(x <-> y)
. A table of available options can be found here. expression
also allows latex like math symbols.
p1 <- qplot(x = 1:10, y = 1:10, geom = "blank")
p1 <- p1 + xlab(expression(decreasing %<->% increasing))
p1 <- p1 + ylab(expression(down %->% up))
p1 <- p1 + ggtitle(expression("Darcy's law" %->% q == -k*frac(Delta*h,Delta*l)))
p1 <- p1 + theme_bw(base_size=14)
This is a pretty old post, but thought I would share another option for anyone who stumbles upon this.
I have a situation which really requires that I have arrows outside of the plotting region (and don't care to involve grid
). As shown below, using expressions and symbols might be a good option for some:
p1 <- qplot(x = 0:12, y = 0:12, geom = "blank")
p1 <- p1 + ylab(expression(symbol('\256')))
p1 <- p1 + xlab(expression(symbol('\256')))
p1 <- p1 + theme(axis.title.y = element_text(size = 30))
p1 <- p1 + theme(axis.title.x = element_text(size = 30))
p1
The theme modifications aren't absolutely necessary but you will probably want to increase the size of the arrows at least a bit.
By default, the symbol()
function takes Adobe Symbol codes which are outline in this document.
If you want to add some text to the labels (just adding to the expression would probably make it too big), the following should get you started:
grid.text(label="X", x = 0.645, y = 0.02)
grid.text(label="Y", x = 0.1, y = 0.5, rot=90)
grid.gedit("GRID.text", gp=gpar(fontsize=15))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With