简体   繁体   English

如何使用汽车包版本 3 在 scatterplot() 中使用图例参数

[英]how to use legend argument in scatterplot() using car package version 3

I am new to R and trying to work through some examples in the excellent "R in Action" by Robert Kabacoff.我是 R 的新手,并试图通过 Robert Kabacoff 出色的“R in Action”中的一些示例进行工作。

He makes use of the car package to make a scatterplot.他利用汽车包制作散点图。 While trying the following example, I found that I was getting warnings and the plot did not match the one in the book:在尝试以下示例时,我发现我收到警告并且情节与书中的情节不符:

library(car)

scatterplot(mpg ~ wt | cyl, data=mtcars, lwd=2, span=0.75, 
            main="Scatter Plot of MPG vs. Weight by # Cylinders", 
            xlab="Weight of Car (lbs / 1000)", 
            ylab="Miles Per Gallon", legend.plot=TRUE, id.method="identify", 
            labels=row.names(mtcars), boxplot="xy")

After some digging, I discovered that the car package has updated to version 3, and the book uses version 2. Apparently, the argument input to most car functions has completely changed.经过一番挖掘,我发现car包更新到了​​版本3,本书使用了版本2。显然,大多数car函数的参数输入已经完全改变了。

I did find documentation for it at https://cran.r-project.org/web/packages/car/car.pdf , but I couldn't completely follow how all the arguments work.我确实在https://cran.r-project.org/web/packages/car/car.pdf找到了它的文档,但我无法完全理解所有参数是如何工作的。

I got very close to getting the plot to work correctly with the following code:我非常接近使用以下代码使绘图正常工作:

scatterplot(mpg ~ wt | cyl, data = mtcars, pch=c(1,2,3), 
            smooth = list(smoother=loessLine, span = .75, lty.smooth=1), 
            main = "Scatter Plot of MPG vs. Weight by # Cylinders", 
            xlab = "Weight of Car (lbs/1000)", ylab = "Miles per Gallon", 
            legend = c(title="cyl", coords="topleft"), 
            id = list(method="identify"),
            showlabels = names(row.names(mtcars)), regLine=c(method=lm, lty=1))

But I can't seem to completely follow how to use the new arguments, especially the legend.但我似乎无法完全理解如何使用新参数,尤其是图例。 I can't seem to be able to plot the legend by coordinates, it only seems to work if I use the coords="topleft" or coords="bottom" type arguments.我似乎无法通过坐标绘制图例,它似乎只有在我使用coords="topleft"coords="bottom"类型参数coords="topleft"

Can anyone explain how to use the legend argument in scatterplot() in the car package version 3?谁能解释一下如何在汽车包版本 3 中的scatterplot()中使用图例参数? Specifically how to plot it in specific coordinates, and outside the plot area?具体如何在特定坐标和绘图区域外绘制它?

If anyone can point me to a tutorial that isn't version 2, that would be helpful as well.如果有人可以向我指出不是版本 2 的教程,那也会有所帮助。

In general, I find ggplot a lot easier than the base plot functions.一般来说,我发现ggplot比基本plot函数容易得多。 You do need to set the cyl variable as a factor though.不过,您确实需要将 cyl 变量设置为一个因素。

ggplot(data = mtcars, aes(x = wt, y = mpg, color = as.factor(cyl), shape = as.factor(cyl))) +
 geom_point() + # plots the scatter plot
 geom_smooth(method = "lm", se = F) + # plots the linear model
 geom_smooth(se = F) + # plots the loess model
 theme_minimal() # changes some of the formatting

在此处输入图片说明

I also highly recommend R for Data Science by Hadley Wickham ( http://r4ds.had.co.nz/ ) for getting started in R. And its free!我还强烈推荐 Hadley Wickham ( http://r4ds.had.co.nz/ ) 的 R for Data Science 开始使用 R。而且它是免费的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM