I'm trying to create a figure with both a side-by-side bar plot and line plot using 2 datasets. Slight difference in my question versus another post, I have the figure created but my side-by-side bar chart is gray for both categories and the outline of the bars are the colors I needed filled in. For now, I've created the mycolors variable which I'm not even sure if I need... I have a couple other pieces of code in this figure that I found here so not sure if maybe they're affecting one another (i.e. the coefficient creation and using said values to create a secondary axis which I understand may not be best practice but that's what they want so it's what they're getting..)
Some other info: Dataset p is a long dataset containing a 'count' variable that has two levels (type1 and type2) and a 'value' for each count type by year. The bar chart should be split by type1 and 2. Dataset q is one measure by year and creates the line. I've included the code and sample data below. Additionally, I tried using fill=count versus colour=count but R gives me the following error
'Caused by error in FUN(): ! object 'count' not found'
NOTE: I've provided an output of the figure. The code below is modified to be an example so it won't output the figure needed if directly copy/pasted. I pivot dataset p to a long dataset in order to use it as a side-by-side plot.
Dataset p
Year<-c(2012,2013,2014,2015,2016)
Count<-c("type1","type2")
Value<-c(1,5,4,9,7,11,19,16,2,14,1,4)
Dataset q
Year<-c(2012,2013,2014,2015,2016)
proportion<-c(0.5,0.2,0.4,0.6,0.8)
Figure
mycolors=c("darkseagreen", "lightsteelblue","darkgrey")
ylim.prim <- c(0, 20)
ylim.sec <- c(0, 1)
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
example <- ggplot(data=p, aes(x=year, y=value*10000, colour=count)) +
geom_bar(stat="identity", width=.5, position = "dodge")+
geom_line(data=q, aes(y=a+proportion*b), color="darkseagreen",linewidth=1.2)+
scale_y_continuous("Counts, per 10,000 events", sec.axis = sec_axis(~ (. - a)/b, name = "Percent Success")) +
scale_x_continuous("Year", breaks=seq(2012,2022, by=1)) +
ggtitle("Example figure comparing counts and success rate")+
theme_bw()+
scale_fill_manual(values=mycolors)+
scale_color_manual(values=mycolors, labels=c("Proportion", "Type1","Type2"))+
theme(legend.position="bottom")
example
Output Attempted figure
You are looking for fill
not color
. But, oh no! You've already used color
. So you can just specify a set of aes()
for each layer of geom_bar
and geom_line
.
example <- ggplot() +
geom_bar(stat="identity", width=.5, position = "dodge", data=p, aes(x=year, y=value*10000, fill=count))+
geom_line(data=q, aes(x=year, y=a+proportion*b), color="darkseagreen",linewidth=1.2) +
scale_y_continuous("Counts, per 10,000 events", sec.axis = sec_axis(~ (. - a)/b, name = "Percent Success")) +
scale_x_continuous("Year", breaks=seq(2012,2022, by=1)) +
ggtitle("Example figure comparing counts and success rate")+
theme_bw()+
scale_fill_manual(values=mycolors)+
scale_color_manual(values=mycolors, labels=c("Proportion", "Type1","Type2"))+
theme(legend.position="bottom")
example