用ggplot绘制多个方块 [英] Draw multiple squares with ggplot
问题描述
我正在尝试改变为ggplot的背景颜色。我想要实现的是如下所示的情节。我已经用 geom_ribbon 尝试过了,但是我只能定义ymin和ymax。这允许我只创建两个方格。
到目前为止使用的代码:
< (x,rnorm(10),y = rnorm(10))
ggplot(df)+
geom_point(aes(x, y)=
geom_ribbon(aes(x = x,ymin = min(y),ymax = 0),fill =red,alpha = .5)+
geom_ribbon(aes(x = x,ymin = min(0),ymax = max(y)),fill =blue,alpha = .5)
这是我想要得到的结果:
ggplot示例http://i50.tinypic.com/25j8p5z.png
感谢您的宝贵时间。
如果您想让背景颜色一直走到图的边缘,而不仅仅是数据的极端, :
ggplot(df)+
geom_rect(xmin = -Inf,xmax = 0,ymin = -Inf, ymax = 0,fill =red)+
geom_rect(xmin = 0,xmax = Inf,ymin = -Inf,ymax = 0, (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)
编辑:透明度
加入 alpha = 0.5 $ c除了使用
geom_rect
,即使没有引用原始的 df
数据框,$ c>也是工作的 ,为每一行 df
绘制了一个矩形(在这种情况下是10次)。设置 alpha = 0.01
表明存在透明度,但在预期水平的十分之一。该方法应该是用矩形进行注释;注释仅绘制一个geom实例,而不是原始数据框的每行一个:
ggplot(df)+
annotate(rect,xmin = -Inf,xmax = 0,ymin = -Inf,ymax = 0,fill =red,alpha = 0.5)+
注解(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)
I am trying to change to 'background' color of a ggplot. What I want to achieve is the plot shown below. I've already tried it with geom_ribbon but than I can only define ymin and ymax. This allows me to only create two squares.
The code I use so far:
df <- data.frame(x = rnorm(10), y = rnorm(10))
ggplot(df) +
geom_point(aes(x, y)) +
geom_ribbon(aes(x = x, ymin = min(y), ymax = 0), fill = "red", alpha = .5) +
geom_ribbon(aes(x = x, ymin = min(0), ymax = max(y)), fill = "blue", alpha = .5)
And this is the result I want to get:
ggplot example http://i50.tinypic.com/25j8p5z.png
Thanks for your time.
A variant on your answer, if you want the "background" colors to go all the way to the edge of the graph and not just to the extremes of the data:
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)
EDIT: transparency
Adding alpha=0.5
was "working" except for the problem that by using geom_rect
, even without referencing the original df
data frame, drew a rectangle for each row of df
(10 times, in this case). Setting alpha=0.01
shows that there is transparency, but at a 10th the expected level. The approach should have been to annotate with rectangles; annotations draw only a single instance of the geom, not one per row of the original data frame:
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)
这篇关于用ggplot绘制多个方块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!