I have a long title that incorporates italics. I tried using \n to move half of the title onto a new line with limited success. I have it mostly figured out, but now I can't center the second line.
title(main=expression(paste("Inoculated \n", italic("Petunia x hybrida\n"), "`Dreams Red` mortality\n as a function of irrigation treatment" )))
I counted three "\n"'s in that expression so I guess you want 4 lines of title. That's fairly easy with nested used of plotmath atop:
title(main=expression(atop( atop(Inoculated,
italic("Petunia x hybrida")),
atop("\'Dreams Red\'"~mortality,
'as a function of irrigation treatment') )))
I was also guessing you actually didn't want backticks and so escaped single-quotes.
The task of 'writing' to a graphics device with an expression containing a number of lines not a power of 2 is a bit more complex.
You can do it with atop, which is actually for formatting math, but is frequently used for this purpose. It puts its first argument on the top and its second on the bottom, so paste as necessary.
plot(x = rnorm(10))
title(main = expression(atop(paste('Inoculated ', italic("Petunia x hybrida"),
"`Dreams Red` mortality"),
"as a function of irrigation treatment")))
Related
This is almost what I want as a plot heading:
plot(1:10)
ylabs<-c("All","Native","Exotic")
i=1
mtext(bquote("("*.(letters[i])*")"~.(ylabs[i])~"("%~~%italic("H'")*")"),side=3)
But I don't want the space after "(" and before the approx. equal sign. Adding the * separator before the symbol gives an error
mtext(bquote("("*.(letters[i])*")"~.(ylabs[i])~"("*%~~%italic("H'")*")"),side=3)
Error: unexpected SPECIAL in
even though the * separator works in other parts of bquote. I can get the right spacing by including the approx. equal symbol directly
mtext(bquote("("*.(letters[i])*")"~.(ylabs[i])~"("*"≈"~italic("H'")*")"),side=3)
but I would like to know if there's a way to get * to work before the plotmath symbol?
I tried this with expression instead of bquote, but couldn't get it to combine the characters with the indexed objects.
The trick is to put the entire text into a subscript:
plot(1:10)
ylabs<-c("All","Native","Exotic")
i=1
b <- bquote(phantom(0)["("*.(letters[i])*")"~.(ylabs[i])~"(" %~~%italic("H'")*")"])
mtext(b, cex = 2, side=3)
I am using ggpairs and while plotting the matrix, I receive a matrix as follows
As you can see, some of the text length is large and hence the text is not seen completely. Is there anyway that I can wrap the text so that it is visible completely.
Code
ggpairs(df)
I want the text to wrap so that it can be seen something like this
You can use the labeller argument of ggpairs to pass a function to be applied to the facet strip text.
ggplot does have a nice ready function label_wrap_gen() that wrap the long labels.
By default ggpairs use the column names as labels, and those can't contain spaces. label_wrap_gen() need spaces to split the labels on multiple rows.
This is a solution:
library(ggplot2)
library(GGally)
df <- iris
colnames(df) <- make.names(c('Long colname',
'Quite long colname',
'Longer tha usual colname',
'I\'m not even sure this should be a colname',
'The ever longest colname that one should be allowed to use'))
ggpairs(df,
columnLabels = gsub('.', ' ', colnames(df), fixed = T),
labeller = label_wrap_gen(10))
I have some very long labels for points on a map (Avg 35 chars per label).
Is there a "word wrap" of sorts in ggmap annotate (or other labeling function) so the lines are not stretched across the map? I'd like to either limit by number of chars or force a linebreak at a space.
Thanks!
Was able to find a solution here by replacing spaces with the linebreak character "\n"
StackedList <- gsub(" ", "\n", UnStackedList)
I'm trying to annotate a plot in ggplot with relevant data from a regression model.
I've followed the suggestions in this SO post and tried to modify the function to have a couple additional items in a newline in the plot.
This is my attempt at a new function:
lm_eqn = function(m){
eq <- substitute(italic(y) == a %.% italic(x)^b*","~~italic(r)^2~"="~r2*","~~italic(n)~"="~nn*","~~italic(p-value)~"="~pv,
list(a = format(exp(coef(m)[1]), digits = 3),
b = format(coef(m)[2], digits = 3),
r2 = format(summary(m)$r.squared, digits = 3),
nn=format(summary(m)$df[2]),
pv=format(summary(m)$coefficients[,4][2])))
as.character(as.expression(eq));
}
It produces the expected output: all in one line. But I'd like to split the text in two lines, the second one starting with italic(n)=. But if I introduce a \n, it throws an error when it finds \n. If I introduce the \n inside the quotes: "\n" then it seems to be ignored and the text remains in one line. I haven't found any reference as to how to introduce a newline in such an expression. Your kind help will be much appreciated.
Thanks.
EDIT: following a coment by #Tim I present a rewriten code and adjusted question.
\n cannot be used in plotmath expressions. You could perhaps break the expression in two parts, and use annotate to add the expressions where you want them. Or, use atop. Check out this post ->
Line break in expression()?
Say I have the following function:
sqrt_x = function(x) {
sqrtx = x^0.5
return(list("sqrtx" = sqrt))
}
attr(sqrt_x, "comment") <- "This is a comment to be placed on two different lines"
if I type
comment(sqrt_x)
I get
[1] "This is a comment to be placed on two different lines"
what I want, however, is that the comment is returned on two different lines (it could also be more lines and different comment elements. Any ideas appreciated.
As Andrie stated: you need to insert newline characters.
If you don't want to have to manually specify where the newlines go, then you can use strwrap to create breaks at convenient points, so that your string doesn't exceed a specified width.
msg <- strwrap("This is a comment to be placed on two different lines", width = 20)
cat(msg, sep = "\n")
# This is a comment
# to be placed on two
# different lines
A complete solution could look something like:
#Add comment as normal
comment(sqrt_x) <- "This is a comment to be placed on two different lines"
#Display using this function
multiline_comment <- function(x, width = getOption("width") - 1L)
{
cat(strwrap(comment(x), width = width), sep = "\n")
}
multiline_comment(
sqrt_x,
20
)
You can use \n to insert a newline. The cat method shows this in the way you want:
attr(sqrt_x, "comment") <- "This is a comment to be placed on two\ndifferent lines"
cat(comment(sqrt_x))
This is a comment to be placed on two
different lines
This is a bit of a hack, and maybe not what you want, but if you provide a multi-element character vector, and the lines are long enough that R's default formatting decides they should be on multiple lines, you may get what you want:
comment(sqrt_x) <- c("This is a comment ",
"to be placed on two different lines")
comment(sqrt_x)
## [1] "This is a comment "
## [2] "to be placed on two different lines"
You could use format to pad automatically:
comment(sqrt_x) <- format(c("This is a comment",
"to be placed on two different lines"),
width=50)
(as shown elsewhere you could also use strwrap() to break up a single long string
into parts)
If you're absolutely desperate to have this and you don't like the extra spaces, you could mask the built-in comment function with something like #RichieCotton's multiline version:
comment <- function(x,width = getOption("width") - 1L) {
cat(strwrap(base::comment(x), width = width), sep = "\n")
}
but this is probably a bad idea.