0.数据和R包
dat = data.frame(Group = paste0("A",1:10),
value = runif(10))
dat
## Group value
## 1 A1 0.6278805
## 2 A2 0.7196941
## 3 A3 0.9113772
## 4 A4 0.3292087
## 5 A5 0.5395085
## 6 A6 0.8165624
## 7 A7 0.1775485
## 8 A8 0.8311558
## 9 A9 0.2654154
## 10 A10 0.5712543
#install.packages("tidyverse")
library(tidyverse)
1.基础的环状barplot
min_break = floor(min(dat$value))
max_break = ceiling(max(dat$value))
mid_break = mean(c(min_break,max_break))
p = ggplot(dat) +
geom_hline(dat = data.frame(y = c(min_break,mid_break,max_break)),
aes(yintercept = y),color = "lightgrey") +
geom_col(aes(x = Group,y = value,fill = value),
position = "dodge2",show.legend = TRUE,alpha = .9) +
geom_segment(aes(x = Group,y = min_break,xend = reorder(Group, value),yend = max_break),
linetype = "dashed",color = "gray12") +
coord_polar()+
theme_bw()
p
基础图由自定义的水平和垂直线、直方图组成。是ggplot2画滴
2.可以按照值的大小排个顺序
p2 = ggplot(dat) +
geom_hline(data = data.frame(y = c(min_break,mid_break,max_break)),
aes(yintercept = y),color = "lightgrey") +
geom_col(aes(x = reorder(Group, value),y = value,fill = value),
position = "dodge2",show.legend = TRUE,alpha = .9) +
geom_segment(aes(x = reorder(Group, value),y = min_break,xend = reorder(Group, value),yend = max_break),
linetype = "dashed",color = "gray12") +
coord_polar()+
theme_bw()
p2
3.细节调整
接下来是修改配色、刻度位置、图例等细节。
k = nrow(dat)+0.7
p2 +
annotate(x = k, y = min_break, label = min_break, geom = "text", color = "gray12") +
annotate(x = k, y = mid_break, label = mid_break, geom = "text", color = "gray12") +
annotate(x = k, y = max_break, label = max_break, geom = "text", color = "gray12") +
scale_y_continuous(limits = c(-mid_break, max_break),
breaks = c(0, min_break, mid_break, max_break),expand = c(0, 0)) +
scale_fill_gradientn("Amount",colours = c( "#6C5B7B","#C06C84","#F67280","#F8B195")) +
guides(fill = guide_colorsteps(barwidth = 15, barheight = .5, title.position = "top", title.hjust = .5)) +
theme(axis.title = element_blank(),axis.ticks = element_blank(),
axis.text.y = element_blank(),legend.position = "bottom")
中间的空白圈圈是通过y轴的limits调整来实现的。
齐活儿~