I would like to create a function that creates a ggplot graph.
data1 <- data.table(x=1:5, y=1:5, z=c(1,2,1,2,1))
data2 <- data.table(x=1:5, y=11:15, z=c(1,2,1,2,1))
myfun <- function(data){
ggplot(data, aes(x=x, y=y)) +
geom_point() +
geom_text(aes(label=y), y=3) +
facet_grid(z~.)
}
myfun(data2)
It is supposed to indicate the text on the chart. However, without knowing the data in advance, I cannot manually adjust the position of the text vertically. Especially I donβt want the label to move positions with data: I want it to always be about 1/4 vertical from the plots. (Upper middle)
How can i do this?
Is there a function that returns y.limit.upand y.limit.bottomthen I can assign y = (y.limit.up + y.limit.bottm) / 2or something else.
source
share