How To Change The Legend Names On Geom_line Ggplot2 R
In a line graph, we have the horizontal axis value through which the line will be ordered and connected using the vertical axis values. Nosotros are going to use the R package ggplot2 which has several layers in it.
First, y'all need to install the ggplot2 bundle if it is not previously installed in R Studio.
Function Used:
- geom_line connects them in the guild of the variable on the horizontal (x) axis.
Syntax:
geom_line(mapping=NULL, information=Nothing, stat="identity", position="identity",…)
- geom_path connects the ascertainment in the same order as in data
Syntax:
geom_path(mapping=NULL, data=Nil, stat="identity", position="identity",…)
Single Line Plot
In this department, we will be dealing with a single line chart and will likewise discuss various attributes that help its appearance.
Data set in Use:
R
val <-
data.frame
(form=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,80,60))
caput
(val)
Output:
Basic Line Plot
For a unproblematic line chart data is roughly passed to the function with some required attributes.
Example:
R
library
(ggplot2)
val <-
data.frame
(form=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,80,60))
ggplot
(data=val,
aes
(x=course, y=num, grouping=1)) +
geom_line
()+
geom_point
()
Output:
Formating Line
- Line Type
For this, the control linetype is used. ggplot2 provides various line types. For instance : dotted, ii dash, dashed, etc. This attribute is passed with a required value.
Example:
R
library
(ggplot2)
val <-
data.frame
(course=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,fourscore,sixty))
ggplot
(data=val,
aes
(x=grade, y=num, group=i)) +
geom_line
(linetype =
"dotted"
)+
geom_point
()
Output:
- Line Color
The control color is used and the desired color is written in double quotes [" "] inside geom_line( ).
Example:
R
library
(ggplot2)
val <-
data.frame
(grade=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,80,threescore))
ggplot
(data=val,
aes
(x=course, y=num, grouping=1)) +
geom_line
(color=
"light-green"
)+
geom_point
()
Output:
- Line Size
The line size tin be inverse using the control size and providing the value of the size inside geom_line( ).
Case:
R
library
(ggplot2)
val <-
data.frame
(course=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,fourscore,threescore))
ggplot
(data=val,
aes
(x=course, y=num, group=ane)) +
geom_line
(color=
"green"
,size=1.v)+
geom_point
()
Output:
Calculation Chart Title, Axis Title
ggtitle() with the appropriate title can exist used to add together chart title and labs again with appropriate input tin be used to add axes title.
Instance:
R
library
(ggplot2)
val <-
information.frame
(course=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,80,60))
line<-
ggplot
(information=val,
aes
(10=class, y=num, group=1)) +
geom_line
(color=
"greenish"
,size=ane.5)+
geom_point
()
line+
ggtitle
(
"Courses vs Students Enrolled in GeeksforGeeks"
)+
labs
(x=
"Courses"
,y=
"Number of Students"
)
Output:
Changing the Theme
Use theme_theme_name() to add the theme. There are a lot of themes bachelor in R library. For instance: dark, classic, etc. Values tin can be provided as desired.
Example:
R
library
(ggplot2)
val <-
information.frame
(grade=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,80,lx))
line<-
ggplot
(data=val,
aes
(x=class, y=num, group=1)) +
geom_line
(colour=
"green"
,size=ane.5)+
geom_point
()
line+
ggtitle
(
"Courses vs Students Enrolled in GeeksforGeeks"
)+
labs
(x=
"Courses"
,y=
"Number of Students"
)+
theme_dark
()
Output:
Adding arrow
To add an arrow in line use the grid library is used. Then to add together arrows apply the pointer( ) to add an arrow. It is besides possible to change the parameters in an arrow like angle, type, ends.
Example:
R
library
(ggplot2)
library
(filigree)
val <-
data.frame
(class=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,80,threescore))
ggplot
(data=val,
aes
(x=course, y=num, group=1)) +
geom_line
(pointer=
arrow
())+
geom_point
()
arr=
arrow
(bending = xx, ends =
"both"
, type =
"closed"
)
ggplot
(data=val,
aes
(x=course, y=num, group=i)) +
geom_line
(arrow=arr)+
geom_point
()
Output:
Calculation Data labels
Utilize characterization to become the values in y-axis and nudge_y to place the data label.
Example:
R
library
(ggplot2)
val <-
data.frame
(form=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,eighty,threescore))
ggplot
(data=val,
aes
(x=form, y=num, group=1, label=num)) +
geom_line
()+
geom_point
()+
geom_text
(nudge_y = 2)
Output:
Scaling axis :
Use xlim( ) to change the x-axis scale and ylim( ) to change the y-axis scale and pass appropriate values to these.
Syntax:
xlim(min,max)
ylim(min,max)
Example:
R
library
(ggplot2)
val <-
data.frame
(grade=
c
(
'DSA'
,
'C++'
,
'R'
,
'Python'
),
num=
c
(77,55,fourscore,lx))
ln <-
ggplot
(data=val,
aes
(x=course, y=num, grouping=1)) +
geom_line
(color=
"greenish"
,size=2)+
geom_point
()
ln+
ylim
(0,100)+
theme_dark
()
Output:
Plotting Multiple lines
For plotting multiple plots into one, nothing changes except that grouping aspect has to set to the name of the cavalcade on the basis of which dissimilar lines will be drawn.
Example:
R
library
(ggplot2)
vacc <-
information.frame
(blazon=
rep
(
c
(
"Covishield"
,
"Covaxin"
), each=ii),
dose=
rep
(
c
(
"D1"
,
"D2"
),2),
slots=
c
(33, 45, 66, 50))
ggplot
(data=vacc,
aes
(x=dose, y=slots, grouping=type)) +
geom_line
(linetype=
"longdash"
, colour=
"green"
, size=ane.v)+
geom_point
(color=
"red"
, size=v)+
theme_dark
()
Output:
You can as well add title, axes championship, data labels in the to a higher place line plot as discussed in the previous section.
Formatting the plot :
- Using separate line types based on groups
To differentiate the lines past changing the type of line provide the line blazon in geom_line() and shape for the legend in geom_point().
Case:
R
library
(ggplot)
vacc <-
data.frame
(type=
rep
(
c
(
"Covishield"
,
"Covaxin"
), each=2),
dose=
rep
(
c
(
"D1"
,
"D2"
),2),
slots=
c
(33, 45, 66, l))
ggplot
(vacc,
aes
(ten=dose, y=slots, group=type)) +
geom_line
(
aes
(linetype=type))+
geom_point
()+
theme_classic
()
ggplot
(vacc,
aes
(ten=dose, y=slots, group=type)) +
geom_line
(
aes
(linetype=blazon))+
geom_point
(
aes
(shape=blazon))+
theme_classic
()
Output:
- Assigning different line colors on the basis of groups
The following lawmaking automatically controls color using the level of the variable "type". It will assign separate colors to each line.
Case:
R
library
(ggplot2)
vacc <-
information.frame
(type=
rep
(
c
(
"Covishield"
,
"Covaxin"
), each=two),
dose=
rep
(
c
(
"D1"
,
"D2"
),ii),
slots=
c
(33, 45, 66, l))
ln <-
ggplot
(vacc,
aes
(10=dose, y=slots, group=type)) +
geom_line
(
aes
(color=type))+
geom_point
(
aes
(colour=blazon))+
theme_classic
()
ln
Output:
To enter color manually you can utilize :
- scale_color_brewer( ) : It uses different color palettes from the RColorBrewer packet. It has various color palettes.
- scale_color_manual( ) : It is used to manually add together detached colors.
Example:
R
library
(ggplot2)
vacc <-
information.frame
(type=
rep
(
c
(
"Covishield"
,
"Covaxin"
), each=two),
dose=
rep
(
c
(
"D1"
,
"D2"
),2),
slots=
c
(33, 45, 66, l))
ln <-
ggplot
(vacc,
aes
(x=dose, y=slots, group=type)) +
geom_line
(
aes
(color=type))+
geom_point
(
aes
(colour=blazon))+
theme_classic
()
ln+
scale_color_brewer
(palette=
"Set2"
)
ln+
scale_color_manual
(values=
c
(
"light-green"
,
"blue"
))
Output:
- Irresolute the position of legends
For changing the legend position legen.position attribute of the theme function is passed with the required value.
Syntax:
theme(legend.position="pos")
pos It can be superlative, right, lesser, left or none
Instance:
R
library
(ggplot2)
vacc <-
data.frame
(type=
rep
(
c
(
"Covishield"
,
"Covaxin"
), each=2),
dose=
rep
(
c
(
"D1"
,
"D2"
),ii),
slots=
c
(33, 45, 66, 50))
ln <-
ggplot
(vacc,
aes
(10=dose, y=slots, grouping=type)) +
geom_line
(
aes
(colour=type))+
geom_point
(
aes
(color=type))+
theme_classic
()
ln <- ln +
scale_color_brewer
(palette=
"Dark2"
)+
theme_classic
()
ln +
theme
(fable.position=
"top"
)
ln +
theme
(legend.position=
"left"
)
ln +
theme
(legend.position=
"none"
)
Output:
Source: https://www.geeksforgeeks.org/line-plot-using-ggplot2-in-r/
Posted by: duartetoneady.blogspot.com
0 Response to "How To Change The Legend Names On Geom_line Ggplot2 R"
Post a Comment