Reputation: 795
Does anyone know how to make the the value of the grid line appear on the grid line in ggplot2
?
I have a very large plot that I want to display the grid line value on intermittently throughout the plot, so when you are zoomed in you can easily tell what the value is. The only way I can currently think to do that is to add the labels with another data frame or something with the label and the x, y position. I was wondering if there was a more efficient way or an actual built in mechanism to do this.
Thanks!
Upvotes: 1
Views: 229
Reputation: 174468
There is no option built-in to do this specifically, but it is pretty straightforward using expand.grid
to create the grid points and labels, then simply draw the result on with geom_text
:
library(ggplot2)
gridlines <- expand.grid(Petal.Width = seq(0, 2, 0.5),
Sepal.Length = seq(4, 8),
Species = "setosa")
ggplot(iris, aes(Sepal.Length, Petal.Width, color = Species)) +
geom_point() +
geom_text(data = gridlines, color = "gray50", aes(label = Sepal.Length),
angle = 90, vjust = -0.5, nudge_y = 0.25) +
geom_text(data = gridlines, color = "gray50", aes(label = Petal.Width),
vjust = -0.5, nudge_x = 0.5) +
theme_bw(base_size = 20) +
theme(panel.grid.minor = element_blank())
Created on 2022-09-05 with reprex v2.0.2
Upvotes: 1