我是初学者所以如果我的问题太基础,我道歉。我输入第一个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"))
图表输出为:
正如我们所见,我能够为绿色矩形创建一个标签,显示失业&gt; 10000.但是,我对这种方法不满意,因为这只是一个快速修复(即我通过调整x,y,nudges等参数使其工作)。如果轴的比例变化怎么办?文本将被扭曲或可能不可见。我有两个问题:
问题1:无论如何我们可以通过编程方式标记绿色矩形,以便失业&gt; 10000?我不太关心我们是否使用文字,标签或图例。我假设使用geom_text()
需要快速修复(即大量运行和重新运行以确保通过不断调整x,y,vjust,hjust和nudges正确显示文本)但设置图例或标签可能是自动的。
问题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
答案 0 :(得分:2)
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="")
就你的第二个问题而言,颜色是垂直方向映射的,因为你的初始调用......
geom_rect(
aes(xmin = start, xmax = end, fill = party),
ymin = -Inf, ymax = Inf, alpha = 0.2,
data = presidential
)
...标识start
日期矩形的开始及其end
日期的结束,但对于y轴,尺寸设置为Inf
因此颜色垂直映射。