0

I am generating a bunch of plots and have stored the appropriate labels for axes in my plots in a list:

labels_list <- c("C[max,1] (mg/L)", "AUC[6wks,1] (mg*day/L)", "somethingElse[subscriptText] (units*might/have*symbols)")

My goal is to create labels where the text in the square brackets are subscripts. I have been using parse() which is okay, except:

  • I can't figure out how to get the axes labels to be bold, though it is specified in my custom ggplot theme
  • The asterisk disappears.

I know that bquote() is an option, but I haven't figured out how to use it if I just want to provide a list of strings as inputs like above. I'm definitely open to any suggestions and solutions provided.

EDIT: It seems that I cannot use my string inputs as-is? labels_list is extracted from a data frame and I would love to not have to, one-by-one, change this for plotting purposes. :-(

jnguyen
  • 19
  • 2
  • Would this [SO post](https://stackoverflow.com/questions/50521699/how-to-add-subscripts-to-ggplot2-axis-text) help? – Rui Barradas Jun 18 '20 at 14:12
  • You really should study `help("plotmath")`. – Roland Jun 18 '20 at 14:15
  • Roland, I understand and have looked at plotmath, bquote, expression, parse, but that still doesn't help my original input. Do I have to manually have the inputs in these long, special formats for it to be correct? I am asking this is because `labels_list` comes from a column in my dataframe that has the labels and units for the variables I am using. While parse() will automatically put the characters in the square brackets as appropriate subscripts, I still have the problems mentioned. If there's a better way to look at things, I will consider, but I don't really appreciate the redirection. – jnguyen Jun 18 '20 at 14:24
  • @IanCampbell Your post was helping a lot, I will vote to undelete it. – Rui Barradas Jun 18 '20 at 14:24
  • @RuiBarradas Alright, feel free to edit it if you want. – Ian Campbell Jun 18 '20 at 14:26
  • @IanCampbell Done, see now. It's pratically your post with asterisks and tildes in this or that place. Most notably, in my opinion, to have 2 subscripts (`max,1` and `6wks,1`) the comma needs to be quoted. – Rui Barradas Jun 18 '20 at 14:29
  • If you want to use plotmath, you have to write plotmath syntax. Now, you could automate transforming your labels into correct syntax by using `gsub`. Personally I think plotting the asterix is just ugly. I would write `(mg~d~L^{-1})` or at least replace `*` with `%.%`. – Roland Jun 19 '20 at 05:22

1 Answers1

1

Here's an approach with expression and bold:

labels_list <- c(expression(bold(C[max * ',' * 1]~(mg/L))), 
                  expression(bold(AUC["6wks"*','*1]~(mg * '*' * day/L))),
                  expression(bold(somethingElse[subscriptText]~(units * '*' * might/have * '*' *symbols))))

ggplot(data = data.frame(x = 1, y = 1), aes(x,y)) +
  geom_point() + 
  labs(x = labels_list[1], y = labels_list[2], caption = labels_list[3])

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • Tildes seem to be better than asterisks. – Rui Barradas Jun 18 '20 at 14:14
  • OP wants `(units*'*'*might/have*'*'*symbols)` (although better alternatives are provided by plotmath) – Roland Jun 18 '20 at 14:14
  • 1
    Thanks for reminding me. I sometimes get caught up in what I think is the "hard" part. – Ian Campbell Jun 18 '20 at 14:16
  • I've played with something like this, but I don't necessarily want to manually have to convert all of the strings in labels_list to accommodate this. It's also not entirely correct - I need the asterisk as a multiplier (the units are mg TIMES day PER litre) and everything INSIDE the brackets needs to be subscripted. – jnguyen Jun 18 '20 at 14:17
  • 1) `expression(bold(C[max * ',' * 1]~(mg/L)))` ; 2) `expression(bold(AUC["6wks"*','*1]~(mg * '*' * day/L)))` ; 3) `expression(bold(somethingElse[subscriptText]~(units * '*' * might/have * '*' * symbols)))` – Rui Barradas Jun 18 '20 at 14:20
  • Hi, to reiterate what I asked above with Roland: Does this mean I have to go and change `labels_list` to accommodate this? It's being pulled automatically from a column in the dataframe I am using, and I would prefer not to go in and manually do this if I can help it. Or if there's a more efficient way or different approach to thinking, I would appreciate learning. – jnguyen Jun 18 '20 at 14:28
  • @jnguyen The post linked to in my comment to the question changes the labels manually. The main tricks are to quote literals and to include spacing chars like `*` and `~` . – Rui Barradas Jun 18 '20 at 14:32
  • Yes, I can see that the labels were changed manually.... But I'm more interested in hearing if there is a more clever solution. I provided a small example but the reality is that I have 20+ variables to plot and want to do it in a controlled/programmatic manner... – jnguyen Jun 18 '20 at 14:40