使用正值和负值以及不同比例的面控制条形图中 geom_text 标签的位置
我希望标签出现在正条和负条之外,所以我不能简单地使用 hjust 来移动位置。现在我有这个,它很接近......
score <- c(1, 2, 3, 4, 5, "X")
group1 <- c(0,0,5,-0.2,-4.9,0)
group2 <- c(0.1,0,-1.2,0.4,0.6,0.1)
group3 <- c(0.1,0,3.4,2.9,-6.4,0)
group4 <-c(0,0,-0.9,-0.3,1.3,0)
data <- data.frame(score=as.factor(score), Group1=group1, Group2=group2, Group3=group3, Group4=group4)
data_long <- pivot_longer(data, c(Group1, Group2, Group3, Group4))
data_long$label <- paste(round(data_long$value,1),"%",sep="")
data_long$value <- data_long$value/100
sig <- rep(0, 24)
sig[c(3, 9, 11, 15, 17, 19)] <- 1
sig <- as.factor(sig)
data_long <- cbind(data_long, sig)
bars <- data_long %>% filter(score != 2,
score != "X") %>%
ggplot(aes(x = value, y=name, fill=sig))+
geom_bar(stat="identity") +
scale_fill_manual(values=c("grey", "firebrick")) +
scale_x_continuous(limits = ~ c(-1, 1) * max(abs(.x)),
labels = scales::percent) +
facet_wrap(~score, scales = "free_x") +
theme(legend.position = "none") +
geom_text(aes(label=label,
x = value + (0.005 * sign(value))), size = 3) +
labs(x = "Deviation From Expected Value",
y = "Group",
title = "Deviations From Expected Value by Score",
caption = "Red bars statistically significant")
print(bars)
但请注意,在左上角的小平面中,比例尺非常小,标签与非零条相距甚远。我假设这是因为我在 geom_text x 美学中使用的乘法因子对于该比例来说很大。我尝试了其中的某种功能,考虑到了限制,但我得到了一个错误
Aesthetics must be valid data columns. Problematic aesthetic(s): x = ~(value + (0.005 * max(abs(.x)) * sign(value)))
任何有关如何继续的建议将不胜感激。
I want labels to appear outside of both positive and negative bars, so I can't simply use hjust to move the location. Right now I have this, which is close...
score <- c(1, 2, 3, 4, 5, "X")
group1 <- c(0,0,5,-0.2,-4.9,0)
group2 <- c(0.1,0,-1.2,0.4,0.6,0.1)
group3 <- c(0.1,0,3.4,2.9,-6.4,0)
group4 <-c(0,0,-0.9,-0.3,1.3,0)
data <- data.frame(score=as.factor(score), Group1=group1, Group2=group2, Group3=group3, Group4=group4)
data_long <- pivot_longer(data, c(Group1, Group2, Group3, Group4))
data_long$label <- paste(round(data_long$value,1),"%",sep="")
data_long$value <- data_long$value/100
sig <- rep(0, 24)
sig[c(3, 9, 11, 15, 17, 19)] <- 1
sig <- as.factor(sig)
data_long <- cbind(data_long, sig)
bars <- data_long %>% filter(score != 2,
score != "X") %>%
ggplot(aes(x = value, y=name, fill=sig))+
geom_bar(stat="identity") +
scale_fill_manual(values=c("grey", "firebrick")) +
scale_x_continuous(limits = ~ c(-1, 1) * max(abs(.x)),
labels = scales::percent) +
facet_wrap(~score, scales = "free_x") +
theme(legend.position = "none") +
geom_text(aes(label=label,
x = value + (0.005 * sign(value))), size = 3) +
labs(x = "Deviation From Expected Value",
y = "Group",
title = "Deviations From Expected Value by Score",
caption = "Red bars statistically significant")
print(bars)
This produces the following chart
But notice how in the top left facet, where the scale is REALLY small, the labels are very far removed from the non-zero bars. I'm assuming this is because the multiplicative factor I use in the geom_text x aesthetic is large for that scale. I took a stab at some sort of function in there that took the limits into account but I got an error
Aesthetics must be valid data columns. Problematic aesthetic(s): x = ~(value + (0.005 * max(abs(.x)) * sign(value)))
Any advice on how to proceed would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
hjust
作为美学映射。如果将其设置为0.5 - sign(value)/2
,则根据需要,正条形为 0,负条形为 1。You can use
hjust
as an aesthetic mapping. If you set it to0.5 - sign(value)/2
it will be 0 for the positive bars and 1 for the negative bars, as desired.