楼主: 屋檐滴语
3156 8

[学习分享] 利用ggplot2包做了一副以复杂为目的的图,大伙继续 [推广有奖]

  • 5关注
  • 7粉丝

已卖:897份资源

教授

80%

还不是VIP/贵宾

-

威望
0
论坛币
11884 个
通用积分
272.9886
学术水平
28 点
热心指数
76 点
信用等级
16 点
经验
2225 点
帖子
882
精华
0
在线时间
2274 小时
注册时间
2010-5-19
最后登录
2025-11-22

楼主
屋檐滴语 发表于 2018-7-5 02:35:10 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
  1. library(tidyverse)
  2. library(gcookbook)

  3. heightweight %>%
  4.   mutate(sex=factor(sex,levels = c("m","f"),
  5.                     labels = c("Male","Famale"))) %>%
  6.   ggplot(aes(ageYear,heightIn,color=sex, shape=sex))+
  7.     geom_point()+
  8.     geom_smooth(method = "lm")+
  9.     scale_color_manual(values = c("darkred", "darkblue"),
  10.                      labels = c("Male", "Famale"))+
  11.     scale_shape_manual(values = c(21,24),
  12.                        labels = c("Male", "Famale"))->p

  13. p +
  14.   ggtitle("Plot title here")+
  15.   theme(
  16.   # 绘图区域选项
  17.     panel.grid.major = element_line(color="wheat3"),
  18.     panel.grid.minor = element_line(color="red",linetype = "dashed",size=0.2),
  19.     panel.background = element_rect(fill="honeydew"),
  20.     panel.border = element_rect(color = "blue",size=1,fill=NA))+
  21.   
  22.   # 坐标轴及标题文本项目选项
  23.   theme(
  24.     axis.title.x=element_text(color="mediumpurple",size=14,face = "bold"),
  25.     axis.title.y=element_text(color="#FFDDAA",size=14,angle=90,face = "bold.italic"),
  26.     axis.text.x=element_text(color="khaki4",size=12),
  27.     axis.text.y=element_text(color="blue"),
  28.     axis.line = element_line(size = 4,color="black",lineend = "square"),
  29.     plot.title=element_text(color="darkred", face = "bold",
  30.                             size=20, hjust = 0, angle=180))+
  31.   
  32.   # 图例选项
  33.   theme(
  34.     legend.position = c(0,1),
  35.     legend.justification = c(0,1),
  36.     legend.background = element_rect(fill="grey85",color="magenta3",size=1),
  37.     legend.title = element_text(color = "blue",face="bold",size=14),
  38.     legend.text = element_text(color="yellow"),
  39.     legend.key = element_rect(color="green",size=0.25)) +
  40.   
  41.   
  42.   # 修改刻度标签的文本
  43.   scale_y_continuous(breaks = c(50,56,60,66,72),
  44.                      labels = c("Tiny",
  45.                                 "Really\nshort",
  46.                                 "Short","Medium",
  47.                                 "Tallish"))+
  48.   
  49.   # 添加矩形阴影区域
  50.   annotate("rect",
  51.            xmin = 13.5, xmax = 14.5,
  52.            ymin = 50, ymax = 72,
  53.            alpha=0.1,fill="red")+
  54.   
  55.   # 添加带有箭头的线段
  56.   annotate("segment",x=13,xend=15,y=50,yend=55,
  57.            size=2,color="green",
  58.            arrow=arrow(angle = 60))+
  59.   
  60.   # 添加地毯
  61.   geom_rug(sides = "b")+
  62.   
  63.   # 分面选项
  64.   facet_grid(sex ~ .)+
  65.   theme(
  66.     strip.background = element_rect(fill="lightblue",color="turquoise4",size=0.8),
  67.     strip.text.y = element_text(size=14,angle = 90,face="bold",color="white"))
  68.   
复制代码
gg.png


二维码

扫码加我 拉你入群

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

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

关键词:ggplot2 gplot plot GPL Background

回帖推荐

zerofung 发表于7楼  查看完整内容

可能注释做的不太好,因为其中用到了一些文本引用,表达式,对表达式的某部分加粗,使用不同字体,使用特殊符号,写在一起了。至于还没弄明白的有:Rstudio中显示与pdf输出结果不一致,主要表现为字体(例如小写的g,以及某些加粗),Rstudio的导出pdf功能也跟代码输出pdf不一样(例如缺少的字体会完全不显示)。共同学习,如果有懂的还请多多指教
已有 3 人评分论坛币 学术水平 热心指数 收起 理由
happy_287422301 + 100 + 2 + 2 精彩帖子
逐梦的太阳 + 5 + 2 + 5 精彩帖子
cheetahfly + 30 精彩帖子

总评分: 论坛币 + 135  学术水平 + 4  热心指数 + 7   查看全部评分

沙发
tmdxyz 发表于 2018-7-5 06:13:21
学习了

藤椅
hifinecon 发表于 2018-7-5 07:55:29 来自手机
屋檐滴语 发表于 2018-7-5 02:35
thanks a lot

板凳
屋檐滴语 发表于 2018-7-5 08:37:42
好看不是目的,配色都是随便找的颜色,主要是学习系统的学习下ggplot2,将来好随心所欲的根据自己需要作图。
已有 1 人评分论坛币 收起 理由
happy_287422301 + 100 补偿

总评分: 论坛币 + 100   查看全部评分

报纸
追梦少年々 学生认证  发表于 2018-7-5 10:14:52
厉害..............

地板
zerofung 学生认证  发表于 2018-7-5 10:17:14
哇感谢!!!我好像也有写类似的东西,看看能不能在这里重复!!!

7
zerofung 学生认证  发表于 2018-7-5 10:27:09
  1. library(ggplot2)
  2. library(showtext)
  3. df <- data.frame(x=1:3,y=1:3,text=letters[2:4], face = c("plain", "bold", "italic"))
  4. GeneName <- "KIF2C"
  5. font.add("STXINGKA", "STXINGKA.TTF")
  6. font.add("STHUPO","STHUPO.TTF")
  7. showtext.auto()
  8. pdf("example.pdf", 7, 4)  ## PDF device
  9. ggplot(df,aes(x,y,color=text)) + geom_text(aes(label=text,fontface=face),size=10) +
  10.   labs(x = bquote(.(GeneName)~a^-10~bold(a^bold("-10"))~log[2](frac(FC,cd))~~~mu[3]%*%phi),
  11.        y = expression(bold("-"*log[bold("-10")]~~~-log[10])),
  12.        color = expression("Temperature (" * degree * C *")"*"\u2030")) + ### Unicode for Per Mille Sign is U+2030
  13.   theme(axis.title.x=element_text(size=20),
  14.         axis.title.y=element_text(size=20)) +
  15.   scale_color_manual(labels=c("b for \n bad",expression("c for"~chi),"d for 狗"),
  16.                      values = c("blue","red","green")) +
  17.   annotate("text",x=2.5,y=0.5,label="这是华文琥珀",family="STHUPO") +
  18.   annotate("text",x=0,y=2.5,label="这是行楷\n真的不骗你,输出pdf你就知道了",family="STXINGKA",hjust="inward",vjust="inward") +
  19.   annotate("text",x=0,y=0.1,label='"P = 5.73 x"~10^-9',hjust="inward",vjust="inward",parse = T)  # can`t use \n and expression
  20. dev.off()
  21. showtext.auto(FALSE)
复制代码


可能注释做的不太好,因为其中用到了一些文本引用,表达式,对表达式的某部分加粗,使用不同字体,使用特殊符号,写在一起了。至于还没弄明白的有:Rstudio中显示与pdf输出结果不一致,主要表现为字体(例如小写的g,以及某些加粗),Rstudio的导出pdf功能也跟代码输出pdf不一样(例如缺少的字体会完全不显示)。共同学习,如果有懂的还请多多指教
输出pdf后显示
rstudio中显示
已有 2 人评分论坛币 收起 理由
happy_287422301 + 100 精彩帖子
cheetahfly + 30 精彩帖子

总评分: 论坛币 + 130   查看全部评分

8
屋檐滴语 发表于 2018-7-5 10:49:38
zerofung 发表于 2018-7-5 10:27
可能注释做的不太好,因为其中用到了一些文本引用,表达式,对表达式的某部分加粗,使用不同字体,使用 ...
我那个图确实是少了回归方程。另外还可以在加一条均线。
至于你做的图,在Rstudio里显示的和保存的效果不一样,在R notebook里试试,谢益辉貌似解决了这个问题。
已有 2 人评分论坛币 热心指数 收起 理由
happy_287422301 + 100 鼓励积极发帖讨论
逐梦的太阳 + 2 + 5 精彩帖子

总评分: 论坛币 + 102  热心指数 + 5   查看全部评分

9
xujingjun 发表于 2018-7-6 01:34:32

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-19 21:31