调整ggplot2中配色键标签的位置

发布于 2025-02-11 20:50:29 字数 905 浏览 3 评论 0原文

我试图稍微重新定位离散配色键的标签,以免它们重叠,而不会改变断裂本身的值。在下图中,两个中心标签(将接近零数据包装)太近了,因此看起来像“ -11”,而不是“ -1”和“ 1”。我想将它们推到任何一侧,或更改刻度的每一半的理由(左侧证明负态和右证明阳性的合理性),或者在保留实际配色杆的间距的同时,在标签之间创造更多空间。 (在我的实际数字中,使配色栏更宽。)

“示例图”

这是用于创建此图的代码:

library(dplyr)
library(ggplot2)
library(scales)

df <- data.frame(
  x = runif(1000),
  y = runif(1000),
  z1 = rnorm(100)*10
)

df %>% ggplot() + 
  geom_point(aes(x=x,y=y, color=z1)) + 
  scale_color_steps2(low = muted("darkblue"), mid = "white", high = muted("darkred"), 
                     midpoint = 0, guide_colorbar(barwidth = 20),
                     breaks = c(-20, -10, -5, -1, 1, 5, 10, 20)) + 
  theme_minimal() + 
  theme(legend.position = 'bottom') +
  labs(x='', y='', color='')

I'm trying to slightly reposition the labels of a discrete colorbar so that they don't overlap, without changing the values of the breaks themselves. In the below plot, the two center labels (bracketing the near-zero data) are too close together, so that it looks like '-11' instead of '-1' and '1'. I'd like to nudge them to either side, or change the justification of each half of the scale (left justify the negatives and right justify the positives), or anything to create more space between the labels while retaining the spacing of the actual colorbar. (Making the colorbar wider is not an option in my actual figure.)

example figure

Here is the code used to create this plot:

library(dplyr)
library(ggplot2)
library(scales)

df <- data.frame(
  x = runif(1000),
  y = runif(1000),
  z1 = rnorm(100)*10
)

df %>% ggplot() + 
  geom_point(aes(x=x,y=y, color=z1)) + 
  scale_color_steps2(low = muted("darkblue"), mid = "white", high = muted("darkred"), 
                     midpoint = 0, guide_colorbar(barwidth = 20),
                     breaks = c(-20, -10, -5, -1, 1, 5, 10, 20)) + 
  theme_minimal() + 
  theme(legend.position = 'bottom') +
  labs(x='', y='', color='')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2025-02-18 20:50:33

总是有点骇客,您会收到警告,但是一个选项是将向量传递到hjust element_text的参数以对齐-1到左侧和1向左:

library(ggplot2)

set.seed(123)

df <- data.frame(
  x = runif(1000),
  y = runif(1000),
  z1 = rnorm(100)*10
)

ggplot(df) + 
  geom_point(aes(x=x,y=y, color=z1)) + 
  scale_color_steps2(low = scales::muted("darkblue"), mid = "white", high = scales::muted("darkred"), 
                     midpoint = 0, guide = guide_colorbar(barwidth = 20),# horizontal_legend,
                     breaks = c(-20, -10, -5, -1, 1, 5, 10, 20)) + 
  theme_minimal() + 
  theme(legend.position = 'bottom') +
  labs(x='', y='', color='') +
  theme(legend.text = element_text(hjust = c(rep(.5, 3), 1, 0, rep(.5, 3))))
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> Results may be unexpected or may change in future versions of ggplot2.

“”

Always a bit hacky and you get a warning but one option would be to pass a vector to hjust argument of element_text to align the -1 to the right and the 1 to the left:

library(ggplot2)

set.seed(123)

df <- data.frame(
  x = runif(1000),
  y = runif(1000),
  z1 = rnorm(100)*10
)

ggplot(df) + 
  geom_point(aes(x=x,y=y, color=z1)) + 
  scale_color_steps2(low = scales::muted("darkblue"), mid = "white", high = scales::muted("darkred"), 
                     midpoint = 0, guide = guide_colorbar(barwidth = 20),# horizontal_legend,
                     breaks = c(-20, -10, -5, -1, 1, 5, 10, 20)) + 
  theme_minimal() + 
  theme(legend.position = 'bottom') +
  labs(x='', y='', color='') +
  theme(legend.text = element_text(hjust = c(rep(.5, 3), 1, 0, rep(.5, 3))))
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> Results may be unexpected or may change in future versions of ggplot2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文