免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

怎么使用ggplot2實現玫瑰氣泡圖

這篇文章主要介紹“怎么使用ggplot2實現玫瑰氣泡圖”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“怎么使用ggplot2實現玫瑰氣泡圖”文章能幫助大家解決問題。

目前成都創新互聯公司已為上1000+的企業提供了網站建設、域名、網絡空間、網站改版維護、企業網站設計、阜新網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。

圖表整體難度中等,使用ggplot2來寫代碼,主要考察關于極坐標轉換與應用。(ggplot2系統中,兩個較為高階的用法分別為:極坐標系、地理空間的多邊形填充)。

#加載包:

library("ggplot2")

library("tidyr")

library("ggthemes")

library("dplyr")

library("Cairo")

library("showtext")

#載入字體:

font.add("myfontl","msyhl.ttc")

font.add("myfont","msyh.ttc")

font.add("myfzhzh","方正正粗黑簡體.TTF")

#鎖定文件臨時目錄:

setwd("D:/R/File")

#導入數據

circle_bubble<-read.csv("circle_bubble.csv",stringsAsFactors = FALSE,check.names = FALSE)

仔細觀察該圖,你會發現,玫瑰圖內側并非底邊對齊,而是靠上下中心對齊,要想做到這種效果,必須將一個 細分維度轉化為負值(柱形圖堆積時會自動將負值堆積在負坐標軸上)。

circle_bubble$Government<--circle_bubble$Government

level<-circle_bubble[,"State"]

circle_bubble$State<-factor(circle_bubble$State,levels=level,ordered=TRUE)

怎么使用ggplot2實現玫瑰氣泡圖

篩選出制作堆積條形圖的數據源:

circle_data<-circle_bubble %>% select(State,Government,Family,Youself) %>% gather(Class,Value,-State)

circle_data$Class<-factor(circle_data$Class,levels=c("Government","Youself","Family"),ordered=TRUE)

怎么使用ggplot2實現玫瑰氣泡圖

#構造三個序列的氣泡圖數據:

circle_point<-circle_bubble[,c("State","General","Very","Raito")]

circle_point_data<-gather(circle_point,Class,Value,-State)

circle_point_data$Class<-factor(circle_point_data$Class,levels=c("General","Very","Raito"),order=TRUE)

怎么使用ggplot2實現玫瑰氣泡圖

#構造氣泡圖模擬Y軸:

circle_point_data <- within(circle_point_data,{

  mood_y <- NA

  mood_y[Class=="General"]<--150

  mood_y[Class=="Very"]<--100

  mood_y[Class=="Raito"]<-140})

#構造模擬氣泡大小

circle_point_data$Value_y<-ifelse(circle_point_data$Class=="Raito",circle_point_data$Value,0.3*circle_point_data$Value)

(這里沒有使用氣泡圖對應真實值作為氣泡大小,而是將除Raito系列之外的兩個序列真實值縮小了70%,這么做的目的是為了整體的氣泡大小比例顯示更為勻稱和諧,因為Ratio序列是單值序列,其與自身的100%單位標度比較,剩余兩個序列相互比較)。

#構造模擬標簽位置

circle_data <- within(circle_data,{

  label_y <- NA

  label_y[Class=="Government"]<-circle_bubble$Government/2

  label_y[Class=="Family"]<-circle_bubble$Family/2

  label_y[Class=="Youself"]<-circle_bubble$Family+circle_bubble$Youself-circle_bubble$Youself/2})

#極坐標化前的柱形圖:

ggplot()+

geom_linerange(data=circle_bubble,aes(x=State,ymin=-150,ymax=140),size=.25,color="#D8E5F2",alpha=0.8)+

geom_col(data=circle_data,aes(x=State,y=Value,fill=Class),width=1,colour="white",size=.25)

怎么使用ggplot2實現玫瑰氣泡圖

###極坐標化前的柱形圖和氣泡圖

ggplot()+

geom_linerange(data=circle_bubble,aes(x=State,ymin=-150,ymax=140),size=.25,color="#D8E5F2",alpha=0.8)+

geom_col(data=circle_data,aes(x=State,y=Value,fill=Class),width=1,colour="white",size=.25)+

geom_point(data=circle_bubble,aes(State,y=140),size=40,colour="#FEFCD3",alpha=.8)+

geom_point(data=circle_point_data,aes(State,y=mood_y,size=Value_y,colour=Class))+

geom_text(data=circle_data,aes(x=State,y=label_y,group=Class,label=abs(Value)),family="myfont",colour="white",size=5)+

geom_text(data=filter(circle_point_data,mood_y==-150),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#AA1A66",size=4)+

geom_text(data=filter(circle_point_data,mood_y==-100),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#A31067",size=4)+

geom_text(data=filter(circle_point_data,mood_y==140),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#F29E4A",size=5)

怎么使用ggplot2實現玫瑰氣泡圖

#極坐標化后的柱形圖和氣泡圖(完整標簽版)

ggplot()+

geom_linerange(data=circle_bubble,aes(x=State,ymin=-150,ymax=140),size=.25,color="#D8E5F2",alpha=0.8)+

geom_col(data=circle_data,aes(x=State,y=Value,fill=Class),width=1,colour="white",size=.25)+

geom_point(data=circle_bubble,aes(State,y=140),size=40,colour="#FEFCD3",alpha=.8)+

geom_point(data=circle_point_data,aes(State,y=mood_y,size=Value_y,colour=Class))+

geom_text(data=circle_data,aes(x=State,y=label_y,group=Class,label=abs(Value)),family="myfont",colour="white",size=5)+

geom_text(data=filter(circle_point_data,mood_y==-150),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#AA1A66",size=4)+

geom_text(data=filter(circle_point_data,mood_y==-100),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#A31067",size=4)+

geom_text(data=filter(circle_point_data,mood_y==140),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#F29E4A",size=5)+

scale_fill_manual(values=c("#FA844F","#BDCCD4","#00C4B5"))+

scale_colour_manual(values=c("#FAC0A1","#F35C57","#FBEB1B"))+

labs(title="各國人民\n對于養老的態度")+

scale_size_area(max_size=34.8)+

coord_polar()

怎么使用ggplot2實現玫瑰氣泡圖

#最終成品圖:

CairoPNG(file="circle_bubble.png",width=1000,height=1000)

showtext.begin()

ggplot()+

geom_linerange(data=circle_bubble,aes(x=State,ymin=-150,ymax=140),size=.25,color="#D8E5F2",alpha=0.8)+

geom_col(data=circle_data,aes(x=State,y=Value,fill=Class),width=1,colour="white",size=.25)+

geom_point(data=circle_bubble,aes(State,y=140),size=40,colour="#FEFCD3",alpha=.8)+

geom_point(data=circle_point_data,aes(State,y=mood_y,size=Value_y,colour=Class))+

geom_text(data=circle_data,aes(x=State,y=label_y,group=Class,label=abs(Value)),family="myfont",colour="white",size=5)+

geom_text(data=filter(circle_point_data,mood_y==-150),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#AA1A66",size=4)+

geom_text(data=filter(circle_point_data,mood_y==-100),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#A31067",size=4)+

geom_text(data=filter(circle_point_data,mood_y==140),aes(x=State,y=mood_y,label=Value),family="myfontl",colour="#F29E4A",size=5)+

scale_fill_manual(values=c("#FA844F","#BDCCD4","#00C4B5"))+

scale_colour_manual(values=c("#FAC0A1","#F35C57","#FBEB1B"))+

labs(title="各國人民\n對于養老的態度")+

scale_size_area(max_size=34.8)+

coord_polar()+

ylim(-250,150)+

guides(colour=FALSE,size=FALSE,fill=FALSE)+

theme_map() %+replace%theme(plot.title=element_text(family="myfzhzh",size=50,hjust=0,lineheight=1.2))

showtext.end()

dev.off()

怎么使用ggplot2實現玫瑰氣泡圖

關于“怎么使用ggplot2實現玫瑰氣泡圖”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注創新互聯行業資訊頻道,小編每天都會為大家更新不同的知識點。

文章名稱:怎么使用ggplot2實現玫瑰氣泡圖
本文來源:http://m.newbst.com/article24/gohdje.html

成都網站建設公司_創新互聯,為您提供網站設計外貿建站自適應網站網站導航網站改版移動網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

小程序開發