r - Prevent overlapping labels with geom_segment and facet -


i'm using geom_segment() , facet_wrap() display estimates across different types , models. previous post helped me organize things, i'm struggling figure out how overset labels don't overlap. things messy once add more models comparison more data points. i've tried changing aspect ratio without resolution.

how can overset, or spread labels out readable while retaining scale of y-axis comparison across models , types?

sample data

dat <- structure(list(temp = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3,  4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4,  5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,  1, 2, 3, 4, 5), rev = c(-5, -11, -20, -29, -40, -9, -20, -32,  -45, -57, -12, -24, -37, -50, -62, -7, -20, -36, -52, -67, -5,  -13, -23, -35, -47, -12, -24, -36, -48, -58, 0, 0, -3, -7, -12,  0, 0, 0, 0, -1, -4, -9, -15, -21, -28, 2, 1, -1, -6, -13, -4,  -7, -8, -8, -6, 8, 16, 23, 29, 34), type = c("type 1", "type 1",  "type 1", "type 1", "type 1", "type 1", "type 1", "type 1", "type 1",  "type 1", "type 1", "type 1", "type 1", "type 1", "type 1", "type 2",  "type 2", "type 2", "type 2", "type 2", "type 2", "type 2", "type 2",  "type 2", "type 2", "type 2", "type 2", "type 2", "type 2", "type 2",  "type 3", "type 3", "type 3", "type 3", "type 3", "type 3", "type 3",  "type 3", "type 3", "type 3", "type 3", "type 3", "type 3", "type 3",  "type 3", "type 4", "type 4", "type 4", "type 4", "type 4", "type 4",  "type 4", "type 4", "type 4", "type 4", "type 4", "type 4", "type 4",  "type 4", "type 4"), model = c("a", "a", "a", "a", "a", "b",  "b", "b", "b", "b", "c", "c", "c", "c", "c", "a", "a", "a", "a",  "a", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "a", "a",  "a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c",  "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "c",  "c", "c")), .names = c("temp", "rev", "type", "model"), row.names = c(na,  -60l), class = "data.frame") 

plot

df.labeled <- dat %>%    ungroup() %>% group_by(type, rev) %>%    mutate(label = c(rev[1], rep(na, length(rev) - 1)))  ggplot(df.labeled, aes(temp, rev, color = model)) +    geom_segment(aes(xend = 0, yend = rev), linetype = "dashed", color = "grey") +   geom_text(aes(label = label, x = -0.1), colour = "black", hjust = 1) +   geom_vline(xintercept = 0) +   geom_point() + geom_line() + facet_wrap(~type) +    scale_y_continuous(breaks = null) +    scale_x_continuous(limits = c(-0.5, na)) +   theme_bw() + theme(panel.grid = element_blank()) 

enter image description here

one option stagger values. however, think labeling points directly cleaner , less confusing. show both methods below.

staggered value labels

# set staggered x-values value labels df.labeled = df.labeled %>%    group_by(type, is.na(label)) %>%    arrange(rev) %>%   mutate(xval = rep(c(-0.1,-0.35), ceiling(n()/2))[1:n()])  ggplot(df.labeled, aes(temp, rev, color = model)) +    geom_segment(aes(xend=xval - 0.08, yend=rev), linetype="11", color="grey70", size=0.3) +   geom_text(aes(x=xval, label=label), colour="black", hjust=1, size=2.5) +   geom_vline(xintercept = 0, colour="grey90", size=0.3) +   geom_point() + geom_line() + facet_wrap(~type) +    scale_y_continuous(breaks = null) +    scale_x_continuous(limits = c(-0.5, na)) +   theme_bw() + theme(panel.grid = element_blank()) 

enter image description here

use y-values point labels

ggplot(df.labeled, aes(temp, rev, color = model)) +    geom_text(aes(label = rev), size=2.5, show.legend=false) +   geom_line(alpha=0.3, size=0.7) +    facet_wrap(~type) +    scale_x_continuous(limits = c(0, na)) +   theme_bw() +    theme(panel.grid = element_blank()) +   guides(colour=guide_legend(override.aes=list(alpha=1, size=1))) 

enter image description here


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -