ggplot2的legends

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


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市粘拾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖而姐,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惹想,死亡現(xiàn)場(chǎng)離奇詭異马昙,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)恋拍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)藕甩,“玉大人施敢,你說(shuō)我怎么就攤上這事⌒疗迹” “怎么了悯姊?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)贩毕。 經(jīng)常有香客問(wèn)我悯许,道長(zhǎng),這世上最難降的妖魔是什么辉阶? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任先壕,我火速辦了婚禮,結(jié)果婚禮上谆甜,老公的妹妹穿的比我還像新娘垃僚。我一直安慰自己,他們只是感情好规辱,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布谆棺。 她就那樣靜靜地躺著,像睡著了一般罕袋。 火紅的嫁衣襯著肌膚如雪改淑。 梳的紋絲不亂的頭發(fā)上碍岔,一...
    開(kāi)封第一講書(shū)人閱讀 52,262評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音朵夏,去河邊找鬼蔼啦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛仰猖,可吹牛的內(nèi)容都是我干的捏肢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼饥侵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼鸵赫!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起躏升,我...
    開(kāi)封第一講書(shū)人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤奉瘤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后煮甥,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體盗温,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年成肘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了卖局。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡双霍,死狀恐怖砚偶,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情洒闸,我是刑警寧澤染坯,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站丘逸,受9級(jí)特大地震影響单鹿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜深纲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一仲锄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧湃鹊,春花似錦儒喊、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春芯义,著一層夾襖步出監(jiān)牢的瞬間肛搬,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工毕贼, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蛤奢。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓鬼癣,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親啤贩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子待秃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容