你如何控制geom

编程入门 行业动态 更新时间:2024-10-28 14:29:25
本文介绍了你如何控制geom_rect()矩形的半透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在ggplot2图中绘制一个半透明的矩形:

library(ggplot2) min_date = min(df1 $ date)#dput(df1)低于 max_date = max(df1 $ date) ggplot(df1)+ geom_rect(aes(xmin = min_date,xmax = max_date, y)= -Inf,ymax = 3),fill =palegreen,alpha = 0.2)+ geom_line(aes(x = date,y = pb,color =P / B))+ geom_line(aes(x = date,y = return_index,color =return))

然而,与我已经完成的一些其他图表(从Hadley Wickham的书中复制)不同,绿色的矩形是完全不透明的:我无法在背景中看到淡灰色的线条。以下是它的样子:

I'm trying to draw a translucent rectangle in my ggplot2 graph:

library(ggplot2) min_date = min(df1$date) # dput(df1) below max_date = max(df1$date) ggplot(df1) + geom_rect(aes(xmin = min_date, xmax = max_date, ymin = -Inf, ymax = 3), fill = "palegreen", alpha = 0.2) + geom_line(aes(x = date, y = pb, colour = "P/B")) + geom_line(aes(x = date, y = return_index, colour = "return"))

However, unlike some other charts I've done (copied from Hadley Wickham's books), the green rectangle is completely opaque: I can't see the lightly shaded grey lines in the backgrounds. Here's what it looks like:

Furthermore, if I change the order of the layers (by putting geom_line before geom_rect), the rectangle hides the lines.

What subtlety did I get wrong? How do I make the green rectangle melt discretely into the background?

Here's my df1:

df1 = structure(list(date = structure(c(1335744000, 1380499200, 1464652800, 1356912000, 1485820800, 1490918400, 1383177600, 1461888000, 1454025600, 1367280000, 1343692800, 1401408000, 1330473600, 1391126400, 1459382400, 1404086400, 1417132800, 1477872000, 1469750400, 1443571200, 1419984000, 1438300800, 1346371200, 1369958400, 1483056000, 1440979200, 1424995200, 1377820800, 1388448000, 1375228800, 1480464000, 1359590400, 1354233600, 1412035200, 1427760000, 1385683200, 1467244800, 1472601600, 1372377600, 1475193600, 1333065600, 1435622400, 1409270400, 1396224000, 1488240000, 1364515200, 1340928000, 1406764800, 1456704000, 1430352000, 1338422400, 1348790400, 1351641600, 1432857600, 1327968000, 1448841600, 1398816000, 1446163200, 1362009600, 1422576000, 1451520000, 1414713600, 1393545600 ), class = c("POSIXct", "POSIXt")), pb = c(3.24284787690623, 2.35203304295562, 1.13562266384702, 2.90837861538151, 1.97393507382208, 1.79790256367522, 2.50992970378761, 1.2966057820916, 0.892051550643623, 2.56310397446516, 2.53722570614735, 2.42427665519818, 3.40294643294178, 2.2456624603825, 1.06554620628802, 2.12883927956712, 1.65800890792078, 1.71460655379306, 1.45450176074979, 1.28199154762022, 1.51004082825039, 1.59579220438853, 2.48072865275449, 2.52511938910325, 1.77197981412129, 1.4666225767599, 1.65482654263216, 2.24097337718875, 2.39207143276774, 2.18796717170196, 1.78667497794161, 2.95189774752025, 2.70906851093917, 1.8620615761957, 1.48932926967017, 2.40482981571083, 1.3614263004647, 1.5052848414737, 2.02094466655017, 1.67901881433697, 3.13131652724628, 1.7081053507639, 2.15479184551088, 2.37902994058881, 1.88440485774789, 2.57891658188723, 2.43424745762712, 2.25929464919641, 0.913664833729333, 1.58426153545149, 2.71711735504797, 2.59023788287105, 2.68172936708349, 1.5228439100185, 3.47144812971019, 1.07692509768545, 2.46172899067256, 1.34932598268774, 2.86559619320822, 1.43158577260698, 1.06755701001995, 1.87542832179586, 2.41716851824514), return_index = c(3.33963134143023, 3.53315257934844, 2.24983743575813, 3.54713517594007, 3.17031433226149, 2.92415007661754, 3.72288287285945, 2.43858382371356, 1.78853472205546, 3.17524563672478, 2.99957813429811, 3.72169243241355, 3.39125767791388, 3.614770311344, 1.98808399128776, 3.61004165944114, 3.16597358572951, 2.74562414401218, 2.30169956340851, 2.58899122167033, 3.00735830908446, 2.97573979012093, 2.9863799905072, 3.38703452069432, 2.98242176129961, 2.83290428019513, 3.44566574584198, 3.47136232987663, 3.75521536603366, 3.36372318786495, 2.90514490677928, 3.58331595473466, 3.28803779749728, 3.46781820411579, 3.2620117615886, 3.69607811617486, 2.19921609345358, 2.40895876306335, 3.04629884791247, 2.66358570431915, 3.25119873581604, 3.04330107396291, 3.68720702192003, 3.66737118374507, 2.97502296939418, 3.18097138521817, 2.95437617876303, 3.88945888388189, 1.81411076858085, 3.36085071796473, 3.00333046534821, 3.15899275395851, 3.27461188875339, 3.32892263614407, 3.47144812971019, 2.10766875686013, 3.79609591229452, 2.68219490565046, 3.54425781082805, 2.99623108334085, 2.08090136364801, 3.4771813132669, 3.79370144175553)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -63L), .Names = c("date", "pb", "return_index"))

解决方案

Basically geom_rect is drawing rectangles, one for each row, on top of each other, thus rendering the object opaque. Check this answer and this link.

Alternative 1 Using annotate instead of geom_rect

ggplot(df1) + annotate("rect", xmin = min_date, xmax = max_date, ymin = -Inf, ymax = 3, fill = "palegreen", alpha = 0.2) + geom_line(aes(x = date, y = pb, colour = "P/B")) + geom_line(aes(x = date, y = return_index, colour = "return"))

Alternative 2 Removing the argument data = df1 from ggplotand add it to the required layers.

ggplot() + geom_rect(aes(xmin = min_date, xmax = max_date, ymin = -Inf, ymax = 3), fill = "palegreen", alpha = 0.2) + geom_line(data= df1, aes(x = date, y = pb, colour = "P/B")) + geom_line(data= df1, aes(x = date, y = return_index, colour = "return"))

Output:

更多推荐

你如何控制geom

本文发布于:2023-11-05 10:53:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:geom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!