如何在R中使用ggplot2创建使用反向灰色调色板的类别小提琴图?
要使用ggplot2创建使用反向灰色调色板的类别小提琴图,我们可以按照以下步骤操作:
- 首先,创建一个数据框。
- 然后,创建使用灰色调色板的小提琴图。
- 创建使用反向灰色调色板的小提琴图。
创建数据框
让我们创建一个如下所示的数据框:
Group<-sample(c("First","Second","Third"),25,replace=TRUE) Score<-rpois(25,10) df<-data.frame(Group,Score) df
执行上述脚本后,将生成以下输出(由于随机化,此输出会在您的系统上有所不同):
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
创建使用灰色调色板的小提琴图
使用scale_fill_grey创建使用灰色调色板的小提琴图:
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()
输出
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
创建使用反向灰色调色板的小提琴图
使用scale_fill_grey创建使用反向灰色调色板的小提琴图(start和end的默认值分别为start=0.2和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)
输出
广告