前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2-theme(主题)

ggplot2-theme(主题)

作者头像
生信补给站
发布2020-08-06 10:36:23
7570
发布2020-08-06 10:36:23
举报
文章被收录于专栏:生信补给站生信补给站

gglpot2的主题可以设置一些字体、颜色、背景色、网格线等东西。

概括的说元素函数主要有四种:element_text(文本)、element_lines(线条)、element_rect(矩形块)和element_blank(主题), 方式总体为 theme(主题元素=函数(参数))

简单示例:

x<-c(2015,2016,2017,2018,2019,2020)

y<-c(12,13,14,25,36,57)

test<-data.frame(Year=x,Num=y)

p_theme <- ggplot(data = test, mapping = aes(x = Year, y = Num)) +geom_line(colour = 'blue', size = 1) + geom_point(colour = 'red', size = 2)

p_theme

Ⅰ.对整体背景进行改变(rect 或者 blank)

theme(plot.background=element_rect(fill=,color=, size=, linetype=, ))#依次为填充色,轮廓色,边界大小,边界线条类型(dotted,dotdash,dashed,solid...) #

p_theme+labs(title="小白学R",subtitle = "学不会",caption = "坚持")+

theme(plot.background=element_rect(fill='grey',color='red', size=2, linetype='dotdash'))

Ⅱ.对标题进行改变(text)

theme(plot.title=element_text(face=,color=,size=, hjust=,vjust=, angle=,lineheight=))#依次是字体类型 ("plain", "italic","bold", "bold.italic"),颜色,大小,水平位置(0到1),垂直位置(0到1),逆时针旋转(0到360),线高度

#对标题、X轴进行设置

p_theme+labs(title="小白学R",subtitle = "学不会",caption = "坚持")+

theme(plot.title=element_text(face="bold.italic",color="steelblue",size=24, hjust=0.5,vjust=0.5,angle=360,lineheight=113), ##对标题进行改变

axis.title.x=element_text(face="italic",color="#FFAEB9",size=16,

hjust=0.5,vjust=0.5, angle=45,lineheight=113), ##对x轴的坐标轴名称进行改动

axis.text.x=element_text(face="bold",color="red",size=16,

hjust=0.5,vjust=0.5, angle=90,lineheight=113), ##对x轴的坐标轴刻度值进行改动

axis.ticks.x=element_line(color='skyblue',size=9,lineend='round'), ##对x轴的刻度点类型进行改动

axis.line.x=element_line(color='yellow',size=6,lineend='butt')) ##对x轴的刻度线进行改动 lineend(round,butt,square)

Ⅲ. 面板背景色及网格线设置

p_theme+labs(title="小白学R",subtitle = "学不会",caption = "坚持")+

theme(panel.grid.major=element_line(color='skyblue',size=2,linetype=NULL,lineend='butt'))+

theme(panel.grid.minor=element_line(color='grey50'))+ #改变主次网格线颜色线型大小等

theme(panel.grid.major.x=element_blank(),panel.grid.minor.x=element_blank())+#同时隐藏与x坐标轴相交的主次网格线

theme(panel.grid=element_blank())+ #隐藏网格线(等价于同时隐藏主次网格线)

theme(panel.background=element_rect(fill='grey')) #面板背景色

Ⅳ.总体调整

theme(rect=element_rect()) #矩形属性

theme(line=element_line()) #线性属性

theme(text=element_text()) #文本属性

theme(title=element_title()) #标题属性

Ⅴ.ggplot2的两个主题

theme_grey() 、theme_bw()

Ⅵ.ggthemes包的十个主题

theme_economist() 、theme_economist_white() 、theme_wsj() 、theme_excel() 、theme_few() 、theme_foundation() 、theme_igray() 、theme_solarized() 、theme_stata() 、theme_tufte()

此12个主题可以自己画出来看一下,是否适合自己的绘图目的和风格。当然有了自己的审美之后,可以自定义theme,然后绘图时候进行调用。

另:theme同样可以根据自己需要的在上述的“+”前后选择合适的调整进行组合,未完待续。。。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-11-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信补给站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档