dxy logo
首页丁香园病例库全部版块
搜索
登录

2D图展示3D数据:基于ggplot2

发布于 2021-03-08 · 浏览 1238 · IP 江苏江苏
这个帖子发布于 4 年零 55 天前,其中的信息可能已发生改变或有所发展。
icon推荐

*** 用电脑看效果会比较好(或下载附件查看),手机APP显示代码行很不友好***

在2020年5月,我曾写过一个有关eoffice程序包的主贴,里面提到了一个分面堆积柱状图,可以在2维坐标系里展示3个维度的数据。当时由于配合使用了ggthemes程序包(图片主题),一时间找不到修改填充色的方法,只能通过eoffice包导出后在ppt中修改。今天又回头看了一下当初的帖子,觉得还是要完善一下的好。所以,今天的主贴内容就是逐步呈现出一个比较完善的分面堆积柱状图(包括分面、填充色、X轴标签顺序、图例标题和标签顺序、字体等)。

1.案例数据介绍

本案例是探究不同性别不同年龄组的某疾病不同严重程度的频数或构成比,涉及到的变量有性别(Male、Female)、年龄(18-24、25-49、50-64和65-88)、疾病严重程度(Common、Severe、Critical)、频数(Frequency)和构成比(Proportion)。

## 读入数据

library(openxlsx)

dat<-read.xlsx("data.xlsx",sheet = 1)

str(dat)

## 'data.frame': 24 obs. of 5 variables:

## $ Age : chr "18-24" "18-24" "18-24" "18-24" ...

## $ Gender : chr "Female" "Female" "Female" "Male" ...

## $ Severity : chr "Common" "Severe" "Critical" "Common" ...

## $ Frequency : num 4 3 1 5 4 1 6 4 3 18 ...

## $ Proportion: num 50 38 12 50 40 10 46 31 23 55 ...

在原始数据中已经将分类变量进行了命名,如果起初是数字赋值,可以通过factor函数定义因子变量和加标签 ,例如:dat$ Severity <- factor(dat$ Severity, labels = c(“Common”, “Severe”, “Critical”))

2.绘制初级图

一般情况下,2维坐标系很难展示这么多维度的信息,所以这里使用到了ggplot2的分面功能,横轴展示性别、分面展示年龄,不同填充色展示疾病严重程度,纵轴展示构成比或频数。

## 加载程序包

library(ggplot2)

## 绘制初级图

ggplot(dat,aes(x=Gender,y=Proportion,fill=Severity))+

geom_bar(stat="identity")+xlab("")+

facet_grid(. ~ Age,labeller = label_both)

## labeller = label_both用来给分面定义标题

img

这个初级图已经把大部分信息展示出来了,能够满足一般的要求。但是,字体/字号、颜色、图例的顺序还可以进一步完善,比如字体改为Time New Roman,性别的显示顺序改为Male和Female,图例的顺序按照严重程度进行了排序(Common、Severe和Critical),

3.修改轴标题、刻度的字体和字号

在初级图的基础上修改字体和字号,strip.text.x修改分面的标题,legend.titlelegend.text修改图例的标题和标签,axis.title.y修改纵轴标题,axis.text.xaxis.text.y修改坐标轴刻度。其中,family=serif为Times New Roman,size为字号,如果想加粗可以用face=”bold“定义。

ggplot(dat,aes(x=Gender,y=Proportion,fill=Severity))+

geom_bar(stat="identity")+xlab("")+

facet_grid(. ~ Age,labeller = label_both)+

theme(strip.text.x = element_text(family = "serif",size=11))+

theme(legend.title =element_text(family="serif",size=12))+

theme(legend.text=element_text(family="serif",size=12))+

theme(axis.title.y = element_text(size = 12,family = "serif"))+

theme(axis.text.x = element_text(family = "serif",size = 11))+

theme(axis.text.y = element_text(family = "serif",size = 11))

img

4.修改横轴性别,修改图例的的显示顺序和填充色 使用scale_x_discrete修改横轴刻度的显示顺序,使用scale_fill_manual修改图例的顺序和填充的颜色。

ggplot(dat,aes(x=Gender,y=Proportion,fill=Severity))+

geom_bar(stat="identity")+xlab("")+

facet_grid(. ~ Age,labeller = label_both)+

theme(strip.text.x = element_text(family = "serif",size=11))+

theme(legend.title =element_text(family="serif",size=12))+

theme(legend.text=element_text(family="serif",size=12))+

theme(axis.title.y = element_text(size = 12,family = "serif"))+

theme(axis.text.x = element_text(family = "serif",size = 11))+

theme(axis.text.y = element_text(family = "serif",size = 11))+

scale_x_discrete(limits=c("Male","Female"))+

scale_fill_manual(labels=c("Common", "Severe", "Critical"),

values = c("darkgreen", "darkcyan", "tomato"))

img

5. 修改主题 如果想让图片看起来更高级一点,我们可以用ggthemes提供的一些主题,比如theme_wsj,theme_stata,theme_excel等。但值得注意的是,这部分内容不能加载最后面,要放在修改字体和填充色的前面,不然字体的修改就是无用功了。这里可以做个区别演示。

把主题修改的语句放在最后面

library(ggthemes)

ggplot(dat,aes(x=Gender,y=Proportion,fill=Severity))+

geom_bar(stat="identity")+xlab("")+

facet_grid(. ~ Age,labeller = label_both)+

theme(strip.text.x = element_text(family = "serif",size=11))+

theme(legend.title =element_text(family="serif",size=12))+

theme(legend.text=element_text(family="serif",size=12))+

theme(axis.title.y = element_text(size = 12,family = "serif"))+

theme(axis.text.x = element_text(family = "serif",size = 11))+

theme(axis.text.y = element_text(family = "serif",size = 11))+

scale_x_discrete(limits=c("Male","Female"))+

scale_fill_manual(labels=c("Common", "Severe", "Critical"),

values = c("darkgreen", "darkcyan", "tomato"))+

theme_wsj()

img

把主题修改语句放在前面

ggplot(dat,aes(x=Gender,y=Proportion,fill=Severity))+ geom_bar(stat="identity")+xlab("")+

facet_grid(. ~ Age,labeller = label_both)+

theme_wsj()+

theme(strip.text.x = element_text(family = "serif",size=11,face = "bold"))+

theme(legend.title =element_text(family="serif",size=12.5,face = "bold"))+

theme(legend.text=element_text(family="serif",size=12,face = "bold"))+

theme(axis.title.y = element_text(size = 12,family = "serif",face = "bold"))+

theme(axis.text.x = element_text(family = "serif",size = 11))+

theme(axis.text.y = element_text(family = "serif",size = 11))+

scale_x_discrete(limits=c("Male","Female"))+

scale_fill_manual(labels=c("Common", "Severe", "Critical"),

values = c("darkgreen", "darkcyan", "tomato"))

img

6.更换主题

我们也可以使用简单的主题,如theme_few

ggplot(dat,aes(x=Gender,y=Proportion,fill=Severity))+ geom_bar(stat="identity")+xlab("")+

facet_grid(. ~ Age,labeller = label_both)+

theme_few()+

theme(strip.text.x = element_text(family = "serif",size=11,face = "bold"))+

theme(legend.title =element_text(family="serif",size=12.5,face = "bold"))+

theme(legend.text=element_text(family="serif",size=12,face = "bold"))+

theme(axis.title.y = element_text(size = 12,family = "serif",face = "bold"))+

theme(axis.text.x = element_text(family = "serif",size = 11,face = "bold"))+

theme(axis.text.y = element_text(family = "serif",size = 11,face = "bold"))+

scale_x_discrete(limits=c("Male","Female"))+

scale_fill_manual(labels=c("Common", "Severe", "Critical"),

values = c("blue", "orange", "red"))

img

以上便是2D图展示3D数据的整个过程了,希望能给各位的数据可视化方案提供些许参考。尊重数据,助力科研,我们在路上!

2D图展示3D数据.docx (290 KB)

最后编辑于 2022-10-09 · 浏览 1238

4 7 7

全部讨论0

默认最新
avatar
4
分享帖子
share-weibo分享到微博
share-weibo分享到微信
认证
返回顶部