3

My dataframe (df) with column names "8A_1" and 8A_2" is:

   8A_1 8A_2
1     2    2
2     4    3
3     4    4
4     4    3
5     4    3
6     1    4
7     2    4
8     2    4
9     4    3
10    4    4

The variable labels are (which are added to the variables with the help of the Hmisc package)

var.labels = 
    c("8A_1"="Variable 1", 
      "8A_2" = "Variable 2")



library(Hmisc)
  label(df) = lapply(names(var.labels), 
                     function(x) label(df[,x]) = var.labels[x])

var.labels 8A_1 8A_2 "Variable 1" "Variable 2"

plot (df$"8A_1", df$"8A_2")

The above command marks df$"8A_1" and df$"8A_2" in the axes. It does not the variable labels that I want. How can I get variable labels instead of variable names in plots or in other statistical analysis (as it is done for example in SPSS)?

agenis
  • 8,069
  • 5
  • 53
  • 102
Amirul Islam
  • 407
  • 1
  • 6
  • 15

1 Answers1

1

You may try something like this

plot (df$"8A_1", df$"8A_2", xlab=label(df)[1], ylab=label(df)[2])

If you want to change the variable names in lm, try this

var.labels = 
  c("8A_1"="Variable_1", 
    "8A_2" = "Variable_2")
names(df) <- var.labels[names(df)]
lm(as.formula(paste(make.names(var.labels), collapse="~")), data=df)
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
  • thanks, this plot command is working fine. However, in other commands the result is not as expected. For example. the following gives the result, – Amirul Islam Oct 17 '16 at 08:30
  • lm (df$"8A_1" ~ df$"8A_2", xlab=label(df)[1], ylab=label(df)[2]) Call: lm(formula = df$"8A_1" ~ df$"8A_2", xlab = label(df)[1], ylab = label(df)[2]) Coefficients: (Intercept) df$"8A_2" 1.5174 0.4998 Warning message: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : extra arguments c("‘xlab’", "‘ylab’") are disregarded. > – Amirul Islam Oct 17 '16 at 08:32
  • you can't use xlab, ylab arguments in lm – Sandipan Dey Oct 17 '16 at 08:35