Legendsggplot2图例的一些操作

  1. 移除图例
  2. require(ggplot2)
    b = qplot(Sepal.Length,Petal.Length,data=iris,geom="point",colour = Species)
    #有图例
    b
    #去除图例
    b+theme(legend.position="none")#右图

  2.图例位置摆放
    

b = qplot(Species,Sepal.Width,data=iris,geom="boxplot",fill = Species)+scale_fill_brewer(palette = "Pastel2")
b+theme(legend.position="top")

  

b+theme(legend.position=c(.92,.9))

  3.移除背景中的边框和背景颜色

  

b+theme(legend.position=c(.92,.9))+theme(legend.key = element_blank())+theme(legend.background = element_blank())

  4.改变图例中key的顺序

b+scale_fill_brewer(palette="Pastel2",limits=c("virginica","versicolor","setosa"))

  5.修改图例名称

b = ggplot(iris,aes(x=Species,y=Sepal.Width,fill = Species))+geom_boxplot()
b+theme(legend.position="top")+labs(fill = "iris-Spec")

  6.移除图例标题
  

b = ggplot(iris,aes(x=Species,y=Sepal.Width,fill = Species))+geom_boxplot()
b+theme(legend.position="top")+guides(fill = guide_legend(title = NULL))

  7.去掉legend的背景色

  1)去掉背景

legend.background = element_blank()

  2)去掉KEY的背景

legend.key = element_blank()

原文地址:https://www.cnblogs.com/yangk/p/4761994.html