2

I'm plotting mpg ~ wt in the R dataset mtcars and I want either the points replaced by the initial of the car model (rowname), or a legend with the rowname beside the points.

I don't know how to code for the first idea, and I get overlapping labels on the second. Is there a way of just labeling with the initial of the car model?

Stripped of colors, and other flourishes, here is the idea:

plot(mpg ~ wt, data = mtcars); text(wt, mpg, row.names(mtcars))

Sorry about the ugliness... BTW I tried with ggplot2... same sort of issues...

enter image description here

The idea behind the replacement of the points with rowname initial came from the paper on the topic of regression by Harold Henderson:

enter image description here

4

1 回答 1

2

我会用你的简称创建一个新变量。为了这个例子,我取了每个行名的第一个字母,但显然有重叠,所以你需要手动指定一个 `legend' 列:

mtcars$legend <- substring(row.names(mtcars), 1, 1)

然后,只需添加legend作为pch参数:

plot(mpg ~ wt, data = mtcars, pch = legend)

带有文本标签的 mpg ~ wt 绘图

顺便说一句,美国汽车的燃油经济性确实没有太大改善!

于 2015-05-03T17:51:25.340 回答