在 ggplot2 中标记 geom_rect()

wat*_*wer 4 r ggplot2

我是初学者,所以如果我的问题太基本,我深表歉意。距离我输入第一个命令已经过去了大约 4 天ggplot2。我读过如何将渐变填充应用于 ggplot2 中的 geom_rect 对象?在发布之前,但这篇文章似乎专注于生成渐变矩形,我已经这样做了。

目标:我想强调哪些美国总统的失业率 > 10000(单位并不重要,因为我的重点是绘制图表的能力。

presidential <- subset(presidential, start > economics$date[1])
ggplot(economics) +
       geom_rect(
         aes(xmin = start, xmax = end, fill = party),
         ymin = -Inf, ymax = Inf, alpha = 0.2,
         data = presidential
       ) +
       geom_vline(
         aes(xintercept = as.numeric(start)),
         data = presidential,
         colour = "grey50", alpha = 0.5
       ) +
       geom_text(
         aes(x = start, y = 2500, label = name),
         data = presidential,
         size = 3, vjust = 0, hjust = 0, nudge_x = 50, check_overlap = TRUE
       ) +
       geom_line(aes(date, unemploy)) +
       geom_rect(
         aes(xmin = start, xmax = end),
         ymin = 10000, ymax = Inf, alpha = 0.4, fill = "chartreuse",
         data = presidential
       ) +
       geom_text(
         aes(x = as.Date("1993-01-20"), y = 12000, label = "High unemployment"),
         size = 3, vjust = 0, hjust = 0, color = "forestgreen"
       )+
       scale_fill_manual(values = c("blue", "red")) 
Run Code Online (Sandbox Code Playgroud)

图形输出为:

图形

正如我们所看到的,我能够为显示失业率 > 10000 的绿色矩形创建一个标签。但是,我对这种方法不满意,因为这只是一个快速修复(即我通过调整 x、y、微移来使其工作)等参数)。如果轴的比例发生变化怎么办?文本将会扭曲或可能不可见。我有两个问题:

问题 1:我们是否可以通过编程方式标记绿色矩形,使得失业率 > 10000?我不太关心我们是否使用文本、标签或图例。我假设使用geom_text()需要快速修复(即大量运行和重新运行,以通过不断调整 x、y、vjust、hjust 和微调来确保文本正确显示),但设置图例或标签可能是自动的。

问题2这是一个概念性问题——当我调用 时scale_fill_manual(),ggplot2 如何知道我是否想要垂直矩形或水平矩形的红色和蓝色?我很好奇。为什么它不要求我也为水平和垂直矩形提供颜色?我是否已经为水平矩形使用提供了恒定的颜色,color = forestgreen因此它只需要剩余的垂直矩形对(即红色和蓝色)的颜色?

我是一个初学者,所以如果我的问题对你们中的一些人来说太基本了,我很抱歉。我将不胜感激任何帮助。


更新:

这是数据的 dput:

structure(list(name = c("Nixon", "Ford", "Carter", "Reagan", 
"Bush", "Clinton", "Bush", "Obama"), start = structure(c(-346L, 
1681L, 2576L, 4037L, 6959L, 8420L, 11342L, 14264L), class = "Date"), 
    end = structure(c(1681L, 2576L, 4037L, 6959L, 8420L, 11342L, 
    14264L, 17186L), class = "Date"), party = c("Republican", 
    "Republican", "Democratic", "Republican", "Republican", "Democratic", 
    "Republican", "Democratic")), .Names = c("name", "start", 
"end", "party"), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)

ggplot2经济数据可通过软件包公开获取

 head(economics)
# A tibble: 6 x 6
        date   pce    pop psavert uempmed unemploy
      <date> <dbl>  <int>   <dbl>   <dbl>    <int>
1 1967-07-01 507.4 198712    12.5     4.5     2944
2 1967-08-01 510.5 198911    12.5     4.7     2945
3 1967-09-01 516.3 199113    11.7     4.6     2958
4 1967-10-01 512.9 199311    12.5     4.9     3143
5 1967-11-01 518.1 199498    12.5     4.7     3066
6 1967-12-01 525.8 199657    12.1     4.8     3018
Run Code Online (Sandbox Code Playgroud)

Cyr*_*ian 5

presidential <- subset(presidential, start > economics$date[1])
ggplot(economics) +
       geom_rect(
         aes(xmin = start, xmax = end, fill = party),
         ymin = -Inf, ymax = Inf, alpha = 0.2,
         data = presidential
       ) +
       geom_vline(
         aes(xintercept = as.numeric(start)),
         data = presidential,
         colour = "grey50", alpha = 0.5
       ) +
       geom_text(
         aes(x = start, y = 2500, label = name),
         data = presidential,
         size = 3, vjust = 0, hjust = 0, nudge_x = 50, check_overlap = TRUE
       ) +
       geom_line(aes(date, unemploy)) +
       geom_rect(
         aes(xmin = start, xmax = end, fill = "chartreuse"),
         ymin = 10000, ymax = Inf, alpha = 0.4,
         data = presidential
       ) +
       geom_text(
         aes(x = as.Date("1993-01-20"), y = 12000, label = "High unemployment"),
         size = 3, vjust = 0, hjust = 0, color = "forestgreen"
       )+
       scale_fill_manual(values = c("chartreuse", "red", "blue"), labels=c("High Unemployment","Democrat","Republican"))+labs(fill="")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

就你的第二个问题而言,颜色是在垂直方向映射的,因为你的初始调用......

geom_rect(
         aes(xmin = start, xmax = end, fill = party),
         ymin = -Inf, ymax = Inf, alpha = 0.2,
         data = presidential
       ) 
Run Code Online (Sandbox Code Playgroud)

...标识矩形在该start日期的开始位置及其在该end日期的结束位置,但对于 y 轴,尺寸设置为Inf因此颜色垂直映射。