How To Change Numbers In Plot R
In this article, nosotros are going to run into how to modify the centrality labels, legend, and plot labels using ggplot2 bar plot in R programming language.
For creating a simple bar plot we will utilise the function geom_bar( ).
Syntax: geom_bar(stat, fill up, colour, width)
Parameters :
stat : Set the stat parameter to place the mode.
fill : Represents color within the confined.
colour : Represents colour of outlines of the bars.
width : Represents width of the bars.
Dataset in utilise:
Let the states first visualize the graph equally it is so that the changes are apparent.
Example:
R
ODI <-
data.frame
(match=
c
(
"M-i"
,
"M-2"
,
"K-3"
,
"M-4"
),
runs=
c
(67,37,74,x))
caput
(ODI)
library
(ggplot2)
perf <-
ggplot
(data=ODI,
aes
(x=friction match, y=runs,fill=match))+
geom_bar
(stat=
"identity"
)
perf
Output:
Adding axis labels and main title in the plot
By default, R will apply the variables provided in the Information Frame every bit the labels of the axis. We tin modify them and change their advent hands. The functions which are used to change axis labels are :
- xlab( ) : For the horizontal axis.
- ylab( ) : For the vertical axis.
- labs( ) : For both the axes simultaneously.
- element_text( ) : The arguments of this function are :
Syntax:
element_text( family, face, colour, size, hjust, vjust, angle, margin)
- element_blank( ): To make the labels NULL and remove them from the plot.
The argument hjust (Horizontal Adjust) or vjust (Vertical Adjust) is used to move the centrality labels. They take numbers in range [0,i] where :
hjust = 0 // Depicts left most corner of the centrality
hjust = 0.5 // Depicts middle of the axis
hjust = i // Depicts correct near corner of the axis
The keywords used are :
- title : To add plot label.
- subtitle : To add together subtitle in the plot.
- caption : To add caption in the plot.
- axis.championship.x : For horizontal axis.
- axis.championship.y : For vertical centrality.
Example:
R
library
(ggplot2)
ODI <-
data.frame
(match=
c
(
"Thousand-1"
,
"Thou-two"
,
"Grand-3"
,
"M-4"
),
runs=
c
(67,37,74,ten))
perf <-
ggplot
(data=ODI,
aes
(x=match, y=runs,make full=friction match))+
geom_bar
(stat=
"identity"
)
perf
ggp<-perf+
labs
(x=
"Matches"
,y=
"Runs Scored"
,
title=
"Runs scored by Virat Kohli in ODI matches"
)
ggp
Output:
A subtitle can also be included with the main title using labs() office and passing subtitle argument with the required subtitle.
Example:
R
library
(ggplot2)
ODI <-
data.frame
(match=
c
(
"M-1"
,
"M-ii"
,
"M-3"
,
"M-4"
),
runs=
c
(67,37,74,10))
perf <-
ggplot
(data=ODI,
aes
(x=match, y=runs,fill=match))+
geom_bar
(stat=
"identity"
)
perf
ggp<-perf+
labs
(10=
"Matches"
,y=
"Runs Scored"
,
title=
"Runs scored by Virat Kohli in ODI matches"
)
ggp
ggp+
labs
(subtitle=
"Functioning"
,explanation=
"GeeksforGeeks Bays"
)
Output:
To move axis labels hjust argument is set according to the requirement.
Case:
R
library
(ggplot2)
ODI <-
data.frame
(match=
c
(
"Chiliad-i"
,
"Chiliad-two"
,
"Yard-three"
,
"G-4"
),
runs=
c
(67,37,74,x))
perf <-
ggplot
(data=ODI,
aes
(x=match, y=runs,fill=match))+
geom_bar
(stat=
"identity"
)
perf
ggp<-perf+
labs
(x=
"Matches"
,y=
"Runs Scored"
,
title=
"Runs scored past Virat Kohli in ODI matches"
)
ggp
ggp +
theme
(
centrality.title.x =
element_text
(hjust=0),
axis.title.y =
element_text
(hjust=0)
)
ggp +
theme
(
axis.title.x =
element_text
(hjust=0.five),
centrality.championship.y =
element_text
(hjust=0.5)
)
ggp +
theme
(
centrality.championship.ten =
element_text
(hjust=1),
centrality.title.y =
element_text
(hjust=i)
)
Output:
Formatting appearance of axis labels and main title of the plot
Axis labels and main titles can exist changed to reflect the desired appearance. For this element_text() office is passed with the required attributes.
Example:
R
library
(ggplot2)
ODI <-
data.frame
(lucifer=
c
(
"M-one"
,
"One thousand-2"
,
"1000-3"
,
"Grand-four"
),
runs=
c
(67,37,74,x))
perf <-
ggplot
(data=ODI,
aes
(10=match, y=runs,make full=match))+
geom_bar
(stat=
"identity"
)
perf
ggp<-perf+
labs
(ten=
"Matches"
,y=
"Runs Scored"
,
championship=
"Runs scored by Virat Kohli in ODI matches"
)
ggp +
theme
(plot.title =
element_text
(
color=
"#006000"
, size=14,face=
"bold"
),
axis.title.x =
element_text
(
colour=
"Purple"
,size=10,face up=
"assuming.italic"
),
centrality.title.y =
element_text
(
colour=
"DarkBlue"
,size=10,face=
"assuming.italic"
)
)
Output:
Removing the centrality labels and plot the title
For this theme() office is called with reference to which part of the plot has to exist modified. To these references, pass element_blank() without any argument.
Example:
R
library
(ggplot2)
ODI <-
information.frame
(match=
c
(
"M-ane"
,
"Thou-2"
,
"M-iii"
,
"Thou-iv"
),
runs=
c
(67,37,74,x))
perf <-
ggplot
(data=ODI,
aes
(10=match, y=runs,fill=match))+
geom_bar
(stat=
"identity"
)
perf
ggp<-perf+
labs
(x=
"Matches"
,y=
"Runs Scored"
,
title=
"Runs scored by Virat Kohli in ODI matches"
)
ggp +
theme
(plot.title =
element_blank
(),
axis.title.ten =
element_blank
(),
centrality.title.y =
element_blank
()
)
Output:
Changing the Position of the legends
To change the position of the fable theme() role is called with legend.position as argument and to this argument, a required position is passed.
Syntax:
theme( legend.position = "Pos")
Parameter:
Pos : Left, Right, Peak, Lesser.
Example:
R
library
(ggplot2)
ODI <-
data.frame
(match=
c
(
"M-one"
,
"G-2"
,
"G-iii"
,
"M-iv"
),
runs=
c
(67,37,74,x))
perf <-
ggplot
(data=ODI,
aes
(x=match, y=runs,make full=match))+
geom_bar
(stat=
"identity"
)
perf
ggp<-perf+
labs
(10=
"Matches"
,y=
"Runs Scored"
,
title=
"Runs scored by Virat Kohli in ODI matches"
)
ggp +
theme
(legend.position=
"peak"
)
Output:
Case:
R
library
(ggplot2)
ODI <-
data.frame
(lucifer=
c
(
"Thou-1"
,
"M-2"
,
"One thousand-3"
,
"K-4"
),
runs=
c
(67,37,74,10))
perf <-
ggplot
(data=ODI,
aes
(x=friction match, y=runs,fill=lucifer))+
geom_bar
(stat=
"identity"
)
perf
ggp<-perf+
labs
(x=
"Matches"
,y=
"Runs Scored"
,
title=
"Runs scored by Virat Kohli in ODI matches"
)
ggp +
theme
(legend.position=
"bottom"
)
Output:
Source: https://www.geeksforgeeks.org/modify-axis-legend-and-plot-labels-using-ggplot2-in-r/
Posted by: parkermorelesucity.blogspot.com
0 Response to "How To Change Numbers In Plot R"
Post a Comment