R語言繪圖系列:
- R語言可視化及作圖1--基礎(chǔ)繪圖(par函數(shù)邪媳,散點圖,盒形圖,條形圖雨效,直方圖)
- R語言可視化及作圖2--低級繪圖函數(shù)
- R語言可視化及作圖3--圖形顏色選取
- R語言可視化及作圖4--qplot和ggplot2美學(xué)函數(shù)
- R語言可視化及作圖5--ggplot2基本要素和幾何對象匯總
- R語言可視化及作圖6--ggplot2之點圖迅涮、條形圖、盒形圖徽龟、直方圖叮姑、線圖
1. 標(biāo)簽繪制
library(ggplot2)
head(mtcars)
p <- ggplot(mtcars,aes(wt,mpg,label=rownames(mtcars)))
p+geom_text() #使用geom_text繪制標(biāo)簽散點圖
使用geom_label繪制標(biāo)簽散點圖
p+geom_label()
繪制點,并通過nudge參數(shù)對標(biāo)簽進行x軸和y軸上的平移
p+geom_point(color='dodgerblue')+geom_text(nudge_x = 0.15,nudge_y = -1)
#所有標(biāo)簽整體向右平移0.15据悔,向下平移1
使用angle參數(shù)對標(biāo)簽角度進行設(shè)置
p+geom_point(color='dodgerblue')+geom_text(nudge_x = 0.1,angle=45)
geom_label可以使用fill對顏色進行填充,fontface設(shè)置字體传透,geom_text不能填充顏色
p+geom_label(aes(fill=factor(cyl)),color='white',fontface='bold',family='Times New Roman')
parse參數(shù)意思是前面?zhèn)魅氲氖且粋€數(shù)學(xué)表達式,size定義標(biāo)簽相對大小极颓。
x <- 1:8
df <- data.frame(x=1:8,y=1.2+x^2)
ggplot(df,aes(x,y))+geom_point()+geom_smooth()+geom_text(aes(x=4,y=40),label='y==1.2+x^2',parse = TRUE,size=7)
#如果parse=FAKSE,圖形上顯示的就直接是y == 1.2 + x^2朱盐,而不是圖上的公式。
畫一個散點圖
p <- ggplot(mtcars,aes(x=wt,y=mpg))+geom_point()
p
annotate函數(shù)傳入標(biāo)簽
p+annotate('text',x=4,y=25,label='I love R', size=5,color='forest green',family='Times New Roman')
添加矩形
a= p+annotate('rect',xmin=3,xmax=4.2,ymin=12,ymax=21,alpha=.2,fill='forest green')
a
添加短線段
b=p+annotate('segment',x=2.5,xend=4,y=15,yend=25,color='blue')
b
c=p+annotate('pointrange',x=3.5,y=20,ymin=12,ymax=28,color='red',size=1.5)
c
2. 圖例繪制
2.1 guide_legend函數(shù)(主要參數(shù):color, shape, size)
圖例調(diào)整函數(shù)也屬于標(biāo)度函數(shù)的一類菠隆,但不可以直接使用加號來連接兵琳,必須放在函數(shù)中,作為一個參數(shù)骇径。
df <- data.frame(x=1:20,y=1:20,color=letters[1:20])
p <- ggplot(df,aes(x,y))+geom_point(aes(color=color))
p+guide_legend(title = 'legend',nrow=4,ncol=5)
#Error: Can't add `guide_legend(title = "legend", nrow = 4, ncol = 5)` to a ggplot object.
#正確方法:
p+guides(color=guide_legend('my legend',ncol=5,nrow=4,label.position = 'left'))
dat <- data.frame(x=1:5,y=1:5,p=1:5,q=factor(1:5),r=factor(1:5))
pp <- ggplot(dat,aes(x,y,color=p,size=q,shape=r))+geom_point()
#畫出散點圖躯肌,在不對圖例進行任何調(diào)整的情況下,圖形有測也會出現(xiàn)三個圖例破衔,分別是color,size,shape
pp+guides(color='colorbar',size='none',shape='legend')
pp+guides(color=guide_colorbar('color'),shape=guide_legend('shape',ncol=5))
guide_colorbar和guide_legend設(shè)置的是不同的圖例清女,guide_colorbar定義色條圖例,guide_legend定義普通圖例运敢。
pp+guides(color=guide_legend('title'),size=guide_legend('title'),shape=guide_legend('title'))
#三個分類變量都是一個圖例校仑,包含了顏色圖形和大小
ggplot(mpg,aes(displ,cty))+geom_point(aes(size=hwy,color=cyl,shape=drv))+guides(color=guide_colorbar(order = 2),shape=guide_legend(order = 3),size=guide_legend(order=1))
#order參數(shù)接受一個小于等于99的值
2.2 標(biāo)度函數(shù)scale
對于連續(xù)型變量,使用的參數(shù)是scale_xxx_continous()传惠,對于分類型變量迄沫,使用的是scale_xxx_discrete()。
pp+scale_color_continuous(guide='colorbar')+
scale_size_discrete(guide='legend')+
scale_shape(guide='legend')
pp+scale_color_continuous(guide=guide_colorbar('color'))+
scale_shape(guide=guide_legend('shape',ncol = 5))+
scale_size_discrete(guide=guide_legend('size',ncol=5))
2.3:theme函數(shù)
pt <- ggplot(mtcars,aes(mpg,wt,color=factor(cyl)))+geom_point()
pt+scale_color_discrete(name='cyl')+
#由于theme()函數(shù)無法定義圖例的標(biāo)題卦方,因此事先用scale函數(shù)定義
theme(legend.title = element_text(color = 'blue',family = 'Times New Roman'),
legend.background = element_rect(color='red',linetype = 2))
pt+scale_color_discrete(name='cyl')+
theme(legend.position = 'bottom', #將圖例放置在圖片底部
legend.text=element_text(color = 'red',size=13,angle=45),
#設(shè)置圖例中圖標(biāo)的標(biāo)簽羊瘩,顏色為紅色,字號為13盼砍,并呈45度角放置尘吗。
legend.key = element_rect(color='black',fill = 'orange'),
#設(shè)置每一個圖標(biāo)的背景,此處邊框色設(shè)置為黑色浇坐,背景填充色為橘黃色睬捶。legend.key就是圖標(biāo)
legend.key.height = unit(1,'cm'),
legend.key.width = unit(1,'cm'))
#設(shè)置圖標(biāo)的大小,注意此處傳入的是unit()近刘。unit()用于設(shè)置傳入的參數(shù)的單位擒贸。
在theme函數(shù)中臀晃,與圖例有關(guān)的主要參數(shù)有:
參數(shù) | 用法 | 功能 |
---|---|---|
legend.background | 接受函數(shù)element_rect() | 定義圖例背景 |
legend.margin | 接受數(shù)值 | 定義圖例的邊緣范圍 |
legend.key | 接受函數(shù)element_rect() | 定義圖例中每一個小圖標(biāo)的背景 |
legend.key.size | 接受unit() | 定義圖例中每一個小圖標(biāo)的大小 |
legend.key.height/width | 接受unit() | 定義圖例中每一個小圖標(biāo)的背景大小 |
legend.text | 接受函數(shù)element_text() | 定義圖例中每一個圖標(biāo)的標(biāo)簽 |
legend.text.align | 取值0-1,0表示左邊介劫,1表示右邊 | 定義圖例標(biāo)簽對齊方式 |
legend.title | 接受函數(shù)element_text() | 定義圖例標(biāo)題樣式徽惋,但是無法定義標(biāo)題是什么 |
legend.position | 接受字符串:“none”, “l(fā)eft”座韵, “right”险绘, “bottom”, “top”誉碴;或者接受一個表示坐標(biāo)的數(shù)值向量 | 定義圖例出現(xiàn)的位置 |
legend.direction | 接受字符串 | 定義圖例中圖標(biāo)的排列方式 |
legend.box | 接受字符串: “horizontal”或“vertical” | 定義多個圖例的排列方式 |
3. 標(biāo)題繪制
標(biāo)題主要有五種:主標(biāo)題宦棺,副標(biāo)題,角注翔烁,x軸標(biāo)簽和y軸標(biāo)簽
p <- ggplot(mtcars,aes(mpg,wt,color=factor(cyl)))+geom_point()
p+ggtitle(label = 'New plot title',subtitle = 'subtitle')+
labs(color='legend')+
xlab('new x label')+
ylab('new y label')+
labs(caption = '(based on mtcars data)')
ggtitle()只能定義標(biāo)題和副標(biāo)題渺氧,默認(rèn)的位置在左上角。
p+labs(title = 'New plot title')+
labs(caption = '(based on mtcars data)')+
theme(plot.title = element_text(color = 'red',size = 9,hjust = 0.5),
#默認(rèn)的標(biāo)題位置出現(xiàn)在左上方蹬屹,通過hjust參數(shù)進行調(diào)整侣背,該參數(shù)接受一個0-1之間的數(shù)值,0表示最左側(cè)慨默,1表示最右側(cè)贩耐。
plot.caption = element_text(color = 'blue'))