Create Violin Plot for Categories with Grey Color Palette in R



To create violin plot for categories with grey color palette in reverse order using ggplot2, we can follow the below steps −

  • First of all, create a data frame.
  • Then, create the violin plot for categories with color of violins in grey palette.
  • Create the violin plot for categories with color of violins with grey palette in reverse order.

Create the data frame

Let's create a data frame as shown below −

 Live Demo

Group<-sample(c("First","Second","Third"),25,replace=TRUE)
Score<-rpois(25,10)
df<-data.frame(Group,Score)
df

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

  Group Score
1 Second 10
2 Second 8
3 Second 7
4 Third  11
5 First  3
6 Second 13
7 Third  15
8 Third  7
9 Second 10
10 Second 8
11 Third 13
12 Third 10
13 First 8
14 Third 8
15 Second 14
16 First  9
17 Third  13
18 Second 10
19 First  9
20 Second 8
21 First  11
22 First  8
23 Third  5
24 Third 12
25 Third 11

Create violin plot with violins in grey color palette

Use scale_fill_grey to create the violin plot with violins in grey color palette −

Group<-sample(c("First","Second","Third"),25,replace=TRUE)
Score<-rpois(25,10)
df<-data.frame(Group,Score)
library(ggplot2)
ggplot(df,aes(Group,Score,fill=Group))+geom_violin()+scale_fill_grey()

Output

Create violin plot with violins in grey color palette in reverse order

Use scale_fill_grey to create the violin plot with violins in reverse order with grey color palette(the default value for start and end are start=0.2 and end=0.8) −

Group<-sample(c("First","Second","Third"),25,replace=TRUE)
Score<-rpois(25,10)
df<-data.frame(Group,Score)
library(ggplot2)
ggplot(df,aes(Group,Score,fill=Group))+geom_violin()+scale_fill_grey(start=0.8,end=0.
2)

Output

Updated on: 2021-08-13T14:30:20+05:30

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements