I have a plot, which displays a combination of a bar chart and a line chart. Now I want to add labels to the plot, but they are all plotted in a line instead of the right position.
Here is the df I am using:
df1 <- data.frame(cat = c("category1", "category2", "category3",
"category1", "category2", "category3",
"category1", "category2", "category3"),
source = c("a", "a", "a", "b", "b", "b", "c", "c", "c"),
value = c(sample (c(1000L:6000L),9, replace = FALSE)),
stringsAsFactors = FALSE)
And the code for the plotting:
library(dplyr)
library(ggplot2)
library(scales)
ggplot(df1) +
geom_bar(data = filter(df1, source %in% c("a", "b")), aes(cat, value, fill = source), stat="identity", position = position_dodge()) +
geom_point(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source))+
geom_line(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source , group = source)) +
labs(x= "categories", y= "a and b (bars)") +
scale_y_continuous(sec.axis = sec_axis(~.*2000/5000, name = "c (line)"),
labels= format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)) +
scale_fill_manual(values= c("darkseagreen4", "darkseagreen3")) +
geom_text(aes(label=value, x = cat, y = value), vjust = 1.5, size = 3) +
theme_light()
Does anybody know how to get the labels in the right position?