ggplot中坐标轴和图例的相关处理

改变坐标轴和图例的名称

方法1, labs()

ggplot中图例框的名字和坐标轴的名字都可以通过labs()来改变,例如:

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  labs(x='横轴',y='纵轴',shape='形状',color='颜色',size='尺寸')

在这里插入图片描述

方法2,scale_xxx_discrete/continuous()

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  scale_x_continuous(name='横轴2')+scale_y_continuous(name='纵轴2')+
  scale_color_discrete(name='颜色2')+scale_size_continuous(name='尺寸2')+
  scale_shape_discrete(name='形状2')

在这里插入图片描述

删除坐标轴和图例的名称

方法1, labs()

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  labs(x='',y='',shape='',color='')

在这里插入图片描述

方法2,scale_xxx_discrete/continuous()

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  scale_x_continuous(name='')+scale_y_continuous(name='')+
  scale_color_discrete(name='')+scale_size_continuous(name='')+
  scale_shape_discrete(name='')

在这里插入图片描述

方法3,theme()

利用theme统一去掉名称后,位置也被去掉了。

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  theme(legend.title=element_blank(),axis.title = element_blank())

在这里插入图片描述

方法4,guides()可以去图例名称

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  guides(shape=guide_legend(title = NULL),
         color=guide_legend(title = NULL),
         size=guide_legend(title = NULL),
         )

在这里插入图片描述

可见guides()和theme()都可以对图例进行调整,但是theme()是统一调整,guides()可实现对单个图例进行调整,作用更加具有独特性。

改变图例或标轴轴的显示刻度

利用scale_xxx_discrete/continuous()

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  scale_x_continuous(name='改变坐标刻度',breaks=c(1,3,5),labels=c('一','三','五'))+
  scale_color_discrete(name='颜色',breaks=c(1,2,3,4,5),labels=c('红','橙','黄','绿','蓝'))

在这里插入图片描述

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  scale_x_continuous(name='改变坐标刻度',breaks=c(1,3,5),labels=c('一','三','五'))+
  scale_color_brewer(name='颜色',breaks=c(1,2,3,4,5),labels=c('红','橙','黄','绿','蓝'))


在这里插入图片描述

调整图例位置

利用 theme()

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  scale_x_continuous(name='改变坐标刻度',breaks=c(1,3,5),labels=c('一','三','五'))+
  theme(legend.position = 'bottom')
  

在这里插入图片描述

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  scale_x_continuous(name='改变坐标刻度',breaks=c(1,3,5),labels=c('一','三','五'))+
  theme(legend.position = c(.95, .95),#plot内位置
        legend.justification = c("right", "top"))

在这里插入图片描述

但是,能不能将shape和size两个图例放在不同的位置展示呢?没有尝试成功,可能不行。

调整title的位置

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  labs(title='This is a title!')+
  theme(plot.title = element_text(size=12,hjust = 0.5))

在这里插入图片描述

title, 坐标轴名称,图例名称的换行

title的换行

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  labs(title='This is a title!\n 令起一行')+
  theme(plot.title = element_text(size=12,hjust = 0.5))

在这里插入图片描述

坐标轴名称的换行

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  labs(x='This is a title! \n 另外一行 \n 再另起一行')

在这里插入图片描述

图例名称的换行

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  labs(size='This is a title! \n 另外一行 \n 再另起一行')

在这里插入图片描述

删除图例

方法1,guides()

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  guides(shape=FALSE,size=FALSE)

在这里插入图片描述

方法2, theme()

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  theme(legend.position='none')

在这里插入图片描述

图例中的颜色

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  scale_x_continuous(name='改变坐标刻度',breaks=c(1,3,5),labels=c('一','三','五'))+
  theme(legend.position = c(.95, .95),#plot内位置
        legend.justification = c("right", "top"),
        legend.background = element_rect(color='black',fill='pink',size = 2),
        legend.key = element_rect(color='purple',fill='yellow',size = 2),
        legend.box.background =  element_rect(color = 'blue',fill='green',size = 2),
        legend.text = element_text(color = 'white'))

    

在这里插入图片描述

改变图例的方向

利用guides()单个调整

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  scale_x_continuous(name='改变坐标刻度',breaks=c(1,3,5),labels=c('一','三','五'))+
  guides(shape=guide_legend(direction='horizontal'))

在这里插入图片描述

利用theme()统一调整

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  theme(legend.direction = 'horizontal')

在这里插入图片描述

改变图例的行数和列数

library(ggplot2)
data=data.frame(x=1:10,y=1:5,z=factor(1:5))
ggplot(data,aes(x=x,y=y,shape=z,color=z,size=x))+geom_point()+
  guides(shape=guide_legend(nrow=2))

在这里插入图片描述

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值