0

I'm trying to use paste in a for loop in R to create new expressions for a ggplot. I currently have:

x_axis_label <- list()
for (i in seq_len(4)) {
  x_axis_label[[i]] <- expression(paste("10"^as.character(i)))
}

but this returns:

[[1]]
expression(paste("10"^as.character(i)))

[[2]]
expression(paste("10"^as.character(i)))

[[3]]
expression(paste("10"^as.character(i)))

[[4]]
expression(paste("10"^as.character(i)))

Is it possible to use a for loop and paste a value within an expression in R?

EDIT: I would like to have a vector similar to the below:

x_axis_label <- c(expression(paste("10"^"2")),
                  expression(paste("10"^"3")),
                  expression(paste("10"^"4")))
expression(paste("10"^"2"), paste("10"^"3"), paste("10"^"4"))

Thanks.

AW27
  • 481
  • 3
  • 15
  • 1
    Isn't this similar to your older question https://stackoverflow.com/questions/65088103/add-two-subscripts-to-expression-with-for-loop-in-r ? – Ronak Shah Apr 20 '21 at 09:54
  • I suppose it is, but does it still work for `paste`? – AW27 Apr 20 '21 at 10:00
  • You don't need `paste` for that. `x_axis_label[[i]] <- substitute(expression(10^i), list(i = i))` works ? – Ronak Shah Apr 20 '21 at 10:20
  • It doesn't seem to be working. I'd also like to get a vector like `c(expression(paste("10"^"2")), expression(paste("10"^"3")), expression(paste("10"^"4")))` – AW27 Apr 20 '21 at 10:29
  • Can you update your post to include your expected output? – Ronak Shah Apr 20 '21 at 10:51
  • I updated it, it appears the question was voted to close but I haven't been able to use the other post to get the result I am looking for. – AW27 Apr 20 '21 at 10:53
  • In that case don't you need `x_axis_label[[i]] <- substitute(expression(paste(10^i)), list(i = i))` ? – Ronak Shah Apr 20 '21 at 11:01
  • That doesn't seem to work because it gives "10^2" and includes the `^` symbol. That is why I was previously creating the vector like the above one manually. – AW27 Apr 20 '21 at 11:02

1 Answers1

0

Instead of expression you could just put everything in a string and then tell the respective ggplot-function (annotate, geom_text or whatever) that it should parse the string. E.g

x_axis_label <- list()
for (i in seq_len(4)) {
  x_axis_label[[i]] <- paste("10^",as.character(i))
}

library(ggplot2)
ggplot() +
  annotate("text", x=1, y=1, label=x_axis_label[1], parse=T)
NicolasH2
  • 774
  • 5
  • 20
  • Thanks, the problem is I want each of the x-axis ticks to have the labels provided above. I understand that there is a function `scale_x_log10` but I'm not sure how I can start it from 10^2 to 10^4. – AW27 Apr 20 '21 at 11:36
  • can't you just that the axis-ticks via `ggplot() + scale_y_continuous(breaks=c(10^2,10^3,10^4))` ? If you need the text to be in scientific format you can instead do: `scale_y_continuous(breaks=c(10^2,10^3), labels = function(x) format(x, scientific = TRUE))` – NicolasH2 Apr 20 '21 at 11:41
  • @Solarian I'm not sure but that doesn't seem to work with my data. I have `min(n) = 1` and `max(n)=201` so it's not allowing me to break up the x-axis that easily. – AW27 Apr 20 '21 at 12:28
  • but if that is the case and your axis doesn't even go so high, then why would you want to add labels of 10^3 and 10^4 to your axis? If you still want the axis to overreach your data points, you can control the axis limits with `ggplot() + xlim(c(1,10000))` – NicolasH2 Apr 20 '21 at 12:34
  • I am doing it because the data was collected at an exponential rate. So I want to label the ticks as such. – AW27 Apr 20 '21 at 12:44
  • if your data was collected at an exponential rate and the x value (e.g. time) reflects that, than your labeling for the ticks should be fine and don't need to be changed (at least from what I understand). Can you share part of your data, so that we can understand what this is all about? – NicolasH2 Apr 20 '21 at 14:03
  • My data frame is similar to `new_data_frame <- data.frame(n = c(1:201), x = rnorm(201), y = rnorm(201))`, where I have 201 rows of data, each representing a value of `n`. I typically use `gather` to create a data frame I can plot. So here I would try to plot `n` on the x-axis, but would have trouble with the log10 rule because `n=1` actually means `n=10^2` and `n=201` means `n=10^4`. – AW27 Apr 20 '21 at 15:06
  • mh...still not quite sure where the problem is. You mentioned earlier that you just wanted to set the limits. What about: `ggplot(new_data_frame, aes(n,y)) + geom_point() + scale_x_log10(limits=c(10,100))` ? – NicolasH2 Apr 20 '21 at 15:13
  • I think the problem is that I want to use "10^2" where n = 1 rather than 100 which would be correct. If I set the code up correctly I get "10^0" instead of "10^2" – AW27 Apr 20 '21 at 16:18