楼主: 松石空月
12103 2

[学习分享] R语言 ggplot2 theme & margin [推广有奖]

  • 0关注
  • 0粉丝

本科生

74%

还不是VIP/贵宾

-

威望
0
论坛币
1028 个
通用积分
90.4623
学术水平
8 点
热心指数
16 点
信用等级
5 点
经验
1043 点
帖子
69
精华
0
在线时间
138 小时
注册时间
2014-11-1
最后登录
2023-10-20

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
ggplot2 作为一款强大的R语言画图包,我认为有一部分功劳归于theme的设置,现将ggplot2中关于theme的设置做个简单归纳

theme中各项参数基本用margin中的margin(),element_blank(),  element_rect(), element_line(), element_text()来定义:

margin()中的参数:t r b l, 分别对应top,right,bottom和left,即上、右、下和左,后面加单位;
element_blank()表示theme某参数为空值;


element_rect(fill= NULL, colour = NULL, size = NULL, linetype = NULL, color= NULL, inherit.blank = FALSE)

element_line(colour= NULL, size = NULL, linetype = NULL, lineend= NULL, color = NULL, arrow = NULL, inherit.blank = FALSE)

element_text(family= NULL, face = NULL, colour = NULL, size = NULL, hjust= NULL, vjust = NULL, angle = NULL, ineheight = NULL,color= NULL, margin = NULL, debug = NULL, inherit.blank = FALSE)


fill, color均与颜色相关,fill为填充色,color为边框色

size 大小,各项参数的大小,如果是线,则为粗度,

linetype ,线的类型

family,face 字体, face = "plain","italic", "bold", "bold.italic" (平的,斜体,粗体,粗体斜体)

hjust,vjust 水平 垂直调整距离

angle 0-360,角度调整


# 区域的介绍

  1. # 1.area
  2. library(tidyverse)
  3. x.tb <- data.frame(item.1 = rep(letters[1:10], times = 10),
  4.                    item.2 = rep(letters[1:5], times = 20),
  5.                    item.3 = rep(letters[6:10], times = 20),
  6.                    num.1 = sample(10:99, 100, replace = T),
  7.                    num.2 = sample(10:99, 100, replace = T)) %>%
  8.   as.tbl()
  9. ggplot(x.tb, aes(num.1, num.2, color = item.1, shape = item.2))+
  10.   geom_point(size = 2)+
  11.   labs(title = "NUM.1 & NUM.2",
  12.        x = "NUM.1",
  13.        y = "NUM.2")+
  14.   facet_grid(~item.3)+
  15.   theme(
  16.     legend.key = element_rect(fill = "chocolate1", colour = "black"),
  17.     legend.background = element_rect(fill = "aliceblue", colour = "brown1"),
  18.     legend.box.background = element_rect(fill = "white", color = "black"),
  19.     strip.background = element_rect(fill = "lightblue1", color = "black"),
  20.     panel.background = element_rect(fill = "white", colour = "black"),
  21.     plot.background = element_rect(fill = "gray87", colour = "black"))
复制代码

ggplot_theme_area.png


# 2.text

  1. # 2.text  
  2. library(tidyverse)
  3. x.tb <- data.frame(item.1 = rep(letters[1:10], times = 10),
  4.                    item.2 = rep(letters[1:5], times = 20),
  5.                    item.3 = rep(letters[6:10], times = 20),
  6.                    num.1 = sample(10:99, 100, replace = T),
  7.                    num.2 = sample(10:99, 100, replace = T)) %>%
  8.   as.tbl()
  9. ggplot(x.tb, aes(num.1, num.2, color = item.1, shape = item.2))+
  10.   geom_point(size = 2)+
  11.   labs(title = "NUM.1 & NUM.2",
  12.        x = "NUM.1",
  13.        y = "NUM.2")+
  14.   facet_grid(~item.3)+  
  15.   theme(plot.title = element_text(size = 16, hjust = 0.5, face = "bold"),
  16.         axis.title = element_text(size = 8),
  17.         axis.text = element_text(size = 6,face = "bold.italic", color = "blue"),
  18.         legend.title = element_text(size = 6, colour = "red"),
  19.         legend.text = element_text(color = "Brown1", face = "bold"),
  20.         strip.text = element_text(size = 10, face = "italic"))
复制代码

ggplot_theme_text.png


# 3.line

  1. library(tidyverse)
  2. x.tb <- data.frame(item.1 = rep(letters[1:10], times = 10),
  3.                    item.2 = rep(letters[1:5], times = 20),
  4.                    item.3 = rep(letters[6:10], times = 20),
  5.                    num.1 = sample(10:99, 100, replace = T),
  6.                    num.2 = sample(10:99, 100, replace = T)) %>%
  7.   as.tbl()
  8. ggplot(x.tb, aes(num.1, num.2, color = item.1, shape = item.2))+
  9.   geom_point(size = 2)+
  10.   labs(title = "NUM.1 & NUM.2",
  11.        x = "NUM.1",
  12.        y = "NUM.2")+
  13.   facet_grid(~item.3)+
  14.   theme(
  15.     axis.ticks = element_line(size = 2, color = "blue"),
  16.     axis.line = element_line(size = 1.5, color = "red"),
  17.     panel.border = element_rect(size = 2,fill = NA, color = "seagreen4"),
  18.     panel.grid.major = element_line(size = 1.1, color = "maroon4"),
  19.     panel.grid.minor = element_line(color = "orangered")
  20.   )
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:ggplot2 margin Theme gplot plot bottom color

ggplot_theme_line.png (45.09 KB)

ggplot_theme_line.png

已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
nuomin + 10 + 1 + 1 + 1 精彩帖子

总评分: 论坛币 + 10  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

本帖被以下文库推荐

沙发
embracefall 发表于 2019-11-1 16:29:49 |只看作者 |坛友微信交流群
[em17]

使用道具

藤椅
yangming98 发表于 2019-11-1 23:56:08 来自手机 |只看作者 |坛友微信交流群
松石空月 发表于 2017-2-19 21:10
ggplot2 作为一款强大的R语言画图包,我认为有一部分功劳归于theme的设置,现将ggplot2中关于theme的设置做 ...
好的好的好的好的好的呀

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-24 17:37