之前有人在公众号留言问文章开头这幅图如何实现,下面的B图是折线图加柱形图,相对比较容易实现,上面的A图稍微有点复杂,我想到的办法是拼图,图A可以看成三个热图,然后加一个堆积柱形图,最后将四个图组合到一起。那就按照这个思路试一下看能不能实现。
最初的想法是左侧的颜色条用堆积柱形图来实现,又看了一遍Y叔公众号关于
aplot
这个包的推文,发现他是用geom_tile()
函数实现的,仔细想想还是geom_tile()
函数实现起来比较方便。
非常感谢下面这位的留言
文本添加下划线的小例子
df<-data.frame(A=1:10,
B=1:10)
library(ggplot2)
ggplot(df,aes(A,B))+
geom_point(size=5)+
theme_minimal()+
ggtitle(expression(underline("Good Good Study, Day Day Up")))+
labs(x=expression(paste(italic("ABC"),"123")))
如何画这个热图昨天的推文已经介绍过了,点击下方蓝色字可以直达昨天的推文
df2<-read.csv("example_data/ggplot2_heatmap_color_bar.csv",header=T)
df2$y<-factor(df2$y,levels = rev(df2$y))
ggplot(df2,aes(x=x,y=y))+
geom_tile(aes(fill=group))+
scale_x_continuous(expand = c(0,0))+
theme(panel.background = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
legend.position = "left",
legend.title = element_blank())+
scale_fill_manual(values = c("green","blue","red"))
library(aplot)
library(ggplot2)
df<-read.csv("example_data/ggplot2_heatmap.csv",header=T)
df1<-reshape2::melt(df)
df1$value1<-ifelse(is.na(df1$value),0,df1$value)
df1$value1<-as.factor(df1$value1)
df1$A<-factor(df1$A,levels = rev(df$A))
p1<-ggplot(df1,aes(x=variable,y=A))+
geom_tile(aes(fill=value1),color="black")+
scale_fill_manual(values = c("white","orangered","red2","red4"))+
scale_x_discrete(position = "top")+
theme(panel.background = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text.x.top = element_text(angle = 90,
hjust = 0,
vjust= 0.5),
plot.title = element_text(hjust=0.5),
legend.position = "none")+
labs(title = expression(underline("Plant growth and development")))+
geom_text(aes(label=value1,color=value1))+
scale_color_manual(values = c("white","black","black","black"))
df2<-read.csv("example_data/ggplot2_heatmap_color_bar.csv",header=T)
df2$y<-factor(df2$y,levels = rev(df2$y))
p2<-ggplot(df2,aes(x=x,y=y))+
geom_tile(aes(fill=group))+
scale_x_continuous(expand = c(0,0))+
theme(panel.background = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
legend.position = "left",
legend.title = element_blank())+
scale_fill_manual(values = c("green","blue","red"))
p1%>%
insert_left(p2,width = 0.05)
这里遇到一个问题是如何将右侧的图例放大左上角去?用代码如何实现我暂时还不知道,出图以后手动编辑吧!
p3<-p1+
theme(axis.text.y = element_blank())
pdf(file = "123.pdf",width = 12)
p1%>%
insert_left(p2,width = 0.05)%>%
insert_right(p3)%>%
insert_right(p3)
dev.off()
这样文章开头提到的图就做好了