[R] Problem with creating a PCA graph in a loop
Gavin Duley
gdu|ey @end|ng |rom gm@||@com
Wed May 8 22:57:56 CEST 2024
> On 8 May 2024, at 09:16, Ivan Krylov <ikrylov using disroot.org> wrote:
>
> В Tue, 7 May 2024 16:57:14 +0200
> gavin duley <gduley using gmail.com> пишет:
>
>> aes(label=current_rownames,
>> colour=wine.data.filt$Treatment
>> )
>
> As you've noticed, aes() remembers variables by their name and
> environment, not by value:
Yes, it was something I wasn’t aware of previously. I thought once I’d created a ggplot object, it was fairly static/unchanging.
That this is limited to aes() explains why the title set using ggtitle isn’t affected in the same way — thanks!
> One way to get around the problem is to ensure that the variables live
> in different environments. Instead of making it a for loop, write a
> function that would accept `i` and return a plot instead of assigning
> it by name:
Thanks, that does seem like a good option. I’m less familiar with writing functions, but it’s something I’m trying to learn about. I’ll try it and see how it goes.
> (In many languages, trying to use a variable as a variable name, while
> possible, usually means you need to consider some kind of nested data
> structure:
> https://perldoc.perl.org/perlfaq7#How-can-I-use-a-variable-as-a-variable-name?
> In R, this structure is a list.)
Ok, that’s useful to know, thanks. I don’t really have much programming experience beyond R, so there’s a lot I don’t know.
> Alternatively, supply a data= argument to geom_label_repel() and make
> your mapping = aes(...) reference variables from the data (which will
> be remembered), ignoring the environment (which is only referenced).
> Something like the following should work, untested:
Thanks, I’ll try that out too.
Many thanks for giving me a few suggestions. I was completely stuck, but these have given me a few good things to try!
Thanks,
gavin,
More information about the R-help
mailing list