在R中的堆叠条形图上分层散点图

随机的

我正在尝试在R中的堆叠条形图上分层放置散点图(带有误差条),以用作热图样式回填,以直观地显示这些点属于哪些类别。我已经成功地构建了这两个组件,但是似乎无法成功地将它们组合在一起,或者找不到可以借鉴的类似示例。

编辑添加:在将代码分解为两个图的ggplot()+ geom结构并删除误差线后,我可以将两个图都放在同一张图上,但是比例尺已关闭(尽管两者的ymax = 35)并且我不能让它们重叠。

     ##library/packages

library(reshape2)
library(ggplot2)
library(forcats) #forcats package
library(scales)
library(plyr)
library(ggplot2)

#Data for point graph:
df<-data.frame(Location=c("Location1","Location2", "Location3"), WALL=c(3.5,1.6,30), NRPK=c(5.6,1.0,21), WALL_CL_L=c(3.2,1.5,27),
               WALL_CL_U=c(3.8,2.0,32), NRPK_CL_L=c(5.0,0.05,19.3), NRPK_CL_U=c(6.1,1.2,23.5))
xWALL<-subset(df, select=c("Location","WALL","WALL_CL_L","WALL_CL_U"))

#Data for bar graph:
dat <- read.table(text = "     FSI_Scale
                  1   6
                  2   9
                  3   7
                  4   8
                  5   5",sep = "",header = TRUE)

datm <- melt(cbind(dat, ind = rownames(dat)), id.vars = c('ind'))

MyColours<-c('green3','green2','yellow1','orange1','red')

Basic<- ggplot() +
  geom_point(data=xWALL, aes(x=Location, y=WALL), size=2, shape=23, color="black", fill="cornflowerblue") +
  geom_bar(data=datm, aes(x=variable, y=value, fill=forcats::fct_rev(ind)), stat = "identity", position = "fill", width = 1) + scale_fill_manual(values = MyColours)+
  theme(axis.title=element_blank(), axis.text=element_blank(),axis.ticks=element_blank())+
  guides(fill=FALSE)+
  scale_y_continuous(limits=c(0,35))

结果是这样的:barandpoint

非常感谢您提供的任何帮助。

随机的

我最终回答了自己的问题。必要的步骤是制作一个非常基本的热图(而不是条形图)并覆盖这些点:

library(RColorBrewer)
library(ggplot2)
#heat map dataframe
d<-data.frame(x=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2), y=rep(0:25,2), z=c(1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5), w=rep(2,52))
#data point dataframe
pd<-data.frame(Loc=c(1,2), var=c(11,17))
#colour palette for heat map
colfunc<-colorRampPalette(c("red","darkorange","yellow1","springgreen","springgreen3"))

ggplot() +
  geom_tile(data=d, aes(x,y, fill = z), show.legend=FALSE)  +
  scale_fill_gradientn(colours = colfunc(5)) +
  scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(breaks=pretty(d$y, n=10),expand = c(0, 0)) + #point after heatmap or it will be covered
  geom_point(data=pd,aes(Loc, var))+
  theme_bw() +
  theme(axis.title.x =element_blank(),
        axis.title.y=element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.border = element_blank(),
        panel.background = element_blank()) 

这样可以调整热图,以根据需要显示不同的阈值,并显示数据相对于它们的下降位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章