install.packages('ggplot2')
library(ggplot2)
ggplot(a)+geom_bar(aes(x1,y,fill/col=x1/x2),position='dodge',stat='summary',fun='sum'/'mean')条形图+theme(text = element_text(family='Kai'))
ggplot(a)+geom_boxplot(aes(x1,y,col=x1/x2))箱线图
ggplot(a)+geom_point(aes(x1,y,col=x1/x2),position=position_jitter(width=0.04))散点图
1+geom_point(aes(x1,y,col=x1/x2),stat='summary',fun='sum'/'mean')+散点
2+geom_line(aes(x1,y,group=1/x2,col=x1/x2),stat='summary',fun='sum'/'mean')+折线
3+geom_errorbar(aes(x=x1,ymin=y-se,ymax=y+se,col=x1/x2),position=position_dodge(0.9),width=0.2)+误差棒
4+geom_text(aes(x1,y,label=marker,col=x1/x2),position=position_dodge(0.9)vjust=2或y+2)+显著字母
ggplot(a,aes(x1,y,fill/col=x1/x2))+geom_bar(position='dodge',stat='summary',fun='sum'/'mean')+geom_errorbar(aes(ymin=y-se,ymax=y+se),position=position_dodge(0.9),width=0.2)+geom_text(aes(label=marker),position=position_dodge(0.9),vjust=-2)条形图+误差棒+显著字母(坐标写一次即可)
ggplot(a,aes(x1,y,col=x1/x2))+geom_point(position=position_jitter(width=0.04),stat='summary',fun='sum'/'mean')+geom_line(aes(group=1/x2),stat='summary',fun='sum'/'mean')+geom_errorbar(aes(ymin=y-se,ymax=y+se),position=position_dodge(0.9),width=0.2)+geom_text(aes(label=marker),position=position_dodge(0.9),vjust=-2)散点图+折线+误差棒+显著字母(坐标写一次即可)
+geom_density(aes(y=liqi))密度图(1个数值型)
+geom_area(aes(x=tan,y=liqi))区域图(2个数值型)
+geom_smooth(aes(x=tan,y=liqi,group/col=chong),formula=y~x,method='lm',se=F)拟合图,分组/线条颜色(2个数值型)
+facet_wrap(~riqi,ncol/nrow=2,labeller='label_both/value')分面图,每行或每列分面数,分面标题
+xlab('自变量1(单位)')+ylab('因变量(单位)')+scale_fill_discrete(name='自变量2')更改轴和图例名称+coord_cartesian(ylim= c(0,80))限定轴范围
(fill=x1/x2,有此即可变色)+scale_fill_manual(values = c('grey70', 'grey50', 'grey30'))改变条形填充颜色(颜色数量=分组数量)
(col=x1/x2,有此即可变色)+scale_color_manual(values = c('red', 'orange', 'yellow'))改变颜色(颜色数量=分组数量)
数据准备
函数:legend(location, title, legend, ....)
参数详解:
x和y:用于定位图例,也可用关键词"bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" 和 "center";当图例用关键词设置位置后,inset = 分数,可以设置其相对位置;
legend:指定图例标签,字符或表达式向量;
fill:用特定的颜色进行填充;
col:设置图例中出现的点或线的颜色;
border:当fill = 参数存在的情况下,用于指定填充的边框颜色;
lty, lwd:图例中线的类型与宽度;
pch:点的类型;
angle:阴影的角度;
density:阴影线的密度;
cex:指定图例显示大小;
bg:指定图例的背景色;
bty:指定图例框是否画出,默认o为画出,n为不画出;
box.lty, box.lwd, box.col: 设置图例边框线型,线粗,颜色,box.lty为虚线,box.lwd决定粗线,box.col决定颜色;
pt.bg:图例中点的背景色;
pt.cex:图例中点的大小;
pt.lwd:图例中点边缘的线宽;
x.intersp:图例中文字离图片的水平距离;
y.intersp:图例中文字离图片的垂直距离;
adj:图例中字体的相对位置;
text.width:图例中字体所占的宽度,调整后图例整个宽度也跟着变化了;
text.col:图例字体的颜色;
text.font:图例字体;
merge:逻辑值,merge=TRUE,合并点与线,但不填充图例框,默认为TRUE;
trace:逻辑值,trace=TRUE显示图例信息;
plot:逻辑值,plot=FALSE不画出图例;
ncol:图例中分类的列数;
horiz:逻辑值,horiz=TRUE,水平放置图例;
title:给图例加标题;
xpd:xpd=FALSE,即不允许在作图区域外作图,改为TRUE即可,与par()参数配合使用;
title.col:标题颜色;
title.adj:图例标题的相对位置,0.5为默认,在中间。0最左,1为最右;
seg.len:指定图例中线的线长,长度单位为字符宽度。
1 图例方位
2 修饰图例
3 图例绘制在图外
4 自定义图例
有时候绘制出的图是分组图,这时候需要自定义绘制图例。
参考资料:
《R语言实战》(中文版),人民邮电出版社,2013.
R语言绘制图例(legend)的各种问题_详细综合解析, https://blog.csdn.net/xiangyong58/article/details/54579293
投必得R语言教程,第二讲 R作图-基础-图形参数设置:标题、图例、文字, https://mp.weixin.qq.com/s?__biz=MzU1Mzc3OTIwNg==&mid=2247495531&idx=1&sn=cdd80d4e950ae2b344cf188c68922fa8&chksm=fbef0602cc988f14e93b71bc6fcc2fda782d3ae9a3a67601a6c87756f4ae85bcc5d9c56d9b51&scene=21#wechat_redirect
一幅图解决R语言绘制图例的各种问题, https://blog.csdn.net/weixin_30469895/article/details/96649305