r - 在月轴上准确放置 geom_text 标签

标签 r ggplot2 geom-text

如何调整geom_text使其正确可见?

data <- read.table(text = "      months preMonth postMonth preOnc postOnc
1 2017-10-19      958       543    242     657
2 2017-11-19      958       177    242    1023
3 2017-12-19      958       127    242    1073
4 2018-01-19      958        41    242      NA
5 2018-02-19      958        21    242      NA
6 2018-03-19      958        21    242      NA")


dataLong <- data %>% tidyr::gather(Type, Amount, 2:5) %>% filter(!is.na(Amount))
dataLong$delim <- substr(dataLong$Type, 1, 3)
dataLong$Type <- factor(dataLong$Type, levels = c("preOnc", "postOnc", "preMonth", "postMonth"))
dataLong$delim <- factor(dataLong$delim, levels = c('pre', 'pos'))
ggplot(dataLong, aes(x=months, y=Amount, fill=Type)) + 
  geom_bar(stat = "identity") +
  geom_hline(yintercept = 1200, col='red',linetype='dashed') + 
  geom_text(aes(x=months[1], y= 1200 ,label = "Expected: 1200", vjust = -1)) +
  facet_grid(. ~ delim,  labeller=label_both) + 
  scale_fill_manual(values = c("grey", "grey", "seagreen3", "seagreen3")) +
  scale_x_date(date_breaks = "1 month", date_labels = "%b-%y")

enter image description here

最佳答案

你想要这个吗?

请注意 geom_text() 调用中的 hjust = -1, vjust = -0.5

library(tidyverse)

data <- read.table(text = "      months preMonth postMonth preOnc postOnc
1 2017-10-19      958       543    242     657
2 2017-11-19      958       177    242    1023
3 2017-12-19      958       127    242    1073
4 2018-01-19      958        41    242      NA
5 2018-02-19      958        21    242      NA
6 2018-03-19      958        21    242      NA")

# the tidyverse (in this case dplyr) case of preparing the data 
dataLong <- data %>% 
  gather(Type, Amount, 2:5) %>% 
  filter(!is.na(Amount)) %>% 
  mutate(
    delim =  substr(Type, 1, 3),
    Type = factor(Type, levels = c("preOnc", "postOnc", "preMonth", "postMonth")),
    delim = factor(delim, levels = c("pre", "pos")),
    months = as.Date(months)
    )

ggplot(dataLong, aes(x = months, y = Amount, fill = Type)) + 
  geom_bar(stat = "identity") +
  geom_hline(yintercept = 1200, col = "red",linetype = "dashed") + 
  geom_text(aes(x = months[1], y = 1200, label = "Expected: 1200"), 
            vjust = -0.5, hjust = -1) +
  facet_grid(. ~ delim, labeller = label_both) + 
  scale_fill_manual(values = c("grey", "grey", "seagreen3", "seagreen3")) +
  scale_x_date(date_breaks = "1 month", date_labels = "%b-%y")

使用居中 geom_label 的替代方案

# first compute the middle of the range of months
mid_month <- range(dataLong$months) %>% {(.[2] - .[1]) / 2 + .[1]}

ggplot(dataLong, aes(x = months, y = Amount, fill = Type)) + 
  geom_bar(stat = "identity") +
  geom_hline(yintercept = 1200, col = "red",linetype = "dashed") +

  # use geom_label and hardcode the parameters
  geom_label(x = mid_month, y = 1200, label = "Expected: 1200", 
             vjust = 0.5, hjust = 0.5, color = "red", inherit.aes = F) +
  facet_grid(. ~ delim, labeller = label_both) + 
  scale_fill_manual(values = c("grey", "grey", "seagreen3", "seagreen3")) +
  scale_x_date(date_breaks = "1 month", date_labels = "%b-%y")

这里,如果没有 inherit.aes = F,我们也会为图例指定颜色(我们不会)...

关于r - 在月轴上准确放置 geom_text 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46826401/

相关文章:

r - 从 data.frame 中提取单行而不丢失名称

r - ggplot2轴通过常数转换

R 绘图使用 ggplot facet_grid 添加图例

r - 如何在ggplot2的geom_text()标签中向标签添加希腊字母

R ggplot 使用三个值和 bgroup 用 atop 进行注释

r - 从两个数值创建序数变量

R v3.4.0-2 在 Arch 上无法找到 libgfortran.so.3

R:根据权重更改某些(但不是全部)绘制数据点的大小

r - 从facet_wrap更改标题的字体大小

r - 避免在 ggplot2 中重叠 geom_point 和 geom_text