Marking points in area R are not printed if matrix instead of data frame

OK ... Silly simulated data ... Subject names by initials, SAT scores and later in life income in thousands of dollars. The entries are scaled and centered and look like this:

names <- c("TK","AJ","CC", "ZX", "FF", "OK", "CT", "AF", "MF", "ED", "JV", "LK", "AS", "JS", "SR", "CF", "MH", "BM")
SAT <- c(1345, 1566, 1600, 1002, 1008, 999, 1599, 1488, 950, 1567, 1497, 1300, 1588, 1443, 1138, 1557, 1478, 1600)
income <- c(150e3, 250e3, 300e3, 100e3, 110e3, 199e3, 240e3, 255e3, 75e3, 299e3, 300e3, 125e3, 400e3, 120e3, 86e3, 225e3, 210e3, 60e3)

dat <- cbind(SAT, income)
row.names(dat) <- names
dat <- scale(dat, scale = T, center = T)

plot(income ~ SAT, col=as.factor(rownames(dat)), pch= 19, xlim = c(-2.5,2.5), ylim=c(-2.5,2.5), data = dat)
abline(v=0,h=0, col = "dark gray")
text(x=dat$SAT, y=dat$income, rownames(dat), pos=3, cex = 0.5)

      

... results in the correct plot except for the missing labels. Here's the graph and error message:

enter image description here  The error is that the $ SAT: $ operator is not valid for atomic vectors

However, as I figured out before doing something drastic :-) everything changes if and only if I do this tiny code modification before plotting:

dat <- as.data.frame(dat)

      

And now...

dat <- cbind(SAT, income)
row.names(dat) <- names
dat <- scale(dat, scale = T, center = T)

dat <- as.data.frame(dat)
plot(income ~ SAT, col=as.factor(rownames(dat)), pch= 19, xlim = c(-2.5,2.5), ylim=c(-2.5,2.5), data = dat)
abline(v=0,h=0, col = "dark gray")
text(x=dat$SAT, y=dat$income, rownames(dat), pos=3, cex = 0.5)

      

enter image description here

So, I think the problem is to always make sure we are dealing with a dataframe before marking the points (?). Is R so internally unfriendly because it was built to be successful without commercial interest, or because it has so many layers that it makes it awkward? (I'm distracted - I don't want to start a conversation ...)

+3


source to share


1 answer


In the future, you must provide your details in case this could be a problem. But I don't think that's the case. You can do your labeling with the simpler text () function after creating the plot:

names <- c(
  "TK","AJ","CC", "ZX", "FF", "OK", "CT", "AF", "MF", 
  "ED", "JV", "LK", "AS", "JS", "SR", "CF", "MH", "BM"
)
SAT <- c(
  1345, 1566, 1600, 1002, 1008, 999, 1599, 1488, 950, 
  1567, 1497, 1300, 1588, 1443, 1138, 1557, 1478, 1600
)
income <- c(150e3, 250e3, 300e3, 100e3, 110e3, 199e3, 240e3, 
  255e3, 75e3, 299e3, 300e3, 125e3, 400e3, 120e3, 86e3, 
  225e3, 210e3, 60e3
)

dat <- data.frame(SAT, income)
dat <- scale(dat, scale = T, center = T)

##  Note the conversion here back to a data.frame object, since scale()
##    converts to a matrix object:
dat <- as.data.frame(dat)
row.names(dat) <- names

plot(income ~ SAT, col=as.factor(rownames(dat)),
     pch= 19, xlim = c(-2.5,2.5), ylim=c(-2.5,2.5),
     data = dat)

##  Plot text labels above (pos=3) point locations:
text(x=dat$SAT, y=dat$income, row.names(dat), pos=3, cex=0.5) 

      



enter image description here

+4


source







All Articles