geom_rect和alpha - 这是否与硬编码值一起工作? [英] geom_rect and alpha - does this work with hard coded values?

查看:105
本文介绍了geom_rect和alpha - 这是否与硬编码值一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相同的标题,尽管完全重写了这个问题。



为什么alpha在第一个图中工作,而不是第二个?我努力想知道为什么使用硬编码值的矩形是在正确的位置绘制的,但不是透明的,但是在data.frame中它可以按预期工作?

<$ p $ (mtcars $ cyl) - $因子(mtcars $ cyl)
mtcars $ am< - 因子(mtcars $ am)

ggplot(mtcars)+
geom_density(aes(x = disp,group = cyl,fill = cyl),alpha = 0.6,adjust = 0.75)+
geom_rect(data = data.frame(xmin = 100,xmax = 200,ymin = 0,ymax = Inf),aes(xmin = xmin,xmax = xmax,ymin = ymin,ymax = ymax),fill =red,alpha = 0.2)

ggplot(mtcars)+
geom_density(aes(xmin = 100,xmax = 200,ymin = 0,aes(x = disp,group = cyl,fill = y = Inf),fill =red,alpha = 0.2)


解决方案

感谢您澄清您的问题。这令我感到困惑,所以我去了谷歌,并最终学习东西新的(在他们的例子中解决了一些变幻莫测的问题后)。显然你正在做的是在彼此顶部绘制许多矩形,有效地消除了你想要的半透明。所以,解决这个问题的唯一方法是在一个单独的df中硬编码矩形坐标,或者...

(x)= 100,xmax(x)=(x = disp,group = cyl,fill = cyl),alpha = 0.6,adjust = 0.75)+
geom_density(data = mtcars,aes = 200,ymin = 0,ymax = Inf),alpha = 0.2,fill =red)

...只是不要将您的data.frame全局分配给图。相反,只在你想要的图层中使用它(在这个例子中, geom_density ),并保留其他图层无df!或者,甚至更好的办法是,使用注释来修改你的绘图在默认的df下:

  ggplot(mtcars)+ 
geom_density(aes(x = disp,group = cyl,fill = cyl),alpha = 0.6,adjust = 0.75)+
annotate(rect ,xmin = 100,xmax = 200,ymin = 0,ymax = Inf,alpha = 0.2,fill =red)

后一种方法可让您为整个绘图使用单个data.frame,因此不必为每个图层指定相同的df。



这两种方法都返回相同的图:


Same title, completely reworded the question though.

Why does the alpha work in the first plot but not the second? I'm struggling to see why with hardcoded values the rect is drawn in the right place but not made transparent but when in a data.frame it works as expected?

mtcars$cyl <- factor(mtcars$cyl)
mtcars$am <- factor(mtcars$am)

ggplot(mtcars) +
    geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) + 
    geom_rect(data=data.frame(xmin=100, xmax=200, ymin=0, ymax=Inf), aes(xmin=xmin, xmax=xmax, ymin=ymin,ymax=ymax), fill="red", alpha=0.2) 

ggplot(mtcars) +
    geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) + 
    geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2) 

解决方案

Thanks for clarifying your question. This was puzzling to me, so I went to google, and ended up learning something new (after working around some vagaries in their examples). Apparently what you are doing is drawing many rectangles on top of each other, effectively nullifying the semi-transparency you want. So, the only ways to overcome this are to hard-code the rectangle coordinates in a separate df, or...

ggplot() + 
  geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
  geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), alpha=0.2, fill="red")

... just don't assign your data.frame globally to the plot. Instead, only use it in the layer(s) you want (in this example, geom_density), and leave the other layers df-free! Or, even better yet, Use annotate to modify your plot out from under the default df:

ggplot(mtcars) + 
  geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) + 
  annotate("rect", xmin=100, xmax=200, ymin=0, ymax=Inf, alpha=0.2, fill="red") 

The latter method enables you to use a single data.frame for the entire plot, so you don't have to specify the same df for each layer.

Both methods return identical plots:

这篇关于geom_rect和alpha - 这是否与硬编码值一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆