> 文章列表 > ggplot作图中的图例处理方法

ggplot作图中的图例处理方法

ggplot作图中的图例处理方法

文章目录

  • 改变坐标轴和图例的名称
    • 方法1, labs()
    • 方法2,scale_xxx_discrete/continuous()
  • 删除坐标轴和图例的名称
    • 方法1, labs()
    • 方法2,scale_xxx_discrete/continuous()
    • 方法3,theme()
    • 方法4,guides()可以去图例名称
  • 改变图例或标轴轴的显示刻度
    • 利用scale_xxx_discrete/continuous()
  • 调整图例位置
    • 利用 theme()
  • 删除图例
    • 方法1,guides()
    • 方法2, theme()
  • 图例中的颜色

改变坐标轴和图例的名称

方法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='尺寸')

ggplot作图中的图例处理方法

方法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')

ggplot作图中的图例处理方法

删除坐标轴和图例的名称

方法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='')

ggplot作图中的图例处理方法

方法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='')

ggplot作图中的图例处理方法

方法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())

ggplot作图中的图例处理方法

方法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),)

ggplot作图中的图例处理方法

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

利用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('红','橙','黄','绿','蓝'))

ggplot作图中的图例处理方法

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('红','橙','黄','绿','蓝'))

ggplot作图中的图例处理方法

调整图例位置

利用 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')

ggplot作图中的图例处理方法

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"))

ggplot作图中的图例处理方法

删除图例

方法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)

ggplot作图中的图例处理方法

方法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')

ggplot作图中的图例处理方法

图例中的颜色

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'))

ggplot作图中的图例处理方法