调整ggplot2中配色键标签的位置
我试图稍微重新定位离散配色键的标签,以免它们重叠,而不会改变断裂本身的值。在下图中,两个中心标签(将接近零数据包装)太近了,因此看起来像“ -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.)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总是有点骇客,您会收到警告,但是一个选项是将向量传递到
hjust
element_text
的参数以对齐-1
到左侧和1
向左:Always a bit hacky and you get a warning but one option would be to pass a vector to
hjust
argument ofelement_text
to align the-1
to the right and the1
to the left: