简体   繁体   English

将文本放在堆积条形图的 x 轴正上方和下方

[英]Place text just above and below x axis of stacked bar graph

I need to place two words (Increasing / Decreasing) to the left of the bar written vertically just above and below the x-axis.我需要在 x 轴正上方和下方垂直书写的条形左侧放置两个词(增加/减少)。 I'm trying annotate but with not much luck.我正在尝试annotate但运气不佳。 The text overlaps and isn't placed correctly in all cases.文本重叠并且在所有情况下都没有正确放置。

What is the best way to place the text just above and below the light grey x-axis with enough room to display the word.将文本放在浅灰色 x 轴上方和下方的最佳方式是什么,并有足够的空间来显示单词。 The possible positive values range from 0 to 50 and negative from 0 to -50.可能的正值范围从 0 到 50,负值范围从 0 到 -50。

There are 3 tests below the R script. R脚本下面有3个测试。

library("tidyverse")

red <- "#CD5C5C"
gold <- "#FCB700"
lt_grey <- "#D8D8D8"
dark_grey <- "#525250"
axis_text_size <- 16

df <- tibble(
  type = c("up", "down"),
  term = c("1yr", "1yr"),
  value = c(3, -6),
  display_value = c(3, 6),
  vjust = c(-0.3, 1.2),
  bar_num_col = c("#FCB700", "#C8F5C0")
)

# Stacked bar plot
ggplot(df, aes(fill = type, y = value, x = term)) + 
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_manual(values = c(red, gold)) +
  labs(title = "Title") +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_text(size = axis_text_size),
    axis.text.y =  element_blank(),
    panel.grid.major.x = element_line(size = 0.2, linetype = 'solid',
                                      colour = "#696969"),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.background = element_rect(fill = dark_grey),
    legend.position = "none"
  ) +
  geom_text(aes(term, label = display_value,  vjust = vjust, color = type), size = 8) +
  scale_color_manual(values = c(red, gold)) +
  
  # Add horizontal line
  geom_hline(yintercept = 0, size = 1.5, colour = lt_grey) +
  
  # Expand grey area above and below ends of bars to make room for the numbers. (bottom, top)
  scale_y_continuous(expand = expansion(mult = c(0.1, 0.1))) +

  annotate("text", x = 0.1, y = 0.3, label= "bold(Increases)",
           col = gold, size = 8, angle = 90, parse = TRUE) +

  annotate("text", x = 0.1, y = -0.31, label= "bold(Decreases)",
           col = red, size = 8, angle = 90, parse = TRUE)


# Tests -------------------------------------------------------------------

# Test 1
df$value <- c(0, 0)  
df$display_value <- c(0, 0)

# Test 2
df$value <- c(2, -5)  
df$display_value = c(2, 5)

# Test 3
df$value <- c(2, -50)  
df$display_value = c(2, 50)
<YOUR CODE> +
annotate("text", x = 0.1, y = 0.1, vjust = 1, hjust = 0, label= "bold(Increases)",
           col = gold, size = 7, angle = 90, parse = TRUE) +
annotate("text", x = 0.1, y = -0.1, vjust = 1, hjust = 1, label= "bold(Decreases)",
           col = red, size = 7, angle = 90, parse = TRUE)

在此处输入图像描述

The text will take up different amounts of chart space depending on the output dimensions and text size;文本将根据 output 尺寸和文本大小占用不同数量的图表空间; if you want more predictability while outputting in different sizes, you might look at the ggtext package and define the annotations to fill boxes whose dimensions are defined in chart space.如果您希望在以不同大小输出时具有更高的可预测性,您可以查看ggtext package 并定义注释以填充尺寸在图表空间中定义的框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM