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