I'm attempting to layer a scatter plot (with error bars) over a stacked bar plot in R which is intended as a heat map style backfill, to visually show which categories the points fall into. I've built both components successfully, but can't seem to combine them successfully, or to find a similar example to draw from.
Edited to add: That after stripping the code down to a ggplot()+ geom structure for both graphs and removing the error bars I can get both on the same graph, but the scale is off (though both have a ymax=35) and I can not get them to overlap.
##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))
The result is this:barandpoint
Thank you very much for any help you can provide.