Reputation: 4200
I have two overlaid plots like the following:
# data
df1 <-
structure(list(votepc = c(0, 0.673469387755102, 1.3469387755102, 2.02040816326531, 2.69387755102041, 3.36734693877551, 4.04081632653061, 4.71428571428571, 5.38775510204082, 6.06122448979592, 6.73469387755102, 7.40816326530612, 8.08163265306122, 8.75510204081633, 9.42857142857143, 10.1020408163265, 10.7755102040816, 11.4489795918367, 12.1224489795918, 12.7959183673469, 13.469387755102, 14.1428571428571, 14.8163265306122, 15.4897959183673, 16.1632653061224, 16.8367346938775, 17.5102040816327, 18.1836734693878, 18.8571428571429, 19.530612244898, 20.2040816326531, 20.8775510204082, 21.5510204081633, 22.2244897959184, 22.8979591836735, 23.5714285714286, 24.2448979591837, 24.9183673469388, 25.5918367346939, 26.265306122449, 26.9387755102041, 27.6122448979592, 28.2857142857143, 28.9591836734694, 29.6326530612245, 30.3061224489796, 30.9795918367347, 31.6530612244898, 32.3265306122449, 33),
onep_l = c(-3.04683912365502, -2.9288860192514, -2.80673920935709, -2.71068474218996, -2.59868878111915, -2.45880238481767, -2.37480332380559, -2.23460911688708, -2.10889054987307, -1.99047522047917, -1.87669853028435, -1.78043232361228, -1.66076809995624, -1.53296366632558, -1.41085191094303, -1.33664619413563, -1.25484911785156, -1.16637330346167, -1.09242033702463, -0.993233870603441, -0.868814797534722, -0.77302875745641, -0.701706681081065, -0.640947950059604, -0.609663458192963, -0.544829184436534, -0.513899902378802, -0.467220126876562, -0.398979818363064, -0.351495428547939, -0.301753873353469, -0.273343077541768, -0.246039966449235, -0.227086264490764, -0.209109319665317, -0.191089387865476, -0.173156552915555, -0.155188763096727, -0.137220973277898, -0.119253183459069, -0.101285393640241, -0.0833069891017477, -0.0584056132113643, -0.0393028396640721, -0.0202000661167799, -0.00109729256948771, 0.0256924282570783, 0.0425580183888852, 0.0562110280723889, 0.0753138016196812),
fivp_l = c(-2.49387426892006, -2.36974716428552, -2.24770162891638, -2.1388852684066, -2.03389263955278, -1.93394559835612, -1.81694187185014, -1.70041708728355, -1.60917342731475, -1.49179521440301, -1.38554027388695, -1.2828269899928, -1.18124762068931, -1.08662087862343, -0.973774490514442, -0.874759479578832, -0.800593190581749, -0.740785783611383, -0.652172657164487, -0.573450243069248, -0.515017429224766, -0.442912043582262, -0.383658887584902, -0.323341573462794, -0.258314472627628, -0.205595872935514, -0.142464773347548, -0.0781368908134405, -0.0281979476398045, 0.0152683648257071, 0.0803180759208437, 0.145684060700703, 0.182860027871694, 0.2363183904669, 0.258069397668474, 0.297763600704798, 0.334517510551307, 0.367774148972865, 0.419400721576291, 0.449637192561124, 0.513674212565069, 0.547916851040202, 0.593941355187954, 0.627584932802024, 0.661819919271989, 0.696215158184725, 0.722204042132779, 0.761601545009354, 0.785896156733814, 0.81555292340472),
median = c(-0.815044779565099, -0.743819763687241, -0.668147718288773, -0.585548583487774, -0.505988754433766, -0.431119846439899, -0.356486563100022, -0.282716406802397, -0.213645730304621, -0.138509706919591, -0.059975750037011, 0.0186730202061682, 0.0961481231683582, 0.169444244583764, 0.24700222334524, 0.326466607231962, 0.402301821964252, 0.483364436665088, 0.556584512770582, 0.632363004969792, 0.714375572928951, 0.793037468395403, 0.877870984426916, 0.962207611709838, 1.03356731008222, 1.10718491059314, 1.19091778408204, 1.26562272052417, 1.33254477864878, 1.400408632728, 1.47564102412404, 1.55608774607028, 1.63025938142047, 1.70942186399136, 1.78062294050334, 1.85771391900231, 1.93531441792801, 2.01186484535411, 2.08303978558049, 2.15951896614375, 2.23618663621212, 2.31396734998362, 2.38727890466078, 2.46022715664746, 2.53619915494825, 2.61247169435546, 2.69197984705106, 2.77223721469485, 2.84670421072328, 2.92572569583212),
fivp_u = c(0.764299748892833, 0.804765490028437, 0.854256504768956, 0.902289718256381, 0.945714401806161, 0.999764874108874, 1.04114220650953, 1.09869642579794, 1.13384320999936, 1.18120470491883, 1.21573101058197, 1.27445218428625, 1.33955162269848, 1.39815685944827, 1.45722714893296, 1.51856910923332, 1.58499640067278, 1.65887613783027, 1.72774433560421, 1.79620159515118, 1.87704543421692, 1.94716171864773, 2.02253169849082, 2.14641395884789, 2.22002130943306, 2.30065135585426, 2.38804631552905, 2.48899162568778, 2.59439697656147, 2.70312286702341, 2.80222435421368, 2.91453946494918, 3.01264339888567, 3.11328329408315, 3.2261870094282, 3.33178637365296, 3.43730759227164, 3.5573350268475, 3.66879134667206, 3.79999754925456, 3.91502805541851, 4.00783123050276, 4.13373332414793, 4.26788897861369, 4.37615979669942, 4.51222537396958, 4.62706390911616, 4.74722412757328, 4.85651749270624, 4.96053300145967),
onep_u = c(1.24217181417973, 1.24340012567997, 1.30632966997455, 1.31283597921233, 1.34664561172377, 1.40608130705138, 1.46639463505858, 1.55432035346093, 1.63060993657855, 1.70669140533282, 1.75150878790505, 1.79370613297722, 1.85971419079275, 1.90664005598401, 1.96816804968573, 2.05064010532441, 2.11033906679661, 2.16732187936881, 2.24341885584056, 2.26113961384522, 2.30626613883065, 2.35990365766008, 2.41958275920563, 2.52183642043004, 2.6273587426011, 2.65379312702584, 2.80741746945145, 2.88815347297832, 2.98483453742366, 3.08893075837908, 3.2206329735052, 3.35233518863132, 3.45100789223406, 3.53673713242526, 3.63137170530705, 3.73958509349264, 3.84330499283437, 3.98647303182275, 4.10457697800157, 4.2028181334765, 4.33391827458348, 4.45082955292471, 4.57842740080235, 4.71809010442487, 4.86428191138964, 5.02989334201345, 5.19486139483882, 5.3378992007234, 5.49199028055479, 5.63979086524642),
div = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
.Label = c("1", "2"), class = "factor")), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"))
df2 <- structure(list(c_vote_div_RADRGHT = c(28.8999999999991, 0, 5.88999999999985, 0, 4.61000000000026, 10.3799999999998, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26.600000000002, 0, 1.46999999999995, 3.30000000000011, 0, 0, 13.9000000000001, 4.61000000000026, 12.86, 0, 19.0999999999997, 13.4000000000009, 10.3799999999998, 10.3799999999998, 0, 17.6500000000005, 12.3899999999997, 0, 0, 0, 0, 13.4000000000009, 0, 0, 17.6500000000005, 0, 0, 12.3899999999997, 0, 20.5099999999998, 0, 11.5900000000001, 17.5, 17.6500000000005, 0, 0, 0, 4.61000000000026, 26.7300000000001, 0, 7.14000000000011, 12.3899999999997, 0, 0, 0, 0, 19.0999999999997, 0, 0, 0, 26.600000000002, 0, 10.3799999999998, 26.7300000000001, 0, 13.4000000000009, 0, 0, 0, 0, 4.75, 0, 19.0999999999997, 9.12000000000001, 0, 4.75, 26.600000000002, 0, 12.3899999999997, 4.61000000000026, 0, 5.88999999999985, 4.29000000000001, 0, 17.6500000000005, 26.600000000002, 0, 0, 0, 26.600000000002, 17.6500000000005, 0, 0, 0, 1.46999999999995, 0, 3.30000000000011, 0, 13.4000000000009, 3.30000000000011, 17.6500000000005, 4.61000000000026, 0, 0, 26.7300000000001, 0, 0, 0, 12.86, 0, 0, 0, 26.600000000002, 0, 0, 0, 4.61000000000026, 0, 12.86, 0, 19.0999999999997, 13.4000000000009, 0, 19.0999999999997, 12.86, 0, 0, 17.6500000000005, 0, 0, 0, 0, 0, 13.9000000000001, 13.4000000000009, 0, 0, 0, 0, 10.3799999999998, 0, 13.4000000000009, 26.7300000000001, 0, 19.0999999999997, 0, 0, 4.29000000000001, 4.61000000000026, 0, 0, 0, 4.29000000000001, 5.88999999999985, 13.4000000000009, 26.600000000002, 0, 0, 0, 10.3799999999998, 0, 0, 4.61000000000026, 0, 0, 0, 0, 0, 0, 0, 1.46999999999995, 0, 10.3799999999998, 4.29000000000001, 0, 3.30000000000011, 11.5900000000001, 0, 0, 0, 4.61000000000026, 4.29000000000001, 26.7300000000001, 4.75, 12.3899999999997, 3.30000000000011, 4.29000000000001, 0, 0, 0, 0, 19.0999999999997, 0, 4.61000000000026, 5.88999999999985, 12.86, 0, 0, 0, 26.7300000000001, 17.6500000000005, 0, 0, 3.30000000000011, 0, 0, 0, 4.29000000000001, 0, 0, 0, 4.61000000000026, 0, 17.6500000000005, 0, 19.0999999999997, 0, 0, 0, 0, 0, 0, 0, 1.46999999999995, 13.9000000000001, 1.46999999999995, 0, 0, 4.29000000000001, 13.4000000000009, 0, 4.61000000000026, 0, 26.600000000002, 0, 0, 0, 5.88999999999985, 0, 0, 13.9000000000001, 0, 0, 3.30000000000011, 13.4000000000009, 26.600000000002, 0, 13.4000000000009, 0, 0, 0, 11.5900000000001, 17.5, 0, 28.8999999999991, 0, 26.600000000002, 0, 0, 0, 0, 0, 0, 0, 28.8999999999991, 20.5099999999998, 26.7300000000001, 4.61000000000026, 4.29000000000001, 13.4000000000009, 0, 0, 13.4000000000009, 0, 0, 26.600000000002, 0, 0, 13.4000000000009, 0, 0, 0, 13.4000000000009, 19.0999999999997, 0, 0, 0, 0, 17.6500000000005, 5.88999999999985, 3.30000000000011, 26.600000000002, 0, 0)),
row.names = c(NA, -300L), class = c("tbl_df", "tbl", "data.frame"))
ggplot() +
geom_density(data = df2, aes(x = c_vote_div_RADRGHT, y = (-3.5 + 1.5 * (..scaled..))), outline.type = "upper", fill = "lightblue") +
geom_line(data = df1, aes(x = votepc, y = median)) +
geom_ribbon(data = df1, aes(x = votepc, ymin = onep_l, ymax = onep_u), fill = "grey", alpha = .125, lty = 0, lwd = .25) + # 99% CI
geom_ribbon(data = df1, aes(x = votepc, ymin = fivp_l, ymax = fivp_u), fill = "grey", alpha = .25, lty = 0, lwd = .01) + # 95% CI
geom_line() + geom_hline(linetype = "dotted", yintercept = 0) + # zero line
labs(title = "Title", x = "X values (and density)", y = "Marginal Effect of Z on Y")
However, since the density plot is shifted (which I want), the part fill = "lightblue"
in geom_density()
fills the area above the density plot, which I do not want (s. image).
How can I instead color the area below the (shifted) density plot (i.e. the black line)?
Many thanks in advance!
Upvotes: 1
Views: 81
Reputation: 2626
You could alternatively shift the relevant data up:
df1_shifted <- dplyr::mutate(df1, across(-c(votepc, div), ~ . + 3.5))
and then plot together with an unshifted density
ggplot() +
geom_density(data = df2, aes(x = c_vote_div_RADRGHT, y = (1.5 * (..scaled..))), outline.type = "upper", fill = "lightblue") +
geom_line(data = df1_shifted, aes(x = votepc, y = median)) +
geom_ribbon(data = df1_shifted, aes(x = votepc, ymin = onep_l, ymax = onep_u), fill = "grey", alpha = .125, lty = 0, lwd = .25) + # 99% CI
geom_ribbon(data = df1_shifted, aes(x = votepc, ymin = fivp_l, ymax = fivp_u), fill = "grey", alpha = .25, lty = 0, lwd = .01) + # 95% CI
labs(title = "Title", x = "X values (and density)", y = "Marginal Effect of Z on Y") +
and shift the scale and zero line back down:
geom_line() + geom_hline(linetype = "dotted", yintercept = 3.5) + # zero line
scale_y_continuous(labels = \(b) b - 3.5)
Result:
Upvotes: 1