其他
R绘图模板——花瓣图!
点击上方
“科研后花园”
关注我们
代码如下:
1、生成随机数据用于作图:
rm(list = ls())
# #生成数据或者自己导入数据
# # df <- read.table("xxx.txt",header = T, row.names = 1, check.names = F)
df <- data.frame(x=LETTERS[1:10],y=sample(10:20,10))
2、加载包
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(tidyverse) # Easily Install and Load the 'Tidyverse'
library(RColorBrewer) # ColorBrewer Palettes
library(grid) # The Grid Graphics Package
library(scales) # Scale Functions for Visualization
3、数据处理
### 通过构造正余弦函数使得极坐标的圆弧成为花瓣状
x<-1:180
y<-sin(10*x*pi/180)
df1<-data.frame(x1=x,y1=abs(y),var=gl(10,18,labels = LETTERS[1:10]))
merge(df1,df,by.x = 'var',by.y = 'x') %>%
mutate(new_y=y1*y) -> df2
4、准备配色
#准备配色
col <- colorRampPalette(brewer.pal(12,"Paired"))(10)
#背景色
color <- colorRampPalette(brewer.pal(11,"PuOr"))(30)
5、绘图
ggplot(data=df2,aes(x=x,y=new_y))+
geom_area(aes(fill=var),
alpha=0.8,
color="black",
show.legend = T)+
coord_polar()+
theme_bw()+
theme(axis.text= element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.title = element_blank(),
legend.position = c(0.95,0.9),
legend.justification = c(1, 1),
legend.direction = 'vertical')+
scale_x_continuous(breaks = seq(9,180,18),
labels = df$x)+
geom_text(data=df,aes(x=seq(9,180,18),
y=y+1,
label=y))+
scale_fill_manual(values = col)
#添加背景
grid.raster(alpha(color, 0.2),
width = unit(1, "npc"),
height = unit(1,"npc"),
interpolate = T)
温馨提示
如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。