gpt4 book ai didi

r - 在 ggplot2 中标记 geom_rect()

转载 作者:行者123 更新时间:2023-12-02 02:13:20 32 4
gpt4 key购买 nike

我是初学者,所以如果我的问题太基本,我深表歉意。距离我输入第一个 ggplot2 命令已经过去了大约 4 天。我读过How can I apply a gradient fill to a geom_rect object in ggplot2?在发布之前,但这篇文章似乎专注于生成渐变矩形,我已经这样做了。

目标:我想强调哪些美国总统的失业率> 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"))

图形输出为:

Graph

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

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

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

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

<小时/>

更新:

这是数据的输出:

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"))

经济数据可通过 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

最佳答案

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="")

enter image description here

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

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

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

关于r - 在 ggplot2 中标记 geom_rect(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39177706/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com