I have a barplot with a geom_line over it. I want to add labels that appear on top of the geom_line and not on top of the bars.
I'm using this code:
ggplot(media_movel, aes(x=SE_PRI_SIN, y=n, fill= " ", group = 1))
geom_bar(stat='identity', position='dodge', color = "black", fill = "skyblue4")
geom_line(data = media_movel, aes(y = media, color = "MÉDIA MÓVEL"), size = 0.75)
geom_text(aes(label= media), hjust= 0.5, vjust = -1, size=3, color= "black")
scale_y_continuous(limits = c(0, max(media_movel$n) 250), expand = expansion(mult = c(0, .1)))
labs(fill = " ", x = "Semana Epidemiológica", y = "Número de Casos")
scale_colour_manual(" ", values=c("CASOS" = "skyblue4", "MÉDIA MÓVEL" = "red"))
theme_minimal(base_size = 10)
theme(legend.position = "bottom", axis.line = element_line(colour = "black"),
axis.text.x=element_text(angle = 90, vjust = 0.5, colour="black"),
axis.text.y=element_text(colour="black"))
It looks like this:
I want the labels on top of the red line. How can I do that?
Here's some reproductible data:
data <- data.frame(SE_PRI_SIN= c("SE 29", "SE 30", "SE 31", "SE 32", "SE 33","SE 34", "SE 35", "SE 36",
"SE 37", "SE 38"),
n = c(5, 10, 28, 33, 78, 19, 50, 88, 97, 7824),
media_movel = c(494.5, 525.5, 568, 645, 700, 618, 476.5, 370.5, 299.5, 4994))
CodePudding user response:
With the provided data, we get this:
I have changed the code a little: By the way it works also without geom_point
:-)
library(tidyverse)
data %>%
ggplot(aes(x=SE_PRI_SIN, y=media_movel, group=1))
geom_col(aes(y= n), position = position_dodge(), color = "black", fill = "skyblue4")
geom_point(color = "red", size = 0.75)
geom_text(
label=media_movel$media_movel,
nudge_x = 0, nudge_y = 300,
check_overlap = T
)
geom_line(aes(color = "MÉDIA MÓVEL"), size = 0.75)
scale_y_continuous(limits = c(0, max(media_movel$n) 250), expand = expansion(mult = c(0, .1)))
labs(fill = " ", x = "Semana Epidemiológica", y = "Número de Casos")
scale_colour_manual(" ", values=c("CASOS" = "skyblue4", "MÉDIA MÓVEL" = "red"))
theme_minimal(base_size = 10)
theme(legend.position = "bottom", axis.line = element_line(colour = "black"),
axis.text.x=element_text(angle = 90, vjust = 0.5, colour="black"),
axis.text.y=element_text(colour="black"))