Reputation: 31
I get this R ggplot2 working graph:
library("xkcd")
library("extrafont")
library("ggplot2")
library("gridExtra")
X <- c(0,20)
prova_A <- 0 + 5*X
prova_B <- 0 + 4*X
graph_3 <- ggplot() + geom_path(aes(X, prova_A), col = "darkorange") + geom_point(aes(c(0,20), c(0,100))) +
geom_path(aes(X, prova_B), col = "red") + geom_point(aes(c(0,20), c(0,80))) +
xkcdaxis(c(0,20),c(0,100)) + xlab("Tempo") + ylab("Nota") +
ggtitle("Estudar para prova") + theme(plot.title = element_text(hjust = 0.5)) +
geom_text(aes(label = "t = "), x = 19, y = -7, family = "xkcd", size = 5.5)
graph_3
Plot with "t = " label visible
I just want to adjust y position in geom_text to -8 or -9 but when I do that it looks like my xlab "hides" de geom_text "t = " label. This way:
Plot with "t = " label partly hidden
I was expecting my text "t = " part legible over the xlab. What's causing this? Do you have any clue how to fix it? I tried "check_overlap" parameter, but it doesn't work for this purpose.
Question customizing order of x-axis labels with unfixed labels in ggplot2 is somewhat relatable, but my problem looks simplier.
[EDIT Dec. 19 GMT: 12:35] Following @Stefan and @VinceGreg suggestion appearing in the comments, the more easy solution is to edit label via scale. Using the code suggestion, a working example is this one (note that "dplyr" package is required now):
graph_3 <- ggplot() + geom_path(aes(X, prova_A), col = "darkorange") + geom_point(aes(c(0,20), c(0,100))) +
geom_path(aes(X, prova_B), col = "red") + geom_point(aes(c(0,20), c(0,80))) +
xkcdaxis(c(0,20),c(0,100)) + xlab("Tempo") + ylab("Nota") +
ggtitle("Estudar para prova") + theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous(breaks = seq(0,20,by = 5) , labels = ~if_else( .x < 20, as.character(.x) ,paste0( "t = ", .x)))
graph_3
Upvotes: 3
Views: 61