本篇内容主要讲解“R语言怎么调整图的位置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“R语言怎么调整图的位置”吧!
image.png我选择使用R语言的ggplot2来实现,这个是箱线图和热图的拼接,右侧的热图可以借助geom_point()
函数实现,将点的形状改为正方块,数值按照正负来映射颜色,按照一定的数值来映射大小。
首先是构造一份数据df<-data.frame(x=LETTERS[1:4],y=1:4)
df
image.png 正常做一个柱形图library(ggplot2)
ggplot(data=df,aes(x=x,y=y))+
geom_col(aes(fill=x))
image.png 拉大图例与主图的距离使用theme()
函数中的legend.box.margin
参数来调节
ggplot(data=df,aes(x=x,y=y))+
geom_col(aes(fill=x))+
theme(legend.box.margin = margin(0,0,0,5,unit = 'cm'))
image.png 将图例放到右上角通过 legend.justification
参数来实现
ggplot(data=df,aes(x=x,y=y))+
geom_col(aes(fill=x))+
theme(legend.box.margin = margin(0,0,0,5,unit = 'cm'),
legend.justification = c(0,1))
image.png 还有另外两个知识点是调节图例的标题和图例的距离以及图例的文本和图例的距离分别需要用到legend.spacing.y
和legend.spacing.x
参数
ggplot(data=df,aes(x=x,y=y))+
geom_col(aes(fill=x))+
theme(legend.box.margin = margin(0,0,0,5,unit = 'cm'),
legend.justification = c(0,1),
legend.spacing.y = unit(5,'cm'),
legend.spacing.x = unit(5,'cm'))
image.png到此,相信大家对“R语言怎么调整图的位置”有了更深的了解,不妨来实际操作一番吧!这里是天达云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!