Scatter Plot which also called a scatter graph, scattergram, scatter chart, scatter diagram, or is a mathematical diagram or type of plot using Cartesian coordinates to display values for typically two variables from a given set of data.
Scatterplot graph displays the value of 2 sets of data on 2 dimensions and each dot represents an observation.
The position of the Y (vertical) and X (horizontal) axis represents the values of the 2 variables in the scatter plot hence it’s really useful to study the relationship between both variables.
A scatterplot can be created using the function plot ().
Basic Syntax for Scatter Plot in R is as follows:
plot(x, y, main, xlab, ylab, xlim, ylim, axes)
Following is the description of the parameters used −
- x: It contains the value for the data set whose values are the horizontal coordinates.
- y: It contains the value for the data set whose values are the vertical coordinates.
- main : It represents the tile of the graph.
- xlab : It represents the label in the horizontal axis.
- ylab : It represents the label in the vertical axis.
- xlim : It is the limits of the values of x used for plotting.
- ylim : It is the limits of the values of y used for plotting.
- axes : It indicates whether both axes should be drawn on the plot.
To create and run our R program in Visual Studio we must first install some tools.
Now open Visual Studio on your pc and then click on Get Tools and features… inside Tools.
Now in the feature installation window check and install the following features.
For Visual Studio 2017
Contains Tools for Data Science.
After this please install R tools from the following link if the above package is not installed properly from the following link
But it is not necessary
https://docs.microsoft.com/en-us/visualstudio/rtvs/installing-r-tools-for-visual-studio
You can also install R programming tool go to the following link for better support and download the software and install it.
For Windows Users:
For Linux Users:
If you are on Linux platform then you can use this fast and easy command used in Linux which can be used to install R. The yum command is used for installing like this:
$ yum install R
For Ubuntu Linux or other Debian-related OSs, a more direct method is:
$ apt-get install r-base
Now let us start creating our R application in visual studio.
Go to Files ->Project and add a new R project
This is our project structure:
Now on the top left side is a new R file (script.R) where we can edit source code with all of Visual Studio Ide editing features.
Also on the bottom left of Visual Studio is where you can find an R Interactive window in which you can interactively develop and test code.
# Get the input values.
input <- mtcars[,c('wt','mpg')]
# Plot the chart for cars with weight between 2.5 to 5 and mileage between 15 and 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”)
# Give the chart file a name.
png(file = “scatterplot.png”)
# Save the file.
dev.off()
You can either this code inside Script.R or on R Interactive console it self.
Visual Studio will generate this scatter plot on a different Window if it cannot display the result on the console itself.
Output:
Syllabus of Data Science training in Mumbai