问题
The issue I am having is that I would like the points above a certain threshold (=<25) to not produce points larger than the set scale. These larger points still need to be displayed, and cannot be excluded:
d=data.frame(y=c(1,2,6,4,4,6,7,8),
x=c(8,4,7,5,4,9,2,3),
coverage=c(0,6,9,88,25,22,17,100),
col=c(0,.25,.50,.76,.80,1.00,.11,.34)
)
ggplot() +
scale_size(range = c(0, 13),
breaks = c(0, 5, 10, 20, 25),
labels = c("0", "5", "10", "20", "25+"),
guide = "legend"
) +
geom_point(data = d, mapping = aes(x = x, y = y, color = col, size = coverage)) +
labs(title = "geom_point")
In the above sample code I have two points which have a "coverage" larger than 25+ and are outside of the scale. I would like these points to appear the same size as the 25+ threshold.
回答1:
I think this is what you're looking for:
d %>%
mutate(coverage_trunc = pmin(coverage, 25)) %>%
ggplot() +
geom_point(mapping=aes(x=x, y=y, color=col, size=coverage_trunc)) +
labs(title="geom_point") +
scale_size(range=c(0,13),
breaks=c(0,5,10,20,25),
labels=c("0","5","10","20","25+"),
name = "Coverage Truncated",
guide="legend")
回答2:
A ggplot only solution is to set the scale limits, and define the out of bounds behavior (oob) correctly. For size, one can use this scale:
ggplot(d, aes(x, y, color = col, size = coverage)) +
geom_point() +
scale_size_area(
max_size = 13,
breaks = c(0,5,10,20,25),
labels = c("0","5","10","20","25+"),
guide = "legend",
limits = c(0, 25),
oob = scales::squish
)
来源:https://stackoverflow.com/questions/54013902/adjust-geom-point-size-so-large-values-are-plotted-but-do-not-appear-larger-in