4

I would like to title each plot with the variable (k) I am using to iterate within a for loop:

> MyData
     Name Stat Year value
1  Site01  Max 2013 29.88
2  Site01  Med 2013 29.67
3  Site01  Min 2013 29.16
4  Site01  Max 2020 31.21
5  Site01  Med 2020 30.38
6  Site01  Min 2020 29.38
7  Site01  Max 2040 35.50
8  Site01  Med 2040 33.17
9  Site01  Min 2040 29.60
10 Site02  Max 2013 53.70
11 Site02  Med 2013 53.49
12 Site02  Min 2013 53.20
13 Site02  Max 2020 53.10
14 Site02  Med 2020 53.01
15 Site02  Min 2020 52.55
16 Site02  Max 2040 52.04
17 Site02  Med 2040 51.74
18 Site02  Min 2040 50.98

pdf("plots.pdf")
for (k in unique(MyData$Name)){
subdata <- subset(MyData, Name == k)

     print(ggplot(subdata, aes(x = Year, y = value, colour = Stat)) 
          + geom_line() + expand_limits(y=c(0,100)) + ggtitle(k))

}
dev.off()

The plots are generated just fine when using a string for ggtitle() (for example: ggtitle("Name")) or after removing ggtitle() completely.

Any suggestions for how to include the value of the iterator variable (k) as the title on each plot?

viridius
  • 477
  • 5
  • 17
  • 2
    Please read (1) [how do I ask a good question](http://stackoverflow.com/help/how-to-ask), (2) [How to create a MCVE](http://stackoverflow.com/help/mcve) as well as (3) [how to provide a minimal reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). Then edit and improve your question accordingly. I.e., provide some input data (e.g. an example set from R), so that your code can be copied, pasted and run. – lukeA May 23 '16 at 22:47
  • I edited to include an example set of data (which actually works fine!) so there must be problem in my larger set of code – viridius May 23 '16 at 23:03
  • Well, if you can't reproduce the problem, it's going to be very difficult to help you. How is your data different than your sample? What exactly is the problem you are experiencing? Is there an error? Is anything plotted on any graph? – MrFlick May 23 '16 at 23:08
  • The problem remained while implementing my full set of code/input data. The problem was remedied after using `ggtitle(as.character(k))` as suggested in the answer below. – viridius May 23 '16 at 23:16
  • Instead of using the for-loop, why not go with `ggplot(subdata, aes(x = Year, y = value, colour = Stat, group=Name)) + geom_line() + expand_limits(y=c(0,100)) + facet_wrap(~Name)`? – Adam Quek May 24 '16 at 01:05

2 Answers2

2

What class are the data in MyData$Name ?

Try replacing ggtitle(k) with ggtitle(as.character(k)) to ensure you're passing ggtitle a string.

conor
  • 1,204
  • 1
  • 18
  • 22
  • Were you actually able to reproduce the original problem? Is your claim that `k` is numeric? The code provided by the OP seemed to work just fine for me. I'm not sure what problem this is fixing. – MrFlick May 23 '16 at 22:57
  • Apologies for the poor question. MyData$Name are strings – viridius May 23 '16 at 23:04
  • However, using `ggtitle(as.character(k))` allows for the full set of code to also run sucessfully, many thanks for this – viridius May 23 '16 at 23:11
  • @MrFlick Originally there was not an example of the data, so an incorrect class could have been the issue. – conor May 23 '16 at 23:15
  • @user3830407 You should be careful in this case. Double check the class of each of `unique(MyData$Name)`, as you may have something unexpected there that "works" after coercion but may not be what you intended. – conor May 23 '16 at 23:18
  • @conor Thanks for these words of caution. I have looked through the PDF output and all seems fine. The plot titles show the site names as I would expect. I will remain vigilant as I finalize the script. – viridius May 23 '16 at 23:27
  • @conor I have a follow up question. What if I wanted to name my plots based on another variable, for example `year` corresponding to k in the above case instead of the iterator k, how would I do that? – Manasi Shah May 17 '19 at 02:11
  • @ManasiShah In the example above, OP uses unique values of the `Name` variable and `year` takes multiple values for each `Name`, so it wouldn't work as a title. If `year` was unique for each `Name` though, then in the way they've written the code above, you'd have to reference it like `ggtitle(as.character(unique(MyData$year[Name == k])))` or something similar. This style can get messy though, and I would recommend avoiding for-loops where you can in `R`. Using `tidyverse` syntax like a `group_by` on the `Name` variable would likely make things more clear. Feel free to post a new question too! – conor May 17 '19 at 07:30
1

You could use paste(), example:

ggtitle(paste(your_value))
Buddy
  • 10,874
  • 5
  • 41
  • 58
cj23
  • 31
  • 6