ggplot2繪制圓形熱圖

常見的熱圖看膩了,這節(jié)來介紹如何通過ggplot2繪制圓形熱圖,為了方便各位觀眾老爺觀看,我制作了一個交互式文檔
https://mp.weixin.qq.com/s/lbTqk8ZlLp6A-VX-MYLgFA

加載R包

library(tidyverse)
library(ggthemes)
library(cowplot)

讀入數(shù)據(jù)

accidents <- read.csv("accidents_per_month.txt") %>% 
  mutate(Year=as.numeric(Year)) %>% as_tibble()
> accidents
# A tibble: 156 x 3
    Year Deaths Month
   <dbl>  <int> <int>
 1  2007    126     1
 2  2007    105     2
 3  2007    133     3
 4  2007    137     4
 5  2007    128     5
 6  2007    108     6
 7  2007    170     7
 8  2007    143     8
 9  2007    146     9
10  2007    145    10

數(shù)據(jù)可視化

先畫一張常見的熱圖

ggplot(accidents, aes(x=Month, y=Year, fill=Deaths)) +
  geom_tile(colour="white") +
  scale_fill_gradient(low = "white", high = "#c85c32") -> p1

p1
image

更改刻度范圍

p1 + xlim(-11,13) + ylim(2000,2020) -> p2

p2
image

轉(zhuǎn)換為極坐標(biāo)

p2 + coord_polar(theta = "x", start = pi/3) -> p3

p3
image

修改主題

 p3 + theme(panel.background=element_rect(fill = "white"),
        axis.title=element_blank(),
        panel.grid=element_blank(),
        axis.text.x = element_blank(),
        axis.ticks=element_blank(),
        axis.text.y=element_blank(),
        legend.position = c(0.48,0.52),
        legend.direction = "vertical",
        legend.background = element_blank(),
        legend.text = element_text(size = 8)) -> p4

p4
image

geom_text添加文本

p4 + geom_text(aes(x=12.5, y=Year, label=Year),hjust=-0.2,
            size=2.5, angle= -53,
            inherit.aes = FALSE) +
  geom_text(aes(x=1, y=2020, label="1"),
            hjust=0.5, vjust = 0,size=3,
            angle= 120, inherit.aes = FALSE) + 
  geom_text(aes(x=11, y=2020, label="11"),
            hjust=0.5, vjust = 0,size=3,
            angle=-30, inherit.aes = FALSE)+
  geom_text(aes(x=12, y=2020, label="12"),hjust=0.5, vjust = 0,
            size=3, angle= -44, inherit.aes = FALSE )+
  geom_text(aes(x=10, y=2020, label="10"),
            hjust=0.5, vjust = 0,size=3,
            angle=-18, inherit.aes = FALSE) + 
  geom_text(aes(x=9, y=2020, label="9"),
            hjust=0.5, vjust = 0,size=3,
            angle=5, inherit.aes = FALSE)+
  geom_text(aes(x=8, y=2020, label="8"),hjust=0.5, vjust = 0,
            size=3, angle= 18, inherit.aes = FALSE )+
  geom_text(aes(x=7, y=2020, label="7"),
            hjust=0.5, vjust = 0,size=3,
            angle=31, inherit.aes = FALSE) + 
  geom_text(aes(x=6, y=2020, label="6"),
            hjust=0.5, vjust = 0,size=3,
            angle=44, inherit.aes = FALSE)+
  geom_text(aes(x=5, y=2020, label="5"),hjust=0.5, vjust = 0,
            size=3, angle= 70, inherit.aes = FALSE )+
  geom_text(aes(x=4, y=2020, label="4"),hjust=0.5, vjust = 0,
            size=3, angle= 80, inherit.aes = FALSE )+
  geom_text(aes(x=3, y=2020, label="3"),hjust=0.5, vjust = 0,
            size=3, angle= 90, inherit.aes = FALSE )+
  geom_text(aes(x=2, y=2020, label="2"),hjust=0.5, vjust = 0,
            size=3, angle= 110, inherit.aes = FALSE )+
  guides(fill = guide_colourbar(barheight = 2.5, barwidth = 1,
  ticks= F,title.position = "left",
  title.theme = element_text(size = 12, angle = 90),
  label.theme = element_text(size = 7)))
image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開了第一次的黨會,身份的轉(zhuǎn)變要...
    余生動聽閱讀 10,912評論 0 11
  • 彩排完,天已黑
    劉凱書法閱讀 4,497評論 1 3
  • 表情是什么,我認(rèn)為表情就是表現(xiàn)出來的情緒。表情可以傳達(dá)很多信息。高興了當(dāng)然就笑了,難過就哭了。兩者是相互影響密不可...
    Persistenc_6aea閱讀 129,859評論 2 7

友情鏈接更多精彩內(nèi)容