library(tidyverse)
a <- ggplot(mpg,aes(hwy,cty)) +
? geom_point(aes(colour=drv))
a
#1.removing the legend
ggplot(mpg,aes(hwy,cty)) +
? geom_point(aes(colour=drv),show.legend=F)
a + theme(legend.position='none')
a + guides(colour='none')
a + scale_colour_discrete(guide='none')
#2.legends and layers
ggplot(mpg,aes(hwy,cty)) +
? geom_point(size=3,colour='grey20') +
? geom_point(aes(colour=drv),size=1)?
ggplot(mpg,aes(hwy,cty)) +
? geom_point(size=3,colour='grey20',show.legend=T) +
? geom_point(aes(colour=drv),size=1)?
ggplot(mpg,aes(hwy,cty)) +
? geom_point(aes(colour=drv))
ggplot(mpg,aes(hwy,cty)) +
? geom_point(aes(shape=drv))
ggplot(mpg,aes(hwy,cty)) +
? geom_point(aes(colour=drv,shape=drv))?
#3.layout
a + theme(legend.position='right')?
a + theme(legend.position='left')
a + theme(legend.position='top')
a + theme(legend.position='bottom')
a + theme(legend.position=c(0.5,0.5))?
a + theme(legend.position=c(1,1))
a + theme(legend.position=c(0,0))
#You can also use legend.justification to set which part of the legend box is set to the position at legend.position.
a + theme(legend.position=c(0.5,0.5),
? ? ? ? ? legend.justification=c(0.5,0.5))?
a + theme(legend.position=c(1,1),
? ? ? ? ? legend.justification=c(1,1))
a + theme(legend.position=c(0,0),
? ? ? ? ? legend.justification=c(0,0))
a + theme(legend.direction='horizontal')
a + theme(legend.position='bottom',legend.direction='vertical')
b <- ggplot(mpg,aes(hwy,cty)) +
? geom_point(aes(colour=class,shape=drv))
b
b + theme(legend.box='horizontal')
b + theme(legend.position='bottom',legend.box='vertical')
b + theme(legend.box.just='right')
b + theme(legend.position='bottom',legend.box.just='top')
#4.names(package called scales provides more),breaks,labels
a + scale_colour_discrete('the type of \n drive train')
a + labs(colour='the type of \n drive train')
a + labs(colour='')
a + labs(colour=NULL)
a + scale_colour_discrete(breaks=c('4','f'))
a + scale_colour_discrete(breaks=NULL)
a + scale_colour_discrete(labels=c('F','R','4'))
a + scale_colour_discrete(labels=NULL)
#5.background,key,title,text,margin
a + theme(legend.background=element_rect(fill='linen'))
a + theme(legend.key=element_rect(fill='linen'))
a + theme(legend.key.size=unit(1,'cm'))
a + theme(legend.key.height=unit(1,'cm'))
a + theme(legend.key.width=unit(1,'cm'))
a + theme(legend.text=element_text(colour='red',size=15))
a + theme(legend.text.align=.5)
a + theme(legend.text.align=0)
a + theme(legend.text.align=1)
a + theme(legend.title=element_text(colour='blue',size=25))
a + theme(legend.title.align=.5)
a + theme(legend.title.align=0)
a + theme(legend.title.align=1)
a + theme(legend.margin=margin(30,30,30,30))