使用正值和负值以及不同比例的面控制条形图中 geom_text 标签的位置

发布于 2025-01-10 17:25:33 字数 1795 浏览 0 评论 0原文

我希望标签出现在正条和负条之外,所以我不能简单地使用 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
enter image description here

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 技术交流群。

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

发布评论

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

评论(1

一抹淡然 2025-01-17 17:25:33

您可以使用 hjust 作为美学映射。如果将其设置为 0.5 - sign(value)/2,则根据需要,正条形为 0,负条形为 1。

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, hjust = 0.5 - sign(value)/2),  size = 3) +
  labs(x = "Deviation From Expected Value",
       y = "Group",
       title = "Deviations From Expected Value by Score",
       caption = "Red bars statistically significant")

输入图片此处描述

You can use hjust as an aesthetic mapping. If you set it to 0.5 - sign(value)/2 it will be 0 for the positive bars and 1 for the negative bars, as desired.

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, hjust = 0.5 - sign(value)/2),  size = 3) +
  labs(x = "Deviation From Expected Value",
       y = "Group",
       title = "Deviations From Expected Value by Score",
       caption = "Red bars statistically significant")

enter image description here

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