What I want: to create a function to insert a line break between every letter of a character variable in R.
What I tried: but it didn't work
wrap_letters <- function(x){ z <- substring(x, 1, 1) # Take the first letter of x and save it in z for(i in 2:stri_length(x)) { #from the second to the length of x w <- substring(x, i, 1) #take the respective letter and save it to w z <- paste0(z,"\n", w) #paste z, "\n", w } z #return z } Reproducible example with data (using ToothGrowth that comes within R):
df <- ToothGrowth %>% mutate(dose = factor(dose), supp = case_when( supp=="OJ" ~ "orange juice", T ~ "ascorbic acid"), supp_label = wrap_letters(supp)) Aplication: to vertically write labels in a facet_grid plot: I want to rotate letters to normal position (i.e. horizontal), but to place letters below each other, so they don't take too much width:
bp <- ggplot(df, aes(x=dose, y=len, group=dose)) + geom_boxplot(aes(fill=dose)) + theme( strip.text.y = element_text(angle = 0) ) + facet_grid(supp_label ~ dose) bp Desired result:
df <- ToothGrowth %>% mutate(dose = factor(dose), supp_label = case_when( supp=="OJ" ~ "o\nr\na\nn\ng\ne\n \nj\nu\ni\nc\ne", T ~ "a\ns\nc\no\nr\nb\ni\nc\n \na\nc\ni\nd")) bp <- ggplot(df, aes(x=dose, y=len, group=dose)) + geom_boxplot(aes(fill=dose)) + theme( strip.text.y = element_text(angle = 0) ) + facet_grid(supp_label ~ dose) bp Note: This is a small reproducible example, I have more categories in my dataframe and I want to make everything reproducible, that is why I need a function.
https://stackoverflow.com/questions/67428819/create-a-function-that-insert-a-line-break-between-every-letter-of-a-character-v May 07, 2021 at 11:58AM
没有评论:
发表评论