使用aes_string在变量名称中添加空格 [英] Adding space in the variable names with aes_string

查看:49
本文介绍了使用aes_string在变量名称中添加空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我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
...

我想画这个:

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)
 }

问题是,如果我想更改xlab,则该操作不起作用,因为我正在使用 aes_string().

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

这不起作用:

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)
}

但它也不起作用.

如何使它正常工作?

推荐答案

问题原因就像@aosmith所说,但实际上可以通过在字符串中添加反引号来解决:

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), "`")

它将与 aes_string 一起使用.

作为旁注,我还尝试了另一种简单的方法,如下所示:

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

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

可行,但并不理想,因为轴标签只是表达式 tab3 [[i]] .

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

这篇关于使用aes_string在变量名称中添加空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆