R语言ggplot2:在一幅图中插入另外一幅图

小明的数据分析笔记本

2022-07-30

这个是公众号 小明的数据分析笔记本 2019年9月14号推文

代码

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
df<-data.frame(x=LETTERS[1:10],y=10:1)
df
##    x  y
## 1  A 10
## 2  B  9
## 3  C  8
## 4  D  7
## 5  E  6
## 6  F  5
## 7  G  4
## 8  H  3
## 9  I  2
## 10 J  1
p1<-ggplot()+
  geom_bar(data=df,aes(x=x,y=y,fill=x),
           stat="identity")+theme_bw()+
  theme(legend.position = "none")

p2<-ggplot()+
  geom_bar(data=df[1:5,],aes(x="",y=y,fill=x),
           stat="identity")+theme_bw()+
  coord_polar("y",start=0)+
  theme_bw()+
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        panel.grid = element_blank())+
  scale_fill_brewer(palette="Set1")
g<-ggplotGrob(p2)
p1+annotation_custom(g,xmin=5,xmax=12,ymin=5,ymax=10)

欢迎大家关注我的公众号

小明的数据分析笔记本