I am struggling with changing the position of a few points' labels in the geom_point in ggplot. So far, my code:
p <- ggplot(all_short_filled, aes(x=Modernization, y=Passionate_love)) +
geom_point(size=2)+geom_abline(intercept = 0.965830, slope = -0.001127)+ theme_bw()
p1 <- p + geom_text(label=all_short_filled$Country, position = position_dodge(width = 1),
vjust = -0.5)
p1
It gives me something like this:
And I want to change the position of a few overlapping labels (such as Russia and Serbia, or the Netherlands and Belgium, so that, e.g., the label of Serbia would go below the dot, not the above). Please, send help :-)
You could create two label columns in your dataset: one for countries that should be plotted above their point and the other for below. Since I do not have a sample of your data I used the mtcars
dataset to create a reproducible example:
This will require you to know which countries and is hardcoded.
library(datasets) # used to create fake data
library(tidyverse)
# create fake dataset for example
df <- tail(datasets::mtcars) %>%
tibble::rownames_to_column("car")
below <- c("Ferrari Dino", "Maserati Bora")
# create two columns for geom_text labels
data <- df %>%
dplyr::mutate(label_above = ifelse(car %in% below, "", car),
label_below = ifelse(car %in% below, car, ""))
# ignore scale_x.. and scale_y.. those were to fit points/labels neatly
ggplot2::ggplot(data, aes(x = hp, y = mpg)) +
geom_point() +
geom_text(aes(label = label_above), vjust = -0.5) + # labels above their points
geom_text(aes(label = label_below), vjust = 1) + # labels below their points
scale_x_continuous(expand = ggplot2::expansion(0.3)) +
scale_y_continuous(expand = ggplot2::expansion(0.15))
That being said, as mentioned in the comments ggrepel
is usually very good at handling this sort of thing.