0

I am trying to create multiple exploratory scatterplots from a big dataset using a for loop. My x will always be the same (Gender), but for my y I want to loop through all of the columns. In the graph, I want the x-axis label to be Gender and the y-axis label to be whatever column of the data the loop is on.

Here is the code:

Library (ggplot)

for (i in df_red){
  g = ggplot(data = df_red) +geom_point (mapping = aes(x=df_red$Gender, y= i, colour = factor(df_red$Group_New))) 
  g = g +labs( x= 'Gender' , y = i,  colour = 'Group') + scale_color_manual(labels =c('1', '2', '3'), values = c('1', '2', '3')) 
  print(g)}

When I run the loop, the graphs look great, all of the data points are there and the X axis and legend are accurate. My problem is the y axis is labeled with the first data point of the column (e.g, 7.25) instead of the column name.

I sort of understand why it's doing this, but I have no idea what I need to change in order to make this work so I know which column I'm looking at in a given graph.

Any help is much appreciated, I am pretty new to R and feeling lost.

mt75345
  • 1
  • 1
  • Can you try this https://stackoverflow.com/a/52045613/786542? – Tung Apr 11 '19 at 17:50
  • 2
    My guess is you want to loop through the *names* of the variables instead of the columns. You can then use `aes_string()` or tidy evaluation from the link above. Also you don't need the dollar sign notation in `aes()`. Use, e.g., `Gender` instead of `df_red$Gender`. – aosmith Apr 11 '19 at 17:55
  • @aosmith using names and aes_string worked perfectly! thank you! – mt75345 Apr 11 '19 at 18:59

0 Answers0