相关疑难解决方法(0)

如何在画布的边界内制作geom_text图

使用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)

三轮车

但这并不理想

  1. 它不是自动化的,因此如果要生成许多图,则会减慢过程
  2. 它并不准确,这意味着单词边缘和画布边缘之间的距离在每种情况下都不相等.

搜索这个问题没有找到解决方案,Hadley Wickham似乎满足于在ggplot2的帮助页面中将标签切成两半(我知道Hadley,他们只是一个例子;)

plot text r ggplot2

38
推荐指数
3
解决办法
2万
查看次数

如何防止ggplot裁剪超出范围的点

我正在使用以下代码来尝试保留超出绘图区域范围的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)

关于这是为什么以及如何纠正的任何想法?如果我取消对主题参数的注释,则可以接近所需的大小,但这会改变绘图区域的纵横比。

r ggplot2

3
推荐指数
2
解决办法
1414
查看次数

标签 统计

ggplot2 ×2

r ×2

plot ×1

text ×1