Create Scatter plot from CSV in R
Last Updated :
23 May, 2024
In R Programming Language we use plot() function to display scatterplot. It takes eight parameters.
Syntax: plot(x, y, main, xlab, ylab, xlim, ylim, axes)
Parameters:
- x: sets variable to be used for horizontal coordinates.
- y: sets variable to be used for vertical coordinates.
- xlab: label for horizontal axis.
- ylab: label for vertical axis.
- main: title of the chart.
- xlim: limits of x for plotting values of x.
- ylim: limits of y for plotting values of y.
- axes: it indicates whether both axes should be drawn on the plot.
To set attributes of scatter plot like color and shape of scatter plot points we use "col" attribute to set the color of scatter plot and to set shape we use "pch", where pch takes numeric values between 0 and 25.
Creating DataFrame:
Here we are going to create dataframe from this dataset. We use read.csv() to read csv file and store that data in a variable.
R
csv_data<-read.csv("diamonds.csv")
print(csv_data)
Output:
Example 1: In this example we create a simple scatter plot, where x is set to carat and y, is set to price. We label the plot as Price vs Carat.
Syntax: plot(x, y, main, xlab, ylab, col, pch)
Where x is carat data, y is price data, xlab is label for x as "Carat" and ylab is label for y as "Price".
R
plot(x = csv_data$carat,
y = csv_data$price,
xlab = "Carat",
ylab = "Price",
main = "Price vs Carat")
Output:
Example 2: In this example we try to set attributes of scatter plot like color and shape of scatter plot points. We set pch value 4 where value 4 is represented as "x" and color is set to green.
R
plot(x = csv_data$carat,
y = csv_data$price,
xlab = "Carat",
ylab = "Price",
col = "green",
pch = 4,
main = "Price vs Carat")
Output:
Example 3: We can also add a regression line to our scatter plot by using abline() function. We pass 2 parameters in which first we pass lm() function(lm() function is used to fit linear models.) where we specify x and y of our dataset and name of our data and, the second parameter is color of the line.
R
plot(x = csv_data$carat,
y = csv_data$price,
xlab = "Carat",
ylab = "Price",
col = "green",
pch = 4,
main = "Price vs Carat")
abline(lm(csv_data$price ~ csv_data$carat,
data = csv_data), col = "black")
Output:
Similar Reads
How to Create an X-Y Scatter Plot in Excel? Excel is powerful data visualization and data management tool which can be used to store, analyze, and create reports on large data. It can be used to visualize and compare data using a graph plot. In excel we can plot different kinds of graphs like line graphs, bar graphs, etc. to visualize or anal
2 min read
How to Color Scatter Plot Points in R ? A scatter plot is a set of dotted points to represent individual pieces of data in the horizontal and vertical axis. But by default, the color of these points is black and sometimes there might be a need to change the color of these points. In this article, we will discuss how to change the color o
2 min read
How to create a reusable plot_ly function in R In data visualization, reusability and consistency are crucial for maintaining clarity and efficiency. Plotly is the powerful library in the R for creating interactive plots. By encapsulating the plotting logic into the reusable functions. We can streamline the plotting process and it can ensure uni
5 min read
How to Read a CSV from URL into R? In this article, we are going to see how to read CSV files from URL using R Programming Language. Method 1: Using Base RÂ Here we are using read.csv() methods, which is an inbuilt function in R programming. This function is similar to Python because it read the CSV file and returns the data into dat
1 min read
How to plot excel data in R? Plotting graph in R using an excel file, we need an excel file with two-column in it, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be discussing the approach
2 min read
Create a Scatter Plot with Multiple Groups using ggplot2 in R In this article, we will discuss how to create a scatter plot with multiple groups in R Programming Language. Geoms can be added to the plot to compute various graphical representations of the data in the plot (points, lines, bars). The geom_point() method is used to create scatter plots in R. The g
2 min read