ggplot2 003 繪圖參數(shù) — 標(biāo)題及圖例

ggplot2 Graphical Parameters

1.標(biāo)題:主標(biāo)題,軸和圖例標(biāo)題

ggplot2軟件包修改繪圖標(biāo)題(主標(biāo)題,軸標(biāo)簽和圖例標(biāo)題)主要是通過以下函數(shù):

  • ggtitle(label)#主標(biāo)題
  • xlab(label)#x軸標(biāo)簽
  • ylab(label)#y軸標(biāo)簽
  • labs(...)#用于主標(biāo)題,軸標(biāo)簽和圖例標(biāo)題

1.1 數(shù)據(jù)準(zhǔn)備

# 將dose轉(zhuǎn)換weight因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# 檢查數(shù)據(jù)
head(ToothGrowth)
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
p
image.png

1.2 更改主標(biāo)題及軸標(biāo)題

1.2.1 使用 ggtitle(), xlab() and ylab()
p + ggtitle("Plot of length \n by dose") +
  xlab("Dose (mg)") + ylab("Teeth length")
image.png
1.2.2 使用labs()
p +labs(title="Plot of length \n by dose",
        x ="Dose (mg)", y = "Teeth length")
image.png

1.3 使用labs()更改圖例

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose))+
  geom_boxplot()
p
# 更改圖例
p1 <- p + labs(fill = "Dose (mg)")
image.png

1.4 更改主標(biāo)題和軸標(biāo)簽的外觀

可以使用theme()和element_text()函數(shù)自定義主標(biāo)題以及x和y軸標(biāo)簽

# main title
p + theme(plot.title = element_text(family, face, colour, size))
# x axis title 
p + theme(axis.title.x = element_text(family, face, colour, size))
# y axis title
p + theme(axis.title.y = element_text(family, face, colour, size))

## family : font family
## face : font face. Possible values are “plain”, “italic”, “bold” and “bold.italic”
## colour : text color
## size : text size in pts
## hjust : horizontal justification (in [0, 1])
## vjust : vertical justification (in [0, 1])
## lineheight : line height. In multi-line text, the lineheight argument is used to change the
## spacing between lines.
## color : an alias for colour
  • example
# Default plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() +
  ggtitle("Plot of length \n by dose") +
  xlab("Dose (mg)") + ylab("Teeth length")
p
# Change the color, the size and the face of
# the main title, x and y axis labels
p1 <- p + theme(
plot.title = element_text(color="red", size=14, face="bold.italic"),
axis.title.x = element_text(color="blue", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold")
)
ggarrange(p,p1)
image.png

1.5 移除x和y軸標(biāo)簽

可以使用element_blank()函數(shù)隱藏主要標(biāo)題和軸標(biāo)簽

p + theme(
  plot.title = element_blank(),
  axis.title.x = element_blank(),
  axis.title.y = element_blank())
image.png

2 圖例的位置及外觀

2.1 數(shù)據(jù)準(zhǔn)備

# 將dose轉(zhuǎn)換weight因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# 檢查數(shù)據(jù)
head(ToothGrowth)
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + 
  geom_boxplot()
p
image.png

2.2 更改圖例位置

可以使用theme()函數(shù)來更改圖例的位置

# legend.position are : “l(fā)eft”,“top”, “right”, “bottom”,vector c(x,y)
p1 <- p + theme(legend.position="top")
p2 <- p + theme(legend.position="bottom")
p3 <- p + theme(legend.position = c(0.8, 0.2))
ggarrange(p1,p2,p3,nrow=1)
image.png

2.3 更改圖例標(biāo)題和文本字體樣式

# 圖例標(biāo)題
p4 <- p + theme(legend.title = element_text(colour="blue", size=10, 
                                      face="bold"))
# 圖例標(biāo)簽
p5 <- p + theme(legend.text = element_text(colour="blue", size=10, 
                                     face="bold"))
ggarrange(p4,p5)
image.png

2.4 更改圖例框的背景顏色

# 背景顏色修改
p6 <- p + theme(legend.background = element_rect(fill="lightblue", 
                                           size=0.5, linetype="solid"))
p7 <- p + theme(legend.background = element_rect(fill="lightblue",
                                           size=0.5, linetype="solid", 
                                           colour ="darkblue"))
ggarrange(p6,p7)
image.png

2.5 更改圖例項的順序

p + scale_x_discrete(limits=c("2", "0.5", "1"))
image.png

2.6 移除圖例

# Remove only the legend title
p8 <- p + theme(legend.title = element_blank())
# Remove the plot legend
p9 <- p + theme(legend.position='none')
ggarrange(p8,p9)
image.png

2.7 刪除條形圖例中的斜線

# 默認(rèn)圖
p10 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose)) + geom_bar()
# 更改條形圖邊框顏色,
# 但在圖例中添加了斜杠
p11 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose)) +
  geom_bar(colour="black")
p11
# 隱藏斜線:
# 1.繪制沒有邊框顏色的條,
# 2.再次用邊框顏色繪制條形,但帶有空白圖例。
p12 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose))+ 
  geom_bar() + 
  geom_bar(colour="black", show.legend = FALSE)
ggarrange(p10,p11,p12,nrow = 1)

然而很尷尬,第二幅圖的圖例并沒有出現(xiàn)斜線

image.png

3. guides() 設(shè)置或刪除圖例以獲得特定的美感

3.1 設(shè)置或刪除具有特定美感的圖例(填充,顏色,大小,形狀等)

# Prepare the data : convert cyl and gear to factor variables
mtcars$cyl<-as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)
head(mtcars)
p <- ggplot(data = mtcars, 
            aes(x=mpg, y=wt, color=cyl, size=qsec, shape=gear))+
  geom_point()
# Print the plot without guide specification
p
image.png

3.2 更改多個指南的圖例位置

# Change the legend position
p +theme(legend.position="bottom")
image.png
# Horizontal legend box
p +theme(legend.position="bottom", legend.box = "horizontal")
image.png

3.3 更改多個圖例的順序

p+guides(color = guide_legend(order=1),
         size = guide_legend(order=2),
         shape = guide_legend(order=3))
image.png

如果使用連續(xù)顏色,則可以使用功能guide_colourbar()更改顏色指南的順序:

qplot(data = mpg, x = displ, y = cty, size = hwy,
      colour = cyl, shape = drv) +
  guides(colour = guide_colourbar(order = 1),
         alpha = guide_legend(order = 2),
         size = guide_legend(order = 3))
image.png

3.4 刪除具有特殊美感的圖例

p+guides(color = FALSE, size = FALSE)
image.png
# Remove legend for the point shape
p1 <- p+scale_shape(guide=FALSE)
# Remove legend for size
p2 <- p +scale_size(guide=FALSE)
# Remove legend for color
p3 <- p + scale_color_manual(values=c('#999999','#E69F00','#56B4E9'),
                       guide=FALSE)
ggarrange(p1,p2,p3,nrow = 1)
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,702評論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,615評論 3 419
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,606評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,044評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,826評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,227評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,307評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,447評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,992評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,807評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,001評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,550評論 5 361
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,243評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,667評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,930評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,709評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,996評論 2 374