1

I have two vectors - vector varnames contains names of variables and vector descs contains their descriptions. I want to paste0 them together, but have the descriptions be italic in a barchart.

I have this right now:

labels <- paste0(varnames, "\n", descs)

but I want something like

labels <- paste0(varnames, "\n", italic(descs))

I'm familiar with expression and substitute but I don't know how to use them for this.

EDIT: I'm familiar with how to do it for two strings. My question is about two vectors.

elbord77
  • 311
  • 1
  • 2
  • 11
  • Possible duplicate of [R plot title with uppercase and italic](https://stackoverflow.com/questions/33584083/r-plot-title-with-uppercase-and-italic) – divibisan Feb 15 '19 at 18:41

1 Answers1

0

Let assume some values for varmanes and descs:

varnames = c("a", "b", "c")
descs = c("desc a", "desc b", "desc c")

Then you can get expression of your strings with parse function:

gsub(" ", "~", descs) %>% 
  paste('"',varnames, '\n "*italic(',.,')', sep = "") %>%
   parse(text = .) -> eNames

eNames
#expression("a
# "*italic(desc~a), "b
# "*italic(desc~b), "c
# "*italic(desc~c))

Notice I used dplyr's pipe (i.e. %>%) only for code clearness. Here's an example using above results and ggplot:

df <- data.frame(dose=c("D0.5", "D1", "D2"),
                 len=c(4.2, 10, 29.5))

ggplot(data=df, aes(x=dose, y=len)) +
  geom_bar(stat="identity") + 
  scale_x_discrete(labels = eNames) +
  theme(text = element_text(size = 20)) +
  coord_flip() +
  labs(x = '')

enter image description here

Ulises Rosas-Puchuri
  • 1,900
  • 10
  • 12