In this example, we are going to fit a linear and a non-parametric model with lm and lowess functions respectively, with default arguments. For that purpose you can add regression lines (or add curves in case of non-linear estimates) with the lines function, that allows you to customize the line width with the lwd argument or the line type with the lty argument, among other arguments. group <- as.factor(ifelse(x < 0.5, "Group 1", "Group 2")) plot(x, y, pch = as.numeric(group), col = group)Īs we said in the introduction, the main use of scatterplots in R is to check the relation between variables. If you have a variable that categorizes the data points in some groups, you can set it as parameter of the col argument to plot the data points with different colors, depending on its group, or even set different symbols by group. In case you need to look for more arguments or more detailed explanations of the function, type ?identify in the command console. In this example we are going to identify the coordinates of the selected points. In the labels argument you can specify the labels you want for each point. Moreover, you can use the identify function to manually label some data points of the plot, for example, some outliers. Plot(y ~ x, pch = 19, col = "black") # Equivalent In order to plot the observations you can type: plot(x, y, pch = 19, col = "black") You can review how to customize all the available arguments in our tutorial about creating plots in R.Ĭonsider the model \(Y = 2 + 3X^2 + \varepsilon\), being \(Y\) the dependent variable, \(X\) the independent variable and \(\varepsilon\) an error term, such that \(X \sim U(0, 1)\) and \(\varepsilon \sim N(0, 0.25)\). You can also specify the character symbol of the data points or even the color among other graphical parameters. Passing these parameters, the plot function will create a scatter diagram by default. You can create scatter plot in R with the plot function, specifying the \(x\) values in the first argument and the \(y\) values in the second, being \(x\) and \(y\) numeric vectors of the same length. The main use of a scatter plot in R is to visually check if there exist some relation between numeric variables. Scatter plots are dispersion graphs built to represent the data points of variables (generally two, but can also be three).
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |