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)
Related
I aim to compare the first half of texts with the entirety of the same texts. I have already done multiple analyses using the full texts, which I simply loaded into r with the help of the "readtext"-function (and some functions to attach variables like the session number). Likewise, I used the same function(s) to load in my texts again and now want to delete the second half of said texts.
My idea was to count the words in each string first, which I did using:
dataframe$numwords <- str_count (dataframe$text, "\\w+")
The next step would be, to use a for-loop to delete half the number of "numwords" from each row in the text column. However, I don't know how to do this. And is there a better way?
My dataframe looks like this (Note: The text in my data frames contains on average about 6000 words per row.)
text
session_no
patient_code
numwords
I do not feel well today.
05
2006X
6
My anxiety is getting worse. Why?
05
2007X
6
I can not do anything right, as always.
10
2006X
8
Edit: Is there a way to keep the punctuation? I am searching the text for specific ngrams. Doing this without punctuation may lead to false alarms, as the detection tool may find a match in text originally coming from two separate sentences.
With the following, we take the text column and split it into words using strsplit().
Then we use lapply() to calculate ho how many words would be half of each text.
Finally, we return only the first half of each text, but we lose all punctuation in the proccess.
lapply(strsplit(dataframe$text, split = "\\W+"), function(words) {
half <- round(length(words) / 2, 0)
paste(words[1:half], collapse = " ")
})
Edit
If we want to keep punctuation, then we need to make some adjustments.
Our regex nos keeps the delimiter, but has the secondary effect of keep some spaces as "words", so we have to remove them. We also use trim_ws() to remove trailing whitespace.
lapply(strsplit(dataframe$text, split = "(?<=\\W)", perl = TRUE), function(words) {
words <- words[words != " "]
half <- round(length(words) / 2, 0)
new_text <- paste(words[1:half], collapse = "")
trimws(new_text)
})
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)
Maybe i am not correct, but it appears to me that there is an undesired space in expressions beginning with a superscript:
df <- data.frame(treatment=as.factor(c("A", "B")), value=c(1,2))
labels <- c(expression(""^14~CH[4]),
expression(""^14~CH[4]~"+"~"SO"[4]^{2-''}))
library(ggplot2)
ggplot(df, aes(treatment, value)) +
geom_bar(stat="identity") +
scale_x_discrete(labels=labels)
I could go to Photoshop to reduce the space between the superscripted 14 and the "C", but maybe there is a way in plotmath? Notice, that this is not happening in the second expression with a superscript in the end.
In expressions, ~ gives you a space between terms. If you don't want a space between terms, you can use *. The end superscript is not preceded by a ~, so no space.
You can also remove most of the quote marks - these are unnecessary except when there are special characters or spaces.
So your expression can become
expression(''^14*CH[4]~+~SO[4]^'2-')
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")))
I would like to format the numbers on a continuous axis in a ggplot graph. I would like to have a French formatting for large numbers with a space every three digit (ie "20 000" instead of "20000"). I know it is possible to do it using the format() function (for instance format(20000, scientific=FALSE, big.mark = " ")) but I don't know how to combine this function with ggplot. I can imagine that there is an option in the scale_y_continuous() but I was unable to find the solution by myself.
Here is my gist file.
french = function(x) format(x, big.mark = " ")
p + scale_y_continuous(labels=french)