You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I often find myself making plots where I want to include some sort of label near the plot margin. Usually I would use geom_text, but sometimes I have a figure (imagery or heatmap, for example) where the text isn't visible without a background, so I use geom_label in those cases. However, I have found that the alignment of the text and background box in geom_label don't behave in a way that I would expect or that is conducive to making tidy looking figures. Here is an example of putting the labels in the lower, left-hand corner without changing the justification. As expected, the labels run off the page and the text is centered within the background box.
I can change hjust and vjust so that the label is aligned in the lower left corner. However, with a smaller than default text size, and a non-centered justification, the text is not centered within the background box. Personally, I think this looks bad, and also results in more of the figure being covered up by the background box than is necessary.
# Adding labels, the text isn't centered in the box (this is more obvious if the text size is reduced)plot+
geom_label(data=labels,
aes(x=x, y=y,
label=label),
size=2,
hjust=0,
vjust=0)
Using nudge_x and nudge_y instead of hjust and vjust allows the text to remain centered within the background box, but requires more fiddling to get the text location correct when the labels are all the same length. When the labels are not all the same length, this method does not allow the desired alignment - either the longer label runs off the left side of the plot or the shorter label is pushed farther toward the center of the figure.
# the text is centered within the box, but labels of different lengths aren't aligned in the bottom, left cornerplot+
geom_label(data=labels,
aes(x=x, y=y,
label=label),
size=2,
nudge_x=1,
nudge_y=0.25)
Ideally, I think the text in a geom_label should be centered within it's background box regardless of the value of hjust or vjust.
The text was updated successfully, but these errors were encountered:
Hi there, thanks for the report! Fortunately, this is already fixed in the development version of ggplot2 by #5030, so it is a matter of time before it comes to CRAN.
I often find myself making plots where I want to include some sort of label near the plot margin. Usually I would use geom_text, but sometimes I have a figure (imagery or heatmap, for example) where the text isn't visible without a background, so I use geom_label in those cases. However, I have found that the alignment of the text and background box in geom_label don't behave in a way that I would expect or that is conducive to making tidy looking figures. Here is an example of putting the labels in the lower, left-hand corner without changing the justification. As expected, the labels run off the page and the text is centered within the background box.
I can change
hjust
andvjust
so that the label is aligned in the lower left corner. However, with a smaller than default text size, and a non-centered justification, the text is not centered within the background box. Personally, I think this looks bad, and also results in more of the figure being covered up by the background box than is necessary.Using
nudge_x
andnudge_y
instead ofhjust
andvjust
allows the text to remain centered within the background box, but requires more fiddling to get the text location correct when the labels are all the same length. When the labels are not all the same length, this method does not allow the desired alignment - either the longer label runs off the left side of the plot or the shorter label is pushed farther toward the center of the figure.Ideally, I think the text in a geom_label should be centered within it's background box regardless of the value of
hjust
orvjust
.The text was updated successfully, but these errors were encountered: