-
-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
geom_text_repel when using size aesthetic #14
Comments
Thanks for the comment! Please post a minimal code example and an image of the plot. |
Not exactly minimal, but handy.
|
Thanks for sharing the interesting use case! In the future, I'll consider adding back Since I deleted those parameters, try using library(ggrepel)
set.seed(423)
ggplot(my.data, aes(V2,V5,label=V10)) +
geom_point(aes(color = V4, size = V3)) +
scale_colour_manual(values=c("no" = rgb(145, 207, 96, max = 255), "yes" = rgb(215, 48, 39, max = 255)),labels = c("No", "Yes")) +
scale_fill_manual(values=c(rgb(145, 207, 96, max = 255), rgb(215, 48, 39, max = 255)),labels = c("No", "Yes")) +
stat_smooth(aes(colour = V4,fill =V4), method="lm", se=TRUE,alpha = 0.10, show.legend=FALSE) +
geom_text_repel(
data=my.data[my.data$V4=="yes",],
size=3.0, alpha = 0.85, na.rm = TRUE,
box.padding = unit(0.75, "lines")
) +
xlab("X label") + ylab("Y label") +
labs(size="Size legend: ", colour="Colour legend: ") +
guides(fill="none",colour = guide_legend(override.aes = list(size=5))) +
ggtitle('Main Title') +
theme_bw() +
theme(legend.position = "top",
legend.key = element_rect(linetype = 0),
legend.text = element_text(size = rel(1.0)),
legend.title = element_text(size = rel(1.0), hjust = 0),
plot.title = element_text(size = rel(1.05)),
panel.margin = unit(c(1, 1, 2, 0.5), "lines"),
axis.title.x = element_text(vjust = .5),
axis.title.y = element_text(vjust = .8)
) |
It looks ok with box.padding Thank you very much. On Thu, Jan 14, 2016 at 5:08 PM, Kamil Slowikowski <[email protected]
|
Hi I'm affraid the case should not be closed, as |
Could I please ask if you might be able to try installing the development version of ggrepel from Github and then running this example? devtools::install_github("slowkow/ggrepel") I copied the example code here: my_pal <- function(range = c(1, 6)) {
force(range)
function(x) scales::rescale(x, to = range, from = c(0, 1))
}
dat <- mtcars
dat$car <- rownames(dat)
ggplot(dat, aes(wt, mpg, label = car)) +
geom_point(aes(size = cyl), alpha = 0.6) + # data point size
continuous_scale(
aesthetics = c("size", "point.size"), scale_name = "size",
palette = my_pal(c(2, 15)),
guide = guide_legend(override.aes = list(label = "")) # hide "a" in legend
) +
geom_text_repel(
aes(point.size = cyl), # data point size
size = 5, # font size in the text labels
point.padding = 0, # additional padding around each point
min.segment.length = 0, # draw all line segments
max.time = 1, max.iter = 1e5, # stop after 1 second, or after 100,000 iterations
box.padding = 0.3 # additional padding around each text label
) +
theme(legend.position = "right") |
It surely looks much better, but not perfect
There are still some overlaps. It looks like the label 'is aware' of the size of its point, but ignores that neighbouring points may also be oversized. |
@slowkow, this is awesome, thank you so much! It worked perfectly for my use case for several plots with varying bubble sizes. Any plan to include this as an argument in |
@z3tt Sorry, but I don't understand. Could I please ask if you might elaborate? |
@slowkow Sorry for being imprecise. I used your dynamic scaling of the range to label bubbles where a variable is mapped to size. |
@z3tt Unfortunately, I am not aware of any approach to simplify the dynamic point size functionality in the above example 🙁 The challenge is that we need two independent layers ( I realize that this example is going to be difficult for newcomers to ggplot2 to fully understand. In my experience, I need to re-read the code each time I want to understand it, and I need to copy-paste it to get it right. This makes me think it may be worthwhile to write up a blog post or a new documentation article that tries to clarify each of the steps. I wish we could change the ggplot2 design a little bit, so that each layer can "see" the previous layers somehow... but it's not obvious to me how to implement this or whether this kind of feature would be useful for other applications. It is possible that I have overlooked a simpler way to implement dynamic point size repulsion... maybe someone can develop a simpler code example? Many users seem interested in this feature, so improvements would be welcome. There's a bit of a rabbit hole if you want to keep reading about this theme: |
I am definitely not a newcomer to ggplot2, however, I will always need to look this up given I don't need it very often^^ |
In cases where there is a
size
aesthetic mapping, points might become quite large in a scatter plot. When there is no need for repelling, the labels may overlap with their corresponding points.I used to use hjust/vjust with (geom_text) to correct that. What can I do now?
The text was updated successfully, but these errors were encountered: