ggplot2繪圖系統(tǒng)——坐標(biāo)系轉(zhuǎn)換函數(shù)
包括餅圖、環(huán)狀條圖蜻势、玫瑰圖、戒指圖鹉胖、坐標(biāo)翻轉(zhuǎn)握玛。
- 笛卡爾坐標(biāo)系(最常見)。
- ArcGIS地理坐標(biāo)系(地圖)甫菠。
- Cartesian坐標(biāo)系挠铲。
- polar極坐標(biāo)系。
利用ploar坐標(biāo)系繪圖
coord_polar
函數(shù)及參數(shù):
coord_polar(theta = 'x', #x/y
start = 0, #0-12淑蔚,起始點市殷,對應(yīng)時鐘刻度
direction = 1) #1/-1,順時針/逆時針
1. 餅圖
#餅圖
a <- ggplot(data = subset(diamonds,color=="E"),aes(factor('E'),
fill=cut))+geom_bar()
b <- ggplot(data = subset(diamonds,color=="E"),aes(factor('E'),
fill=cut))+geom_bar()+
coord_polar()
c <- ggplot(data = subset(diamonds,color=="E"),aes(factor('E'),
fill=cut))+geom_bar()+
coord_polar(theta = 'y')
grid.arrange(a,b,c,ncol=3)
image.png
去掉餅圖中心的空白刹衫,只需將條形圖的標(biāo)準(zhǔn)寬度設(shè)為1醋寝。還需去掉極坐標(biāo)刻度、標(biāo)簽等多余的顏色带迟。
a=ggplot(data = subset(diamonds,color=="E"),aes(factor('E'),fill=cut))+
geom_bar(width = 1)+ #設(shè)標(biāo)準(zhǔn)寬度
coord_polar(theta = 'y')
b=a+theme(axis.text = element_blank(), #去刻度標(biāo)簽
axis.title = element_blank(), #去標(biāo)題
axis.ticks = element_blank(), #去刻度
panel.background = element_blank(), #去背景
panel.grid = element_blank()) #去網(wǎng)格線
grid.arrange(a,b,ncol=2)
image.png
2. 環(huán)形條圖
示例比較下音羞。
a <- ggplot(diamonds,aes(cut))+
geom_bar(width = 1,fill='deeppink1',color='black')
b <- a+coord_polar(theta = 'y')
grid.arrange(a,b,ncol=2)
image.png
細(xì)節(jié)的修飾。
data=data.frame(group=c("A","B","C","D"),
value=c(33,62,56,67))
ggplot(data,aes(x=group,y=value,fill=group))+
geom_bar(width = 0.85,stat = 'identity')+
coord_polar(theta = 'y')+
labs(x='',y='')+
ylim(c(0,75))+
#添加條柱標(biāo)簽
geom_text(hjust=1,size=3,aes(x=group,y=0,
label=group,color=group))+
theme(legend.position = 'none',
axis.text.y=element_blank(),
axis.ticks = element_blank())
image.png
3. 南丁格爾玫瑰圖和戒指圖
玫瑰圖
dsmall <- diamonds[sample(nrow(diamonds),1000),]
ggplot(dsmall,aes(color,fill=cut))+
geom_bar(width = 0.9)+ #使玫瑰圖之間留下空隙
scale_fill_brewer(palette = 'Oranges')+
coord_polar(start = 1)+#為更好找到注釋的橫坐標(biāo)
theme(axis.title = element_blank(),
panel.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())+
annotate('text',label=levels(dsmall$color),
x=1:7,y=plyr::count(dsmall,vars = 'color')[,2]+2, #后續(xù)會講更方便的位置參數(shù)
fontface='bold')
image.png
戒指圖
#戒指圖
dat=data.frame(count=c(10,60,30),category=c('A',"B","C"))
dat$fraction=dat$count/sum(dat$count)
dat=dat[order(dat$fraction),]
dat$ymax=cumsum(dat$fraction)
dat$ymin=c(0,head(dat$ymax,n=-1))
ggplot(dat,aes(fill=category,ymax=ymax,ymin=ymin,
xmax=5,xmin=3))+ #戒指粗細(xì)
geom_rect()+
coord_polar(theta = 'y')+
xlim(c(0,5))+ #此范圍要包含(xmin,xmax)
theme(panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank())+
annotate("text",x=0,y=0,label='Ring plot',
color='forestgreen',fontface='bold')+
annotate("text",x=c(4,4,4),y=c(0.05,0.25,0.7),
label=c('A','C','B'))+ #戒指環(huán)每部分添加文字
labs(title = '')+
theme(legend.position = 'none')
image.png
坐標(biāo)軸翻轉(zhuǎn)
coord_flip
函數(shù)仓犬,x和y軸互換嗅绰。
a <- ggplot(dsmall,aes(color,price))+
geom_boxplot(fill='darkgreen')+
coord_flip()
b <- ggplot(dsmall,aes(carat))+
geom_histogram(fill='hotpink',color='black')+
coord_flip()+
scale_x_reverse() #將x刻度翻轉(zhuǎn),僅適用連續(xù)型變量
grid.arrange(a,b,ncol=2)
image.png
若不翻轉(zhuǎn)x軸搀继,如下所示:
image.png