banner



How To Color In Bar Graph R

In this article, we are going to encounter various methods to change the colour of a bar chart using ggplot2 in the R programming linguistic communication.

For creating a simple bar plot we will employ the office geom_bar( ).

Syntax:

geom_bar(stat, fill, colour, width)

Parameters :

  • stat : Prepare the stat parameter to identify the fashion.
  • fill : Represents colour inside the bars.
  • color : Represents color of outlines of the confined.
  • width : Represents width of the bars.

Dataset in Use:

Allow us start plot a regular barplot to display what a regular plot without any help volition be printed.

Example:

R

library (ggplot2)

ODI <- data.frame (match= c ( "Yard-1" , "Grand-2" , "M-iii" , "M-iv" ),

runs= c (67,37,74,10))

perf <- ggplot (data=ODI, aes (ten=lucifer, y=runs))+

geom_bar (stat= "identity" )

perf

perf+ coord_flip ()

Output:

Bar Plot

Now let us see how colors are added to the barplot.

Method 1: Using default colors

  • Different fill colour

Use the command fill to add colour inside the confined. Since, the bars are in different x-axis values we need to assign the ten-centrality variable to the make full. In our case, match is in the x-axis, so we write fill=match. You need to write this command inside the aes() besides known every bit the quoting role or aesthetic function under the ggplot2 library.

Example:

R

library (ggplot2)

ODI <- information.frame (friction match= c ( "M-1" , "One thousand-2" , "M-3" , "One thousand-iv" ),

runs= c (67,37,74,ten))

perf <- ggplot (data=ODI, aes (ten=match, y=runs,fill=friction match))+

geom_bar (stat= "identity" )

perf

Output:

  • Same fill colour

Apply the same make full. But this time since nosotros have to assign a single color nosotros volition use it inside the geom_bar( ). Then assign the color of your choice to the fill every bit shown below. The theme is changed because some color plots might not be visible to the default theme in R.

Example:

R

library (ggplot2)

ODI <- data.frame (match= c ( "M-i" , "Grand-2" , "M-3" , "M-iv" ),

runs= c (67,37,74,10))

perf <- ggplot (data=ODI, aes (x=lucifer, y=runs))+

geom_bar (stat= "identity" ,fill= "lightblue" )+

theme_dark ()

perf

Output:

  • Same outline

Here we demand a control color for assigning colors to the outline. Outline colors look practiced when the fill color is white. It is the property of colour considering some colors overshadow other colors or if we assign same colour to fill and outline both. Since we need the same fill and the same outline we will write these commands inside the geom_bar( ).

Example:

R

library (ggplot2)

ODI <- data.frame (match= c ( "1000-1" , "M-2" , "K-3" , "Chiliad-four" ),

runs= c (67,37,74,10))

perf <- ggplot (data=ODI, aes (10=match, y=runs))+

geom_bar (stat= "identity" ,color= "cherry" ,fill= "white" )+

theme_classic ()

perf

Output:

  • Different Outline

Here we need to provide unlike outlines. The fill will be the aforementioned which is white. The fill will be inside the geom_bar( ). The color will be inside aes( ) nether ggplot( ) as information technology is variable in this instance.

Example:

R

library (ggplot2)

ODI <- data.frame (match= c ( "M-i" , "G-two" , "M-3" , "M-iv" ),

runs= c (67,37,74,10))

perf <- ggplot (data=ODI, aes (10=lucifer, y=runs,colour=match))+

geom_bar (stat= "identity" ,fill= "white" )+

theme_classic ()

perf

Output:

Method 2: Setting color manually

  • scale_fill_manual( )

This function is used to provide custom colors. We can either write the color code as "#XXXXXX" or we tin can direct write the color name equally "color_name". The make full will be inside the ggplot2 every bit we need to manually assign different colors to the bars. Invoke the office and manually insert a vector which consists of the color code or the color proper name. The length of the vector should be aforementioned as the number of bars.

Example:

R

library (ggplot2)

ODI <- information.frame (friction match= c ( "Chiliad-1" , "Thou-ii" , "Yard-three" , "M-4" ),

runs= c (67,37,74,ten))

perf <- ggplot (data=ODI, aes (x=match, y=runs,fill=match))+

geom_bar (stat= "identity" )+

scale_fill_manual (values= c ( "#9933FF" ,

"#33FFFF" ,

"red" ,

"darkblue" ))

perf

Output:

Similarly, we can set outlines color manually. Simply replace the keyword fill everywhere to color in the above code. The role volition now go : scale_color_manual( )

  • Gray scale

The office used here is scale_fill_grey( ). The fill is inside the aes( ) as nosotros need different gray calibration colors. Invoke the role scale_fill_grey( ) at last.

Example:

R

library (ggplot2)

ODI <- data.frame (match= c ( "M-ane" , "M-2" , "K-3" , "M-iv" ),

runs= c (67,37,74,x))

perf <- ggplot (data=ODI, aes (x=match, y=runs,fill=match))+

geom_bar (stat= "identity" )+

scale_fill_grey ()

perf

Output:


How To Color In Bar Graph R,

Source: https://www.geeksforgeeks.org/change-color-of-bars-in-barchart-using-ggplot2-in-r/

Posted by: brownpromarls1989.blogspot.com

0 Response to "How To Color In Bar Graph R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel