How To Loop Through A Column In A Data Frame In R
In this article, nosotros will larn how to plot all columns of the DataFrame in R programming language.
Dataset in use:
x y1 y2 y3 one one 0.08475635 0.4543649 0 2 2 0.22646034 0.6492529 1 three 3 0.43255650 0.1537271 0 4 iv 0.55806524 0.6492887 3 v 5 0.05975527 0.3832137 one vi 6 0.08475635 0.4543649 0 7 seven 0.22646034 0.6492529 1 viii 8 0.43255650 0.1537271 0 ix 9 0.55806524 0.6492887 3 10 10 0.05975527 0.3832137 ane
Method i: using plot.ts() office
We merely have to pass our dataframe in the plot.ts() function, and it will plot all the dataframe columns in a time serial plot. In the Y-axis we can come across the names of the columns of our dataframe.
Syntax: plot.ts(df)
Parameters:
df: DataFrame object
Example:
R
ready.seed
(200)
df <-
data.frame
(x = 1:10,
y1 =
rnorm
(5),
y2 =
runif
(v),
y3 =
rpois
(5, 1))
plot.ts
(df)
Output:
Method two: using zoo() role
The zoo() function is nowadays in the zoo package, so it has to be imported by placing require(zoo) function on the get-go line of the lawmaking. This function converts the dataframe in such a format("zoo" series) that information technology can be plotted easily in a class of a fourth dimension serial plot.
Since y is NULL in our case, a time serial plot of x is produced. But if both x and y were a uni-variate "zoo" series then a scatter plot of y vs x would've been produced.
Syntax: plot(zoo(df))
Parameters:
df: DataFrame object
Example:
R
crave
(zoo)
set.seed
(200)
df <-
data.frame
(ten = ane:10,
y1 =
rnorm
(five),
y2 =
runif
(5),
y3 =
rpois
(5, i))
df <-
zoo
(df)
plot
(df)
Output:
Method iii: using plot() role
In this approach, we will plot a line chart showing multiple columns of a data frame using the bones plot() function. The plot() function is divers as a generic function for plotting in R Language. It can be used to create basic plots of a different type.
Syntax: plot(Ten, Y, type = "50", col = 1, ylim = c(0, iii))
Parameters:
X: X-axis. ->
Y: Y-axis ->
type: specifies the type of plot
col: specifies color
ylim: specifies limit for y-centrality
In the code below first dataframe column is Ten-axis, and the residue columns are y-axis, and they are plotted confronting the get-go cavalcade in course of a line nautical chart. The value of col should be dissimilar for different lines such that different line has a different color, and finally we have specified ylim which limits the value of y-centrality.
We can also plot different types of plots by specifying different values for type parameters. For example, type="h", would take plotted a histogram.
Example:
R
crave
(zoo)
gear up.seed
(200)
df <-
data.frame
(ten = 1:x,
y1 =
rnorm
(5),
y2 =
runif
(five),
y3 =
rpois
(5, 1))
plot
(df$x, df$y1, blazon =
"o"
, col = ane, ylim =
c
(0, 3))
lines
(df$x, df$y2, type =
"o"
, col = 2)
lines
(df$x, df$y3, type =
"o"
, col = 3)
Output:
Method 4: Using ggplo2() bundle
For this the data has to be reshaped to exist in the course where it tin be plotted.
Example:
R
library
(ggplot2)
set.seed
(200)
df <-
data.frame
(x = 1:x,
y1 =
rnorm
(5),
y2 =
runif
(v),
y3 =
rpois
(v, 1))
df_reshaped <-
information.frame
(x = df$x,
y =
c
(df$y1, df$y2, df$y3),
group =
c
(
rep
(
"y1"
,
nrow
(df)),
rep
(
"y2"
,
nrow
(df)),
rep
(
"y3"
,
nrow
(df))))
caput
(df_reshaped,x)
Output:
x y group one one 0.08475635 y1 2 ii 0.22646034 y1 iii 3 0.43255650 y1 4 4 0.55806524 y1 5 5 0.05975527 y1 6 half dozen 0.08475635 y1 7 7 0.22646034 y1 eight viii 0.43255650 y1 9 ix 0.55806524 y1 10 10 0.05975527 y1
Plotting multiple variables in the same panel
In this method, nosotros plot a line graph for each column of the dataframe in the aforementioned panel. This can be useful when comparing columns that store aforementioned type of data but differ in some nature. Afterward reshaping the data, let u.s.a. come across how line plots of the diverse column will appear.
Syntax:
ggplot(df_reshaped, aes(10, y, col = group)) + geom_line()
Instance:
R
library
(ggplot2)
set.seed
(200)
df <-
information.frame
(x = 1:ten,
y1 =
rnorm
(5),
y2 =
runif
(v),
y3 =
rpois
(5, 1))
df_reshaped <-
data.frame
(x = df$x,
y =
c
(df$y1, df$y2, df$y3),
grouping =
c
(
rep
(
"y1"
,
nrow
(df)),
rep
(
"y2"
,
nrow
(df)),
rep
(
"y3"
,
nrow
(df))))
ggplot
(df_reshaped,
aes
(x, y, col = group)) +
geom_line
()
Output:
Plotting multiple variables in the dissimilar panels
In this method, nosotros plot a line graph for each column of the dataframe in the dissimilar panel of a same plot. We can achieve this job by calculation facet_grid() office. facet_grid() function produces a layout panel defined past rows and columns.
Syntax:
ggplot(df_reshaped, aes(x, y, col = group)) + geom_line()+ facet_grid(grouping ~ .)
Example:
R
library
(ggplot2)
set.seed
(200)
df <-
data.frame
(x = 1:10,
y1 =
rnorm
(5),
y2 =
runif
(5),
y3 =
rpois
(five, 1))
df_reshaped <-
information.frame
(x = df$x,
y =
c
(df$y1, df$y2, df$y3),
grouping =
c
(
rep
(
"y1"
,
nrow
(df)),
rep
(
"y2"
,
nrow
(df)),
rep
(
"y3"
,
nrow
(df))))
ggplot
(df_reshaped,
aes
(x, y, col = grouping)) +
geom_line
()+
facet_grid
(group ~ .)
Output:
How To Loop Through A Column In A Data Frame In R,
Source: https://www.geeksforgeeks.org/how-to-plot-all-the-columns-of-a-dataframe-in-r/
Posted by: haynespeaske.blogspot.com
0 Response to "How To Loop Through A Column In A Data Frame In R"
Post a Comment