The Wayback Machine - https://web.archive.org/web/20241119095555/https://www.geeksforgeeks.org/how-to-plot-a-graph-in-r-using-csv-file/
Open In App

How to plot a graph in R using CSV file ?

Last Updated : 26 Mar, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

To plot a graph in R using a CSV file, we need a CSV file with two-column, 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 looking at the way to plot a graph using a CSV file in R language.

Approach

  • Import csv file
  • Pass required parameters to plot function
  • Plot graph
  • Display plot

Functions Used:

  • To import/read the CSV file to the R console, the user must need to use the read.csv() function in R.This function will read a CSV file available in your current working directory.

Syntax:

read.csv(file)

  • plot() function for plotting of R objects. With the provided parameters this function returns a scatter plot by default.

Syntax:

plot(x,y,main,xlab,ylab,sub,asp)

Parameters:

  • x:-the x coordinates of points in the plot
  • y:-the y coordinates of points in the plot
  • main:-an overall title for the plot
  • sub:-a subtitle for the plot
  • xlab:-a title for the x-axis
  • ylab:-a title for the y-axis
  • asp:-the y/x aspect ratio

Return:

Scatter plot of the given x and y values.

Example:

R




data=read.csv('input_gfg.csv')
  
print(data)
  
plot(x = data$x,y = data$y,
     xlab = "x-axis",
     ylab = "y-axis",
     main = "Plot"
)


Output:



Previous Article
Next Article

Similar Reads

Plot Paired dot plot and box plot on same graph in R
R Programming Language is used for statistical computing and graphics. R was first developed at the University of Auckland by two professors Ross Ihanka and Robert Gentleman Dot Plot The dot plot is a graphical representation of how one attribute varies with respect to another attribute. On the x-axis, we usually plot the attribute with respect to
7 min read
Set Aspect Ratio of Scatter Plot and Bar Plot in R Programming - Using asp in plot() Function
asp is a parameter of the plot() function in R Language is used to set aspect ratio of plots (Scatterplot and Barplot). Aspect ratio is defined as proportional relationship between width and height of the plot axes. Syntax: plot(x, y, asp ) Parameters: x, y: Coordinates of x and y axis asp: Aspect ratio Example 1: # Set seed for reproducibility set
1 min read
How to Plot a Zoom of the Plot Inside the Same Plot Area Using ggplot2 in R
When working with data visualization, it can be useful to highlight a particular portion of a plot by zooming in on a specific region. This is often referred to as an inset plot. In R, using the ggplot2 package, you can overlay a zoomed-in section of your plot within the same plot area to provide more detailed insights while maintaining the context
4 min read
Read contents of a CSV File in R Programming - read.csv() Function
read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. Syntax: read.csv(file, header, sep, dec) Parameters: file: the path to the file containing the data to be imported into R. header: logical value. If TRUE, read.csv() assumes that your file has a header row, so row 1 is the n
3 min read
Create Scatter plot from CSV in R
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 o
2 min read
Plot Only One Variable in ggplot2 Plot in R
In this article, we will be looking at the two different methods to plot only one variable in the ggplot2 plot in the R programming language. Draw ggplot2 Plot Based On Only One Variable Using ggplot & nrow Functions In this approach to drawing a ggplot2 plot based on the only one variable, firstly the user needs to install and import the ggplo
5 min read
How to Plot a Correlation Matrix into a Graph Using R
A correlation matrix is a table showing correlation coefficients between sets of variables. It's a powerful tool for understanding relationships among variables in a dataset. Visualizing a correlation matrix as a graph can provide clearer insights into the data. This article will guide you through the steps to plot a correlation matrix using R Prog
4 min read
How to create matrix and vector from CSV file in R ?
In this article, we will discuss how to convert CSV data into a matrix and a vector in R Programming Language. We will use read.csv() function to load the csv file: Syntax: object=read.csv(path) where, path is the location of a file present in our local system. Matrix: Matrix is a two-dimensional data structure that contains rows and columns. It ca
2 min read
Read CSV file and select specific rows and columns in R
In this article, we are going to see how to read CSV file and select specific rows and columns in R Programming Language. CSV file: To import a CSV file into the R environment we need to use a pre-defined function called read.csv(). Pass filename.csv as a parameter within quotations. First, we need to set the path to where the CSV file is located u
1 min read
How to calculate mean of a CSV file in R?
Mean or average is a method to study central tendency of any given numeric data. It can be found using the formula. [Tex]Mean= (sum of data)/(frequency of data)[/Tex] In this article, we will be discussing two different ways to calculate the mean of a CSV file in R. Data in use: Method 1: Using mean function In this method to calculate the mean of
2 min read
Reading the CSV file into Dataframes in R
In this article, we will learn how to import or read a CSV file into a dataframe in R Programming Language. Data set in use: Step 1: Set or change the working directory In order to import or read the given CSV file into our data frame, we first need to check our current working directory, and make sure that the CSV file is in the same directory as
3 min read
Export CSV File without Row Names in R
In this article, we will learn how to export CSV files without row names in R Programming Language. In R language we use write.csv() function to create a CSV file from the data. Syntax: write.csv(df, path) Parameters: df: dataframe objectpath: local path on your system where .csv file will be written/saved. For this, we have to create a dataframe w
1 min read
Export List to CSV or text File in R
In this article, we will discuss how to export a list to CSV and text file using R Programming language. Exporting to both of these types of files can be done using capture.output() function. This function is mainly used to evaluate its arguments with the output being returned as a character string or sent to a file. By just changing the file name
2 min read
How to Import a CSV File into R ?
A CSV file is used to store contents in a tabular-like format, which is organized in the form of rows and columns. The column values in each row are separated by a delimiter string. The CSV files can be loaded into the working space and worked using both in-built methods and external package imports. Method 1: Using read.csv() method The read.csv()
3 min read
How to Convert a CSV File to Microsoft Excel in R
CSV refers to Comma-Separated Values. It holds plain text as a series of values (cells) separated by commas (, ) in a series of lines (rows). CSV file can actually open in a text editor and read it. On the other hand, Excel is used to display the data in horizontal and vertical rows. The data are usually stored in the cells. We have an option of fo
2 min read
Add header to file created by write.csv in R
In R programming, the write.csv() function is used to save data frames as comma-separated values (CSV) files. When a CSV file is created using the write.csv() function, it does not contain a header by default. However, it is often useful to include a header row in the CSV file that contains the names of the variables in the data frame. In this arti
3 min read
How to Get Multiple Years Y-Axis Data from a Single File on the Same Plot using R
In time-series data analysis, it is common to work with data that spans multiple years. Displaying data from multiple years on the same plot can provide valuable insights by allowing us to visually compare trends over time. In this article, we will walk through the process of plotting data from multiple years on the same y-axis using R's ggplot2 pa
4 min read
Plot multiple boxplots in one graph in R
In this article, we will learn how to plot multiple boxplot in one graph in R Programming Language. This can be accomplished by using boxplot() function, and we can also pass in a list, data frame or multiple vectors to it. For this purpose, we need to put name of data into boxplot() function as input. Syntax: boxplot(x, data, notch, varwidth, name
2 min read
Plot Arrows Between Points in a Graph in R Programming - arrows() Function
arrows() function in R Language is used to create arrows between the points on the graph specified. Syntax: arrows(x0, y0, x1, y1, length) Parameters: x0: represents x-coordinate of point from which to draw the arrow y0: represents y-coordinate of point from which to draw the arrow x1: represents x-coordinate of point to which the arrow is drawn y1
2 min read
Plot labels at end of ggplot line graph in R
In this article, we will be looking at the approach to plot labels at the end of the ggplot2 line plot in the R programming language. In this approach to plot the labels at the end of the ggplot2 line, the user needs to install and import the ggplot2 and ggrepel package on the R working console, here the ggplot2 package will be used to plot the sim
3 min read
Plot Several Curve Segments on the Same Graph
R is a programming language and software environment for statistical computing and graphics. It is widely used among statisticians and data scientists for developing statistical software and data analysis. R Programming Language is known for its powerful and flexible data visualization capabilities. The base installation of R includes several libra
5 min read
Export Plot to EPS File in R
An EPS file is a graphics file saved in the Encapsulated PostScript (EPS) file format. The EPS files may contain images, graphics, or even text. In this article, we will discuss how we export a plot to an EPS file using R programming language. Method 1 : Using setEPS() method The postscripts can be defined using the setEPS() method in R. postscript
2 min read
Saving grid.arrange() Plot to File in R
Combining multiple plots into one figure is a common task in data visualization, particularly when comparing or summarizing different aspects of data. grid. arrange() from the gridExtra package in R is an effective tool for this purpose, but saving the resulting arrangement to a file can be tricky for those unfamiliar with the right methods. Prereq
3 min read
Append row to CSV using R
In this article, we will see how to append rows to a CSV file using R Programming Language. By default, the write.csv() function overwrites entire file content. In order to append the data to a CSV File, use the write.table() method instead and set the parameter, append = TRUE. The write.table method prints its required argument x, upon conversion
2 min read
Merge multiple CSV files using R
In this article, we will be looking at the approach to merge multiple CSV files in the R programming language. In this approach to merge multiple CSV files, the user needs to install and import three different packages namely- dplyr,plyr, and readr in the R programming language console to call the functions which are list.files(), lapply(), and bin
2 min read
How to Export DataFrame to CSV in R ?
R Programming language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to Export DataFrame to CSV file in R Programming Language. Approach: Write Data in column wise formatCreate DataFrame for these dataWrite Data to the CSV filePrint the success message Function used: data
1 min read
Writing to CSV files in R
For Data Analysis sometimes creating CSV data file is required and do some operations on it as per our requirement. So, In this article we are going to learn that how to write data to CSV File using R Programming Language. To write to csv file write.csv() function is used. Syntax: write.csv(data, path) Parameter: data: data to be added to csvpath:
1 min read
How to write to CSV in R without index ?
We know that when we write some data from DataFrame to CSV file then a column is automatically created for indexing. We can remove it by some modifications. So, in this article, we are going to see how to write CSV in R without index. To write to csv file write.csv() is used. Syntax: write.csv(data,path) Lets first see how indices appear when data
2 min read
How to convert CSV into array in R?
In this article, we are going to see how to convert CSV into an array in R Programming Language. If we load this file into any environment like python, R language .etc, the data will be displayed in rows and columns format, let us convert CSV file into an array. Each and every column in the CSV will be converted into array of multiple dimensions. M
2 min read
How to append a whole dataframe to a CSV in R ?
A data frame in R programming language is a tabular arrangement of rows and columns arranged in the form of a table. A CSV file also contains data stored together to form rows stacked together. Content can be read from and written to the CSV file. Base R contains multiple methods to work with these files. The write.csv() method overwrites the entir
3 min read
three90RightbarBannerImg