Adding space in the variable names with aes_string

2019-07-20 20:01发布

问题:

This is part of my df:

      CWRES      ID  AGE  BMI  WGT
3     0.59034000  1 37.5 20.7 64.6
4     1.81300000  1 37.5 20.7 64.6
5     1.42920000  1 37.5 20.7 64.6
6     0.59194000  1 37.5 20.7 64.6
7     0.30886000  1 37.5 20.7 64.6
8    -0.14601000  1 37.5 20.7 64.6
9    -0.19776000  1 37.5 20.7 64.6
10    0.74208000  1 37.5 20.7 64.6
11   -0.69280000  1 37.5 20.7 64.6
38   -2.42900000  1 37.5 20.7 64.6
39   -0.25732000  1 37.5 20.7 64.6
40   -0.49689000  1 37.5 20.7 64.6
41   -0.11556000  1 37.5 20.7 64.6
42    0.91036000  1 37.5 20.7 64.6
43   -0.24766000  1 37.5 20.7 64.6
44   -0.14962000  1 37.5 20.7 64.6
45   -0.45651000  1 37.5 20.7 64.6
48    0.53237000  2 58.5 23.0 53.4
49   -0.53284000  2 58.5 23.0 53.4
50   -0.33086000  2 58.5 23.0 53.4
51   -0.56355000  2 58.5 23.0 53.4
52    0.00883120  2 58.5 23.0 53.4
53   -1.00650000  2 58.5 23.0 53.4
80    0.85810000  2 58.5 23.0 53.4
81   -0.71715000  2 58.5 23.0 53.4
82    0.44346000  2 58.5 23.0 53.4
83    1.09890000  2 58.5 23.0 53.4
84    0.98726000  2 58.5 23.0 53.4
85    0.19667000  2 58.5 23.0 53.4
86   -1.32570000  2 58.5 23.0 53.4
89   -4.56920000  3 43.5 26.7 66.2
90    0.75174000  3 43.5 26.7 66.2
...

I want to plot this:

for(i in names(tab3)[2:5]) {
  df2 <- tab3[, c(i, "CWRES")]
  p99 = ggplot(tab3) + geom_point(aes_string(x = i, y = "CWRES"))
  print(p99)
 }

The problem is that if I want to change the xlab it does not work because I am using aes_string().

This does not work:

names(tab3) = c("ID","age [years]","BMI (kg/m2)","body weight ,kg")
for(i in names(tab3)[2:5]) {
    df2 <- tab3[, c(i, "CWRES")]
  p99 = ggplot(tab3) + geom_point(aes_string(x = i, y = "CWRES"))
  print(p99)
}

I also tried another way:

namecol = c("ID","age [kgg]","BMI","bodyweight")
for(i in names(tab3)[2:5]) {
    df2 <- tab3[, c(i, "CWRES")]
  p99 = ggplot(tab3) + geom_point(aes_string(x = i, y = "CWRES")) +
  ylab("CWRES") +
    for(a in namecol[1:4]) {
      xlab(namecol[a])
    }
  print(p99)
}

but it does not work either.

How can I get this to work?

回答1:

I think the key is that you would need backticks around names that are not syntactically valid in aes_string. And putting the backticks inside the quotes is a trick that I don't know.

Prior to the existence of aes_string, there was this trick to be able to use ggplot in functions. This would allow you to get around (rather than solve) the problem you are having.

This will involve creating a plotting function based on your dataset tab3 and then going through the names of your explanatory variables with lapply. Here I name the plotting function plot1.

names(tab3) = c("CWRES", "ID","age [years]","BMI (kg/m2)","body weight ,kg")

plot1 = function(variable) {
    tab3$variable = tab3[,variable]
    ggplot(tab3, aes(x = variable, y = CWRES)) +
        geom_point() +
        xlab(variable)
}

# Make a single plot
plot1(names(tab3)[3])

# Make all the plots; name object if want to save list
lapply(names(tab3)[2:5], function(x) plot1(x))


回答2:

The problem cause just like @aosmith said, but it actually can be solved literally by putting backticks into the string:

new_names <- paste0("`", names(tab3), "`")

And it will work with aes_string.

As a side note, I also tried another simple method like this:

ggplot(tab3) + geom_point(aes(x = tab3[[i]], y = CWRES))

which worked but not as ideal because the axis label will be just the expression tab3[[i]].



标签: r loops ggplot2