Labeling only some data points

28 views
Skip to first unread message

Zack Weinberg

unread,
Aug 4, 2016, 12:01:47 PM8/4/16
to ggplot2
I'm plotting the attached data set like this:

ggplot(x) + facet_grid(cal.set ~ algorithm) +
geom_segment(aes(xend=dx/1000, yend=dy/1000), x=0, y=0,
arrow=arrow(length=unit(0.005, "npc"))) +
labs(x="Longitudinal error (km)", y="Latitudinal error (km)") +
coord_equal()

Now I want to apply labels to the longest arrows in each facet. I can
do "every arrow longer than some distance" like this:

x$label <- as.character(NA)
x$label[x$distance > 500*1000] <- as.character(x$true.cc)[x$distance > 500*1000]

ggplot(x) + facet_grid(cal.set ~ algorithm) +
geom_segment(aes(xend=dx/1000, yend=dy/1000), x=0, y=0,
arrow=arrow(length=unit(0.005, "npc"))) +
geom_text(aes(x=dx/1000+ 100*sign(dx), y=dy/1000 + 100*sign(dy),
label=label),
hjust="outward", vjust="outward") +
labs(x="Longitudinal error (km)", y="Latitudinal error (km)") +
coord_equal()

... but what I actually *want* is "every arrow longer than some
distance, plus, if that puts fewer than 3 labels in facet X, the
longest three arrows in facet X". Can anyone suggest a way to
accomplish that?
x.dput.gz

Ben Bond-Lamberty

unread,
Aug 4, 2016, 1:26:27 PM8/4/16
to Zack Weinberg, ggplot2
Zack,

Using the dplyr package (but easy to do using other methods too), I
think you can do this by:

x %>%
group_by(cal.set, algorithm) %>%
mutate(label = ifelse(distance>500*1000 | min_rank(distance)<3,
as.character(true.cc), NA)) ->
x

In other words, "within each facet group, add a label if the distance
is greater than 500*1000 OR if it's one of the top 3 distances".

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages