使用geom_text标记散点图的外围点.根据定义,这些点往往靠近画布边缘:通常至少有一个单词与画布边缘重叠,使其无用.
显然,这可以通过以下情况手动解决+ xlim(c(1.5, 4.5)):
# test
df <- data.frame(word = c("bicycle", "tricycle", "quadricycle"),
n.wheels = c(2,3,4),
utility = c(10,6,7))
ggplot(data=df, aes(x=n.wheels, y=utility, label=word)) + geom_text() + xlim(c(1.5, 4.5))
Run Code Online (Sandbox Code Playgroud)

但这并不理想
搜索这个问题没有找到解决方案,Hadley Wickham似乎满足于在ggplot2的帮助页面中将标签切成两半(我知道Hadley,他们只是一个例子;)
我正在使用以下代码来尝试保留超出绘图区域范围的geom元素,但它似乎仍将其裁剪到绘图区域上方一定距离之外。
g <- ggplot(iris, aes(x = Species, y = Petal.Length)) +
stat_summary(geom = 'bar', fun.y = mean) +
geom_point() +
scale_y_continuous(limits = c(0,8), expand = c(0,0), oob = function(x, ...) x) +
geom_text(label = 'obText', aes(x = 2, y = 9)) #+
# theme(plot.margin = unit(c(60,5.5,5.5,5.5), "points"),
# aspect.ratio = 1)
gb <- suppressWarnings(ggplot_build(g))
gt <- ggplot_gtable(gb)
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid::grid.newpage()
grid::grid.draw(gt)
Run Code Online (Sandbox Code Playgroud)
关于这是为什么以及如何纠正的任何想法?如果我取消对主题参数的注释,则可以接近所需的大小,但这会改变绘图区域的纵横比。