YouTip LogoYouTip

R Scatterplots Charts

A scatter plot displays all data in the form of points on a Cartesian coordinate system to show the degree of interaction between variables. The position of the points is determined by the values of the variables, and each point corresponds to an X and Y axis coordinate. Scatter plots can be drawn using the plot() function, with the following syntax: plot(x, y, type="p", main, xlab, ylab, xlim, ylim, axes) * **x** the data set for the x-axis (horizontal coordinate) * **y** the data set for the y-axis (vertical coordinate) * type: the type of plot, p for points, l for lines, o for both points and lines with lines passing through points. * **main** the chart title. * **xlab, ylab** the label names for the x-axis and y-axis. * **xlim, ylim** the range of the x-axis and y-axis. * **axes** Boolean value, whether to draw two x-axes. The type parameter can select the following values: * p: point plot * l: line plot * b: both points and lines * c: only the lines shown in parameter b * o: both points and lines, with lines passing through points * h: draw vertical lines from points to the horizontal axis * s: step plot, first horizontal then vertical * S: step plot, first vertical then horizontal * n: empty plot Create a simple line plot: ## Example x<-c(10,40) y<-c(20,60) # Generate png image png(file="runnob-test-plot2.png") plot(x, y, "l") !(#) Create a simple line plot, using the type "o" parameter, which draws both points and lines with lines passing through points: ## Example x<-c(10,40) y<-c(20,60) # Generate png image png(file="runnob-test-plot.png") plot(x, y, "o") Next, we use R language's built-in dataset mtcars for testing. !(#) We use the wt and mpg columns from the mtcars dataset: ## Example input <-mtcars[,c('wt','mpg')] print(head(input)) The output is: wt mpg Mazda RX4 2.620 21.0Mazda RX4 Wag 2.875 21.0Datsun 710 2.320 22.8Hornet 4 Drive 3.215 21.4Hornet Sportabout 3.440 18.7Valiant 3.460 18.1 Next, we use the above data to create a scatter plot: ## Example # Data input <-mtcars[,c('wt','mpg')] # Generate png image png(file="scatterplot.png") # Set the x-axis range from 2.5 to 5, y-axis range from 15 to 30. plot(x = input$wt,y = input$mpg, xlab ="Weight", ylab ="Milage", xlim =c(2.5,5), ylim =c(15,30), main ="Weight vs Milage" ) !(#) ### Scatter Plot Matrix A scatter plot matrix uses the method of drawing two-variable scatter plots. It can be considered as a large graphical matrix, where each non-diagonal element's position contains a scatter plot of the variable in that row versus the variable in that column. The diagonal elements contain the variable names. This way, the scatter plot matrix clearly shows the correlation relationships between pairs of variables being studied. A scatter plot matrix draws scatter plots for each pair of numerical variables in the dataset. R language uses the following function to create a scatter plot matrix: pairs(formula, data) Parameters: * **formula** a series of variables * **data** the data set of variables ## Example # Output image png(file="scatterplot_matrices.png") # 4 variables to draw matrix, 12 plots pairs(~wt+mpg+disp+cyl,data=mtcars, main ="Scatterplot Matrix") !(#)
← Redis GeoR Charts Cn β†’