I have code in r
to create a plot with labels on the points
library(tidyverse)
library(ggrepel)
set.seed(123) # Set seed for reproducibility
# Generate random strings for the country, party, pro_party, type, and label columns
random_strings <- function(n, length = 10) {
sapply(1:n, function(x) paste0(sample(c(letters, LETTERS), length, replace = TRUE), collapse = ""))
}
# Number of unique values in each column
num_unique_countries <- 17
num_unique_parties <- 45
# Generate random values for each non-numeric column
random_countries <- random_strings(num_unique_countries)
random_parties <- random_strings(num_unique_parties)
random_pro_parties <- sample(c("Pro", "Anti"), num_unique_parties, replace = TRUE)
random_types <- sample(c("TypeA", "TypeB"), num_unique_parties, replace = TRUE)
random_labels <- random_strings(num_unique_parties)
# Replace original values with random values
random_data <- structure(list(
country = factor(sample(random_countries, 45, replace = TRUE)),
party = sample(random_parties, 45, replace = TRUE),
party_pro = c(0.9, 0.959999999403954, 0.75, 0.890909087657928, 0.5625, 0.519999980926514, 0.820000004768372, 0.85, 0.55, 0.685714292526245, 0.825, 0.533333349227905, 0.8125, 0.740000009536743, 0.633333325386047, 0.666666674613953, 0.580000019073486, 0.557142877578735, 0.6, 0.52857141494751, 0.7, 0.928571426868439, 0.925, 0.718181824684143, 0.919999998807907, 0.65, 0.75, 0.5125, 0.462500013411045, 0.155729166666667, 0.105753946304321, 0.159259255727132, 0.368030298839916, 0.36893940170606, 0.453809530394418, 0.292962966141877, 0.333928579092026, 0.292857142857143, 0.272916668156783, 0.240533324877421, 0.320000001362392, 0.398541666269302, 0.36433531443278, 0.151041662693024, 0.143181818181818),
vote = c(34.33, 13.58, 7.23, 10.3, 12.9, 5.98, 8.56, 5.88, 15.1, 8.79, 4.45, 12.83, 18.29, 6.8, 18.68, 0, 10.79, 15.43, 9.08, 8.11, 1.1, 7.97, 3.16, 4.9, 5.02, 9.77, 4.97, 1.14, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1),
pro_party = sample(random_pro_parties, 45, replace = TRUE),
type = sample(random_types, 45, replace = TRUE),
label = sample(random_labels, 45, replace = TRUE)
), row.names = c(NA, -45L), class = c("tbl_df", "tbl", "data.frame"))
random_data |>
ggplot(aes(x=country,y=party_pro,color=type,label=label)) +
geom_point() +
coord_flip() +
scale_color_manual(values = c("red","black")) + labs(x="",y="",color="",size = "") +
scale_size_continuous(breaks = c(1,5,10,20,30)) +
geom_text_repel()
This yields the output:
Which only shows 2 labels.
However, when I run just geom_text()
, all labels are shown:
random_data |>
ggplot(aes(x=country,y=party_pro,color=type,label=label)) +
geom_point() +
coord_flip() +
scale_color_manual(values = c("red","black")) + labs(x="",y="",color="",size = "") +
scale_size_continuous(breaks = c(1,5,10,20,30)) +
geom_text()
I need to use the repel feature because I need to offset labels in different directions in a way that I can’t with even conditional geom_text()
calls.
Why is geom_text_repel()
only labelling 2 points? Is there a way to fix this?
Recognized by R Language Collective
4
As a workaround, instead of coord_flip(), change the mapping of the axes in the aes()
random_data |>
ggplot(aes(y=country,x=party_pro,color=type,label=label)) +
geom_point() +
scale_color_manual(values = c("red","black")) + labs(x="",y="",color="",size = "") +
scale_size_continuous(breaks = c(1,5,10,20,30)) +
geom_text_repel()