ggplot2调整所有字体大小_翻译字体转换器

(89) 2024-06-17 11:01:01

作者:居居

日期:2021-11-08

ggpubr是一个ggplot2的拓展包,代码量大幅度降低。主题系统简单。闲话少说,上例子,代码可以直接copy去改改,我们要致力于从复杂的绘图工作中解脱出来,专心搞科研!

还是那句话,下面的代码懂的都懂,不明白的去看看ggplot2和ggpubr。

1.准备好程辑包(似乎只有R语言社区这么称呼这东西)

library("tidyverse") library("rio") library("ggpubr") library("data.table") 

2.ggpubr预设主题(用的时候直接复制,前面的序号可别复制进去,否则出错)

2.1 NPG
2.2 AAAS
2.3 NEJM
2.4 Lancet
2.5 JAMA
2.6 JCO
2.7 UCSCGB
2.8 D3
2.9 LocusZoom
2.10 IGV
2.11 COSMIC
2.12 UChicago
2.13 Star Trek
2.14 Tron Legacy
2.15 Futurama
2.16 Rick and Morty
2.17 The Simpsons

3.主题设置和字体设置(重头戏,中英字体分设在此,这部分只需要设置一次,后面终身使用)

fontSize=9 #字号 themE="NPG" windowsFonts(st=windowsFont("宋体"), tnr=windowsFont("Times New Roman"), fs=windowsFont("仿宋"), wryh=windowsFont("微软雅黑"), dx=windowsFont("等线") ) p<- font("xylab", size = fontSize,family="dx")+ #坐标轴标题 #刻度的文字大小和字体 font("xy.text", size = fontSize,family="tnr")+ #图例文字大小和字体 font("legend.text", size = fontSize,family="tnr")+ #图例的标题的文字大小和字体 font("legend.title", size = fontSize,family="tnr")+ #图例图标大小 theme(legend.key.size = unit(0.1, "inches")) + #图表网格颜色、线形、线宽 grids(linetype = "dashed",color='gray66',size=0.1)+ #图表外边框的线宽 border(size=0.3)+ #刻度的宽度和长度还有朝向 theme(axis.ticks = element_line(size = 0.3),axis.ticks.length =unit(-0.1,'cm')) 

4.准备数据

data<-dataset%>%gather("Hokkaido", "Aomori" , "Iwate" , "Miyagi" , "Tokyo",key =City, ,value=Infection_population ) data$Date<-as.character(data$Date) data%>%dplyr::filter(Infection_population>10100 & Infection_population<20100) data%>%select(1) dataT<-data.table(dataset) nrow(dataT) view(dataT[Infection_population<1000]) 

ggplot2调整所有字体大小_翻译字体转换器 (https://mushiming.com/)  第1张

5.绘图

g<-ggbarplot(data, "Date", "Infection_population", fill = "City", color = "white", x.text.angle = 45 , y.text.angle = 0 , xlab="日期", ylab="感染人数", palette = themE, position = position_dodge(0.9) )+p g+p ggsave(filename = "foo2.png",g,width = 15, height = 9, dpi = 300, units = "cm",device='png') 

ggplot2调整所有字体大小_翻译字体转换器 (https://mushiming.com/)  第2张

由于先前设置的图片大小是15cm*9cm,所以在word中也要把图片调成这个大小,才能让字体大小等于9,英文9号对应的是中文小五号。中英文字号大小自行百度,其他的原理跟上一篇文章的一样seaborn傻瓜级主题设置,轻松使图表达到报告和论文的样式要求

ggplot2 绘图天下第一,这也是很多人不舍得放弃R的原因。欢迎交流

THE END

发表回复