你的答案变型,如果你想要的“背景”的颜色,一路去到图形的边缘,不仅对数据的极端:
ggplot(df) +
geom_rect(xmin = -Inf, xmax = 0, ymin = -Inf, ymax = 0, fill = "red") +
geom_rect(xmin = 0, xmax = Inf, ymin = -Inf, ymax = 0, fill = "blue") +
geom_rect(xmin = 0, xmax = Inf, ymin = 0, ymax = Inf, fill = "green") +
geom_rect(xmin = -Inf, xmax = 0, ymin = 0, ymax = Inf, fill = "yellow") +
geom_point(aes(x, y), size = 5)
编辑:透明度
添加除了使用geom_rect
,即使没有参考原始数据帧,也为df
(在这种情况下为10次)的每一行绘制了一个矩形的问题,是“工作的”。设置alpha=0.01
表明存在透明度,但是在预期水平的十分之一处。该方法应该是用矩形进行注释;注释只画出一个实例GEOM的,每个原始数据帧的行不是一个:
ggplot(df) +
annotate("rect", xmin=-Inf, xmax=0, ymin=-Inf, ymax=0, fill="red", alpha=0.5) +
annotate("rect", xmin=0, xmax=Inf, ymin=-Inf, ymax=0, fill="blue", alpha=0.5) +
annotate("rect", xmin=0, xmax=Inf, ymin=0, ymax=Inf, fill="green", alpha=0.5) +
annotate("rect", xmin=-Inf, xmax=0, ymin=0, ymax=Inf, fill="yellow", alpha=0.5) +
geom_point(aes(x, y), size=5)
你可能在想'geom_rect'的? – joran 2013-04-09 14:13:50
你是对的!我盯着ggplot的文档页面已经很久了,但我完全错过了这个。 – jeroen81 2013-04-09 14:16:38