我正在尝试在随后的geom_bar
中添加第二个ggplot
。我的目标是将Rb
列设为另一个geom_bar
或geom_line
,其alpha
值小于Ra
列。
仅添加geom_bar(aes(fill = "Rb"), data = Rb, alpha = 0.5) +
会给我一个错误。
RaRb_multiple_portfolio %>%
ggplot(aes(x = date, y = Ra)) +
geom_bar(stat = "identity", fill = palette_light()[[1]]) +
scale_y_continuous(labels = scales::percent) +
facet_grid(portfolio ~.)
数据:
RaRb_multiple_portfolio <- structure(list(portfolio = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), date = structure(c(14638, 14666, 14699, 14729, 14757, 14790,
14820, 14852, 14882, 14911, 14943, 14974, 15005, 15033, 15064,
15093, 15125, 15155, 15184, 15217, 15247, 15278, 15308, 15338,
15370, 15399, 15429, 15460, 15491, 15520, 15552, 15583, 15611,
15644, 15674, 15705, 15736, 15764, 15792, 15825, 15856, 15884,
15917, 15947, 15978, 16009, 16038, 16070, 16101, 16129, 16160,
16190, 16220, 16251, 16282, 16311, 16343, 16374, 16402, 16435,
16465, 16493, 16525, 16555, 16584, 16616, 16647, 16678, 16708,
16738, 16769, 16799, 14638, 14666, 14699, 14729, 14757, 14790,
14820, 14852, 14882, 14911, 14943, 14974, 15005, 15033, 15064,
15093, 15125, 15155, 15184, 15217, 15247, 15278, 15308, 15338,
15370, 15399, 15429, 15460, 15491, 15520, 15552, 15583, 15611,
15644, 15674, 15705, 15736, 15764, 15792, 15825, 15856, 15884,
15917, 15947, 15978, 16009, 16038, 16070, 16101, 16129, 16160,
16190, 16220, 16251, 16282, 16311, 16343, 16374, 16402, 16435,
16465, 16493, 16525, 16555, 16584, 16616, 16647, 16678, 16708,
16738, 16769, 16799, 14638, 14666, 14699, 14729, 14757, 14790,
14820, 14852, 14882, 14911, 14943, 14974, 15005, 15033, 15064,
15093, 15125, 15155, 15184, 15217, 15247, 15278, 15308, 15338,
15370, 15399, 15429, 15460, 15491, 15520, 15552, 15583, 15611,
15644, 15674, 15705, 15736, 15764, 15792, 15825, 15856, 15884,
15917, 15947, 15978, 16009, 16038, 16070, 16101, 16129, 16160,
16190, 16220, 16251, 16282, 16311, 16343, 16374, 16402, 16435,
16465, 16493, 16525, 16555, 16584, 16616, 16647, 16678, 16708,
16738, 16769, 16799), class = "Date"), Ra = c(-0.0948872608042938,
0.0302755216011101, 0.109509374168276, 0.0675884429320068, -0.00937566173026139,
-0.0442456567055521, 0.0263801851944143, 0.00318724997211461,
0.202759790762527, 0.0992464210546844, 0.0331912713453499, -0.015375554473906,
0.0868786183903849, 0.0100536551901418, 0.0286174602166953, -0.0274009342461045,
0.0477619553240034, -0.0350395337827284, 0.111243702738654, -0.0760670390870968,
-0.19473648040682, 0.0205694820429436, -0.054733098166583, 0.0682766389012968,
0.112508235502539, 0.0989678337814781, 0.0739776624058417, -0.0778936488279476,
-0.0424885430950294, 0.0138132067327883, 0.0370948419616735,
0.104220192269326, 0.0257977875313471, -0.0653758936725506, 0.0206765670321762,
-0.0331978796646172, 0.0459967665108327, 0.0552131951309991,
-0.000289038079323678, 0.0467567703212923, 0.0574042959901928,
-0.0617565836608712, 0.0973752248359905, 0.0791625271669534,
0.0269995748653737, 0.103737103529047, 0.0907495812344652, 0.0227121544730271,
0.00734696777318722, 0.0724242452161474, -0.0884597860603288,
-0.00180937982178941, 0.148447137163009, 0.0356352771121147,
-0.00188446228116335, 0.0719317528926942, -0.0220585944721102,
-0.0149739468100067, 0.012160709853553, -0.0480664578331269,
0.105034946851949, 0.0807185735469189, -0.0537784678404317, 0.08580369715499,
0.0578329095094572, -0.00413696087383086, 0.100908729437132,
-0.0264238451196098, -0.0531058562741711, 0.089221823269344,
0.0592775349591494, -0.0466140220194076, -0.0324543280284071,
0.040461500651837, 0.112948906250191, 0.150760867494087, 0.0366868965112026,
-0.0352642397698215, -0.00524457715189108, 0.0811196102370648,
0.23822541533366, 0.0857771251154598, 0.097767188809079, -0.0738324125707621,
0.141084465644355, -0.00912753868357619, 0.0806669069049688,
-0.0238096561235811, 0.100425089289185, -0.0325272656732213,
0.0637249660949819, -0.0934402133720926, -0.337635233332065,
-0.0764207019244058, -0.0964420083567047, 0.0690782425887693,
0.255651430673436, 0.0469073794799679, 0.0658709723418882, -0.136204976031786,
-0.0768141485880729, 0.0259548898084385, -0.00319379944001241,
0.0966554643766788, 0.00621880726581625, 0.00597054803924091,
0.0225199213670437, -0.00210743255682733, 0.209914414909479,
0.082359699913491, 0.0022708373568967, 0.0799221099691287, 0.053650716657119,
-0.0657343501353934, 0.122274188288198, 0.113508762579071, 0.0511809275516375,
0.0773256615040627, 0.109488529395578, 0.0153575853468548, 0.0478307825009991,
0.0803668250490346, -0.140404270833783, -0.0331368143380832,
0.205760745095413, 0.0433322211465355, -0.0173814954091196, 0.0963050099783751,
-0.036212751703251, -0.0601313777757405, -0.0346822267253157,
-0.0371457054216252, 0.171820009459403, 0.0791974722345823, -0.0804742630982458,
0.177970300303055, 0.0849689284363104, 0.0201851656659449, 0.150649345853857,
-0.0115063238958011, -0.0768285380389714, 0.069827400625452,
0.0948614176925398, -0.0513055357403434, -0.0882095451180497,
0.0553533269112185, 0.135987317934586, 0.112768021703555, -0.00466316241944587,
-0.0269002126492291, 0.017421971785416, -0.019946998528106, 0.187764361197549,
0.0717208192985805, 0.0487753913357556, 0.00236615928006989,
0.076915364139609, 0.0246973687074086, 0.0141854973337803, -0.00673938007813102,
0.026737247332576, -0.0343330903616014, 0.130167010138882, -0.0421103092631623,
-0.109563023670665, 0.0328171549012379, -0.0619865491776667,
0.0620552826724812, 0.144808445969794, 0.154907811958851, 0.0960348669070077,
-0.0474691222679526, -0.0226598502249128, 0.0130321279214851,
0.0387566481367436, 0.117787177486319, 0.0054546885463187, -0.0898503010772251,
0.0169193332162585, -0.0730824817238171, -0.0732128971382042,
0.0289687390744189, 0.00234984144111894, 0.0216247106160377,
0.061489398844679, -0.100346745106101, 0.13139929580948, 0.118998704501265,
-0.000205387455966988, 0.0930949464235868, 0.106704574863721,
0.0120557733655795, -0.0589037439937506, 0.0895251326595248,
-0.0334572821549963, 0.058021767567366, 0.137629179871578, 0.031893984951965,
0.0152946074201263, 0.0806838721571619, -0.0216268582656897,
0.0347441553511907, 0.0693658229516736, -0.0635439457799468,
0.0851319766483341, 0.0945598372557455, -0.0428018913420644,
0.0446834233222919, 0.0545981694880231, -0.0218008334235876,
0.0214174561915483, -0.0481914087679454, -0.0394526122093185,
0.0808773744123512, 0.0261753707834982, -0.0763661790968175),
Rb = c(-0.0992694593884564, 0.0348283591339307, 0.068397224023041,
0.0125545242303939, -0.0748183111654798, -0.0539663535880949,
0.0745098686261538, -0.0561129740863406, 0.11663645208132,
0.0577758856655917, -0.0164270534496109, 0.0555560159840449,
0.0317587422885386, 0.0219314575147931, -0.0155093936806331,
0.0260937443148994, -0.0104713610049483, -0.0248329580474735,
0.00428005545000443, -0.0530801127863052, -0.0306957786148545,
0.10211838816366, -0.0149939984161437, -0.00228910328047904,
0.062082681836837, 0.0710320179493622, 0.0451416718497148,
-0.0112731285333133, -0.0633802116125403, 0.0326371851134801,
0.0184477388779991, 0.0420367925900897, 0.015328060311864,
-0.0635743081781358, 0.00900588918253709, -0.00391439281607187,
0.019064226380394, 0.00782300140264014, 0.0257772573101906,
0.0175088012110325, 0.0279221139641526, -0.0289174450380663,
0.037266923999177, -0.0104002208069295, 0.0253315139403991,
0.0502496361173688, 0.030906496179854, 0.0353564107210347,
-0.0257415643531403, 0.0439399539485563, 0.00431102880164103,
0.0027509654850697, 0.0375860124063512, 0.0186891323323166,
0.0169489784849961, 0.0328207036117749, -0.00517606091553924,
0.0160400012135695, 0.0481005157889629, -0.0217560495827547,
-0.0350662958512303, 0.0799497195436767, -0.034345981037485,
0.0275097185252378, 0.018553368229401, -0.041130811530941,
0.0285023168047667, -0.0551901546607581, -0.0137080320838491,
0.105063160808759, 0.00710179158702462, -0.00686758604350579,
-0.0992694593884564, 0.0348283591339307, 0.068397224023041,
0.0125545242303939, -0.0748183111654798, -0.0539663535880949,
0.0745098686261538, -0.0561129740863406, 0.11663645208132,
0.0577758856655917, -0.0164270534496109, 0.0555560159840449,
0.0317587422885386, 0.0219314575147931, -0.0155093936806331,
0.0260937443148994, -0.0104713610049483, -0.0248329580474735,
0.00428005545000443, -0.0530801127863052, -0.0306957786148545,
0.10211838816366, -0.0149939984161437, -0.00228910328047904,
0.062082681836837, 0.0710320179493622, 0.0451416718497148,
-0.0112731285333133, -0.0633802116125403, 0.0326371851134801,
0.0184477388779991, 0.0420367925900897, 0.015328060311864,
-0.0635743081781358, 0.00900588918253709, -0.00391439281607187,
0.019064226380394, 0.00782300140264014, 0.0257772573101906,
0.0175088012110325, 0.0279221139641526, -0.0289174450380663,
0.037266923999177, -0.0104002208069295, 0.0253315139403991,
0.0502496361173688, 0.030906496179854, 0.0353564107210347,
-0.0257415643531403, 0.0439399539485563, 0.00431102880164103,
0.0027509654850697, 0.0375860124063512, 0.0186891323323166,
0.0169489784849961, 0.0328207036117749, -0.00517606091553924,
0.0160400012135695, 0.0481005157889629, -0.0217560495827547,
-0.0350662958512303, 0.0799497195436767, -0.034345981037485,
0.0275097185252378, 0.018553368229401, -0.041130811530941,
0.0285023168047667, -0.0551901546607581, -0.0137080320838491,
0.105063160808759, 0.00710179158702462, -0.00686758604350579,
-0.0992694593884564, 0.0348283591339307, 0.068397224023041,
0.0125545242303939, -0.0748183111654798, -0.0539663535880949,
0.0745098686261538, -0.0561129740863406, 0.11663645208132,
0.0577758856655917, -0.0164270534496109, 0.0555560159840449,
0.0317587422885386, 0.0219314575147931, -0.0155093936806331,
0.0260937443148994, -0.0104713610049483, -0.0248329580474735,
0.00428005545000443, -0.0530801127863052, -0.0306957786148545,
0.10211838816366, -0.0149939984161437, -0.00228910328047904,
0.062082681836837, 0.0710320179493622, 0.0451416718497148,
-0.0112731285333133, -0.0633802116125403, 0.0326371851134801,
0.0184477388779991, 0.0420367925900897, 0.015328060311864,
-0.0635743081781358, 0.00900588918253709, -0.00391439281607187,
0.019064226380394, 0.00782300140264014, 0.0257772573101906,
0.0175088012110325, 0.0279221139641526, -0.0289174450380663,
0.037266923999177, -0.0104002208069295, 0.0253315139403991,
0.0502496361173688, 0.030906496179854, 0.0353564107210347,
-0.0257415643531403, 0.0439399539485563, 0.00431102880164103,
0.0027509654850697, 0.0375860124063512, 0.0186891323323166,
0.0169489784849961, 0.0328207036117749, -0.00517606091553924,
0.0160400012135695, 0.0481005157889629, -0.0217560495827547,
-0.0350662958512303, 0.0799497195436767, -0.034345981037485,
0.0275097185252378, 0.018553368229401, -0.041130811530941,
0.0285023168047667, -0.0551901546607581, -0.0137080320838491,
0.105063160808759, 0.00710179158702462, -0.00686758604350579
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -216L), .Names = c("portfolio", "date",
"Ra", "Rb"), vars = "portfolio")
答案 0 :(得分:1)
是吗?
RaRb_multiple_portfolio %>%
gather("key", "value", -portfolio, -date) %>%
ggplot(aes(x = date, y = value, fill = as.factor(key))) +
geom_bar(stat = "identity", position = 'identity', alpha = 0.5) +
scale_y_continuous(labels = scales::percent) +
scale_fill_manual(values = palette_light()[1:2]) +
facet_grid(portfolio ~.)
首先,您需要在同一列(称为“值”)下收集值,变量“键”仅区分“ Ra”和“ Rb”,然后根据键绘制更改fill
我删除了palette_light()[[1]]
,因为可能是在您的环境中声明的。如果要使用scale_fill_manual()
更改颜色,可以放回去。只要确保它具有与键列中的类数相同的列数即可。