R語言與醫學統計圖形-【23】ggplot2坐標系轉換函數


ggplot2繪圖系統——坐標系轉換函數

包括餅圖、環狀條圖、玫瑰圖、戒指圖、坐標翻轉。

  • 笛卡爾坐標系(最常見)。
  • ArcGIS地理坐標系(地圖)。
  • Cartesian坐標系。
  • polar極坐標系。

利用ploar坐標系繪圖

coord_polar函數及參數:

coord_polar(theta = 'x', #x/y
            start = 0, #0-12,起始點,對應時鍾刻度
            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
去掉餅圖中心的空白,只需將條形圖的標准寬度設為1。還需去掉極坐標刻度、標簽等多余的顏色。

a=ggplot(data = subset(diamonds,color=="E"),aes(factor('E'),fill=cut))+
  geom_bar(width = 1)+ #設標准寬度
  coord_polar(theta = 'y')

b=a+theme(axis.text = element_blank(), #去刻度標簽
          axis.title = element_blank(), #去標題
          axis.ticks = element_blank(), #去刻度
          panel.background = element_blank(), #去背景
          panel.grid = element_blank()) #去網格線

grid.arrange(a,b,ncol=2)

image.png

2. 環形條圖

示例比較下。

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
細節的修飾。

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))+
  #添加條柱標簽
  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)+#為更好找到注釋的橫坐標
  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, #后續會講更方便的位置參數
           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))+ #戒指粗細
  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'))+ #戒指環每部分添加文字
  labs(title = '')+
  theme(legend.position = 'none')

image.png

坐標軸翻轉

coord_flip函數,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刻度翻轉,僅適用連續型變量

grid.arrange(a,b,ncol=2)

image.png

若不翻轉x軸,如下所示:
image.png


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM