I am stuck implementing the idea to combine the barplot containing fact, target and prognosis values with a line representing a fitted model based on fact and leading to prognosis values.
I am trying the following:
library(dplyr)
library(ggplot2)
bardf <- data.frame(vals = c(12,12.5, 11, 14,14.5, 15.2,14.5),
groups = c("fact", "target", "fact", "fact", "target", "target","prognosis") %>% factor,
xaxs = c("Jan","Jan", "Feb", "Mar","Mar", "Apr","Apr") %>%
factor(ordered = T, levels = c("Jan", "Feb", "Mar", "Apr")))
p <- bardf %>%
ggplot(aes(x = xaxs, y = vals, group = groups, fill = groups))+
geom_bar(stat= "identity", position = position_dodge(0.9))
model_fits<- data.frame(fittedvals = c(12.1, 11.5, 14.1, 14.5),
groups = c("fact", "fact", "fact", "prognosis") %>% factor,
xaxs = c("Jan", "Feb", "Mar", "Apr") %>%
factor(ordered = T, levels = c("Jan", "Feb", "Mar", "Apr")))
p +
geom_line(aes(x = xaxs, y = fittedvals, group = groups),
data = model_fits, stat= "identity",position = position_dodge(0.9))
This is returning the following plot:
I would like to place the nodes of the line at the x axis middle of fact or prognosis bars like this:
Note: there will be no situation when the prognosis and fact bars will be plotted for the same month simultaneously but I need to consider different fact sources like this:
data.frame(vals = c(12,12.5,13, 11, 14,14.5, 15.2,14.5),
groups = c("fact1","target","fact2", "fact1", "fact1", "target", "target","prognosis") %>% factor,
xaxs = c("Jan","Jan","Jan", "Feb", "Mar","Mar", "Apr","Apr") %>%
factor(ordered = T, levels = c("Jan", "Feb", "Mar", "Apr"))) %>%
ggplot(aes(x = xaxs, y = vals, group = groups, fill = groups))+
geom_bar(stat= "identity", position = position_dodge(0.9))
For groups where two or more fact sources are available I would like to skip any x adjustments of the plotted line and plot it at exactly month x position:
In other words: how do I manually specify adjustment of each node of the line plotted over barplotor or set that the x adjustment is not required for the specific node?