Reputation: 91
I'm having trouble finding a way to repel text labels in one facet grid "Mountain"(top-center). All other state labels are clear enough to distinguish, but I can't figure out how to move around the state labels in "Mountain" without affecting other grids.
I've tried using:
geom_text_repel(force = x)
But I don't know of any to force the code to affect only a single grid. Below is my code along with the output. My desired outcome is to have the states in "Mountain" grid to appear like they do in "West North Central" or something that can be read easily.
Another thing I haven't quite figured out is my x axis labels. I included the annual range to be 2010 to 2018, to allow space for the state labels, but I only want the axis to show 2010 to 2017 years.
Code/output:
p2 + xlim(breaks = seq(2010,2017, by = 1))
Error in limits.numeric(c(...), "x") : length(lims) == 2 is not TRUE
Any suggestions?
p <- ggplot(plotflow, aes(x = YEAR, y = Y, group = STATE)) +
theme_bw() +
theme(panel.grid.major = element_blank()) +
theme(panel.grid.minor = element_blank()) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
p0 <- p + geom_point(color = "gray70")
p1 <- p0 + geom_smooth(mapping = aes(group = STATE),
se = F, color = "gray10")
p2 <- p1 + geom_text_repel(data = subset(dataplot, YEAR == max(YEAR)),
mapping = aes(x = YEAR, y = Y, label = ALPHA_CODE),
size = 3.6, segment.color = NA, nudge_x = 40) + coord_cartesian(c(min(dataplot$YEAR), 2018))
p2 + labs(x = "Year") +
facet_wrap(CENSUS_DIVISION ~ ., nrow = 3)
Upvotes: 1
Views: 1374
Reputation: 13853
For your first question on applying geom_text_repel()
to one particular facet, you can use a similar solution to that posted here. In your case, your call to geom_text_repel()
might look something like this (I can't try it myself, since I don't have your data):
p2 <- p1 + geom_text_repel(
data = subset(dataplot, YEAR == max(YEAR)),
mapping = aes(x = YEAR, y = Y, label = ALPHA_CODE, CENSUS_DIVISION = "MOUNTAIN"),
size = 3.6, segment.color = NA, nudge_x = 40) +
coord_cartesian(c(min(dataplot$YEAR), 2018))
The idea is to apply to the specific mapping aesthetic associated with the facet(s) you want to apply that geom.
As for the x-axis labeling thing, I would suggest you ask that in a separate question. Is your data formatted as a "Date" or as "numeric"? If it is numeric, then xlim(2010,2017)
should be all you need.
Upvotes: 2