相同的標題,雖然完全重寫了這個問題。geom_rect和alpha - 用硬編碼值工作嗎?
爲什麼alpha在第一個圖中工作,但不是第二個?我很努力地想知道爲什麼硬編碼值的矩形是在正確的位置繪製的,但是沒有變得透明,但是在data.frame中它按預期工作?
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)
只是一個補充。如果您將此與scale_y_continuous結合使用(例如limits = c(0.005,0.015)),請準確指定斷點,但不能有(ymin = 0,ymax = Inf),您的最小/最大值必須下降 – nzcoops
+1這解釋了我曾經用過ggplot的所有alpha規範問題! – geotheory
這太好了!但我仍然不明白爲什麼'geom_rect(...,alpha = .1)'doesn 't work but not'annotate(「rect」,... alpha = .1)' – Stuart