The short version: How would I make this contrived code plot a proper greek beta character AND the rest of the label string, with the space and the less than character and the numbers formatted as typed?
library("ggplot2")
df<-data.frame(a=1:15,b=6:20)
ggplot(data=df, aes(x=a,y=b)) + geom_point() +
geom_text(x=5,y=4,label="beta=1.00 p<0.0001", parse=TRUE)
If I leave out the ", parse=TRUE" argument, the string works great, but won't give me a real greek beta. If I leave it in, all hell breaks loose.
The long version (and why I can't find a duplicate): I finally discovered in How to use Greek symbols in ggplot2? that methods for placing greek characters on a ggplot depends upon where you're putting them. Since I am using geom_text to force strings of text onto the body of the plot, my attempts to use expression(beta) have failed and I recently started using the parse argument mentioned above. I came across How can I add alpha-numeric AND greek characters to geom_text() in ggplot?, which I thought was my answer, but fixing the "space" resulted in extra parentheses popping in, "=" replaced by a comma, and loss of formatting for all of my previously formatted text numerics.
A link to a guide for using the parse argument, and how to make sense of what seems to me to be completely unintuitive would be very helpful to me, and probably future R users ending up here. Many searches have been fruitless.
This should do it:
g <- ggplot(data=df, aes(x=a,y=b)) + geom_point()
g + annotate("text", x=5,y=4,parse=TRUE, label="beta==1.00 * ' p<0.0001'")
It's a matter of splitting the label with single quotes to the side of the whitespace and connecting two label bits with *. You also need the == for equals to.
Related
I often use R to run batch jobs that contain PDF outputs with scatter plots produced using a combination of plot() and points(), among other graphical functions. (I don't use ggplot2 much and would like to avoid using it for this question.)
When using the text() function in a plot in order to add text near a plotted symbol, I like to use the pos = 4 option to right-justify text next to say a symbol like pch = 23 (filled diamond). But I've noticed that sometimes the text will get wrapped as part of multiple lines, and other times it stays on a single line. Unfortunately when the lines are wrapped it causes text overlap problems. Why does text() sometimes wrap text on multiple lines?
Is there a way to force R to keep the text added to a plot from text() on a single line?
Here is my code:
text(x=data_frame_w_data_to_plot$x_axis_value,
y=data_frame_w_data_to_plot$y_axis_value, labels=data_frame_w_data_to_plot$text_to_plot, col="black", cex=1, pos=4)
Answering my own question here. Found the text string of "\n" just happened to be in one of the cells, and simply used gsub() to slightly modify this cell. This solved the problem. Learn from my mistake. :)
data_frame_w_data_to_plot$text_to_plot <- gsub("\n", " ", data_frame_w_data_to_plot$text_to_plot)
I have trouble making my plots looking similar because my labels (both xlab and ylab) move if I use italic. Consider the following short example:
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab=expression(italic(p)~"-value"))
The problem is that "p-value" is slightly nearest from the axis than "p-value". I believe it's because the p tail defines the writing line which is considered different without italic. How can I fix that easily?
I frequently use both strings and expressions for my plots and that would be complicated to use mtext (with the line argument) to manage each label for each plot.
I found a workaround playing with unicode characters.
p is \U1D631 and P is \U1D617
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab="\U1D631-value")
This solved my problem.
I am plotting a graph in R but the italics function and it is being pretty frustrating under the main title:
Mapped territories of different C. austriacus individuals at the Far Gardens coral reef site
Any help would be appreciated.
You didn't give any information about your data but if the problem is italics in the title, maybe this code could help:
plot(rnorm(100), main = substitute(paste(italic('p value'), " = 0.01")))
See also this question.
Personally I think using paste to construct plotmath expression is "ugly"; This is an alternative the more clearly demonstrates "clean" use of expression:
plot(rnorm(100),main=expression( italic(p~value) == 0.01 ))
The other reason to use expression is that it will be accepted by Lattice functions whereas the substitute approach will not:
xyplot(1~1,main=substitute( paste(italic('p value'), " = 0.01" )))
#Error in paste(italic("p value"), " = 0.01") :
# could not find function "italic"
It would succeed if expression() were used inside the substitute call but it's excess baggage in that instance. I complained to Deepayan Sarkar once and his response was that substitute returns an unevaluated "call" rather than a true 'expression'.
I think other users have answered this, but I found it not so cut and dry, and built off of their input because it gets tricky for long titles like the one in your example.
Here is the cleanest line of code I could conjure for combined italic/normal texts, using a generic plot... let me know how it works for your data (or anyone who reads this and finds it doesn't work with certain graphs, inbox me, I enjoy learning and would rather share than just store it in my noggin)
plot(1:10, main = expression('Mapped territories of different '*italic(C.~austriacus)*' individuals at the Far Gardens coral reef site'))
Now, to line break with expression or like terms that allow italicized or superscript/subscript text (other than simple labels), you can use the atop function to evenly break apart super long labels. One may prefer to use the preview app for final editing and labels, but if you want to stick to R for everything, you can use the following example:
plot(1:10, main = expression(atop('Mapped territories of different '*italic(C.~austriacus), ' individuals at the Far Gardens coral reef site')))
which gives:
plot with long mixed title
Thanks to #42-
I don't know if you have seen some unwanted bold-face font like picture below:
As you see the third line is bold-faced, while the others are not. This happens to me when I try to use ggplot() with lapply() or specially mclapply(), to make the same chart template based on different data, and put all the results as different charts in a single PDF file.
One solution is to avoid using lapply(x, f) when f() is a function that returns a ggplot() plot, but I have to do so for combining charts (i.e. as input for grid.arrange()) in some situation.
Sorry not able to provide you reproducible example, I tried really hard but was not successful because the size of code and data is too big with several nested functions and when I reduced complexity to make a reproducible example, the problem did not happen.
I asked the question because I guessed maybe someone has faced the same experience and know how to solve it.
My intuition is that it's not actually being printed in bold, but rather double-printed for some reason, which then looks bold. This would explain why it doesn't come up with a simpler example. Especially given your mention of nested functions and probably other complicated structures where it's easy to get an off-by-one or similar error, I would try doing something where you can see exactly what's being plotted -- perhaps by examining the length() of the return value from apply().
Changing the order of elements of the vector, so that the order of the elements in the key is different, may also help. If you consistently get the bold-face on the last element, that also tells you a little bit more about where something is going wrong.
As #Dinre also mentioned, it could also be related to your plotting device. You can try out changing your plotting device. I have my doubts about this though, seeing as it's not a consistent problem. You could also try changing the position of the key, which depending on your plotting device and settings, may move you in or out of a compression block, thus changing which artifacts crop up.
Reproducible example and a solution may be as follows:
library(ggplot2)
d <- data.frame(x=1:10, y=1:10)
ggplot(data = d, aes(x=x, y=y)) +
geom_point() +
geom_text(aes(3,7,label = 'some text 10 times')) +
geom_text(data = data.frame(x=1,y=1),
aes(7,3, label = 'some text one time'))
When we try to add a label by geom_text() manually inserting x and y do not shorten the data. Then same label happen to be printed as many times as the number of rows our data has. Data length may be forced to 1 by replacing data within geom_text().
Consider the following example:
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux (g ',CO[2]~m^{-2}~h^{-1},')')))
Obviously I want a full space between "g" and "CO", but for some reason I get a smaller (with some labels even zero) space in the graph label.
The problem is even more obvious, if I do it like this:
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux (g C',O[2]~m^{-2}~h^{-1},')')))
Am I doing something wrong? Is there a way to fix the spacing or even a better way to create labels with lots of sub/superscripts and greek letters?
In all likelihood you are getting a typographically correct "space", in the font your OS uses for non-serif display. You can change fonts or you can insert blank space that is sufficient to hold a particular character string with plotmath phantom():
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux',phantom(x),'(g ',CO[2]~m^{-2}~h^{-1},')')))
Or as #baptiste points out this can be done without plomath paste using ordinary plotmath separators because a tilde in a true R expression gets handled as a "space":
ylab=expression(flux*phantom(x)*(g~CO[2]~m^{-2}~h^{-1})))