如何在单个图/地图中可视化三个参数的相对贡献?

huangapple go评论93阅读模式
英文:

How to visualise the relative contribution of 3 parameters in a single plot/map?

问题

I have a dataframe that contains a relative contribution of 3 parameters (please see below to get the reproducible dataframe). The contributions from each parameter add to 1. I want to make a single ggplot2 plot (map) where I can visualize which parameter contributes how much.

What I have tried so far:

ggplot2::ggplot(df, ggplot2::aes(x, y, fill = sum)) +
        ggplot2::geom_tile() +
        ggplot2::scale_fill_viridis_c() +
        ggplot2::coord_quickmap() +
        ggplot2::facet_grid(parameter~.)

which produces:

如何在单个图/地图中可视化三个参数的相对贡献?

As you can see, it is not so informative about how much exactly each parameter contributes. How can I visualize these data in a single plot with detailed information about the contribution value of each parameter?

Reproducible data:

> dput(df)
structure(list(parameter = c("a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c"), x = c(-73.5, 
-72.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -64.5, -63.5, 
-73.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -70.5, -69.5, 
-68.5, -67.5, -66.5, -65.5, -70.5, -69.5, -68.5

<details>
<summary>英文:</summary>

I have a `dataframe` that contains a relative contribution of 3 parameters (please see below to get the reproducible `dataframe`). The contributions from each parameter add to 1. I want to make a single `ggplot2` plot (map) where I can visualise which parameter contributes how much.

What I have tried so far:

ggplot2::ggplot(df, ggplot2::aes(x, y, fill = sum)) +
ggplot2::geom_tile() +
ggplot2::scale_fill_viridis_c() +
ggplot2::coord_quickmap() +
ggplot2::facet_grid(parameter~.)


which produces:
[![enter image description here][1]][1]
As you can see, it is not so informative about how much exactly each parameter contributes. How can I visualise these data in a single plot with detailed information about the contribution value of each parameter?
**Reproducible data:**

> dput(df)
structure(list(parameter = c("a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c",
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c",
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c",
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c",
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c",
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c"
), x = c(-73.5, -72.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5,
-64.5, -63.5, -73.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5,
-70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -70.5, -69.5, -68.5,
-67.5, -66.5, -65.5, -71.5, -70.5, -69.5, -68.5, -67.5, -66.5,
-73.5, -71.5, -70.5, -69.5, -68.5, -67.5, -74.5, -70.5, -69.5,
-68.5, -67.5, -70.5, -69.5, -68.5, -67.5, -66.5, -71.5, -70.5,
-69.5, -68.5, -67.5, -74.5, -71.5, -70.5, -69.5, -68.5, -74.5,
-71.5, -70.5, -69.5, -72.5, -71.5, -70.5, -69.5, -71.5, -70.5,
-69.5, -69.5, -68.5, -67.5, -69.5, -73.5, -72.5, -70.5, -69.5,
-68.5, -67.5, -66.5, -65.5, -64.5, -63.5, -73.5, -70.5, -69.5,
-68.5, -67.5, -66.5, -65.5, -70.5, -69.5, -68.5, -67.5, -66.5,
-65.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -71.5, -70.5,
-69.5, -68.5, -67.5, -66.5, -73.5, -71.5, -70.5, -69.5, -68.5,
-67.5, -74.5, -70.5, -69.5, -68.5, -67.5, -70.5, -69.5, -68.5,
-67.5, -66.5, -71.5, -70.5, -69.5, -68.5, -67.5, -74.5, -71.5,
-70.5, -69.5, -68.5, -74.5, -71.5, -70.5, -69.5, -72.5, -71.5,
-70.5, -69.5, -71.5, -70.5, -69.5, -69.5, -68.5, -67.5, -69.5,
-73.5, -72.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -64.5,
-63.5, -73.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -70.5,
-69.5, -68.5, -67.5, -66.5, -65.5, -70.5, -69.5, -68.5, -67.5,
-66.5, -65.5, -71.5, -70.5, -69.5, -68.5, -67.5, -66.5, -73.5,
-71.5, -70.5, -69.5, -68.5, -67.5, -74.5, -70.5, -69.5, -68.5,
-67.5, -70.5, -69.5, -68.5, -67.5, -66.5, -71.5, -70.5, -69.5,
-68.5, -67.5, -74.5, -71.5, -70.5, -69.5, -68.5, -74.5, -71.5,
-70.5, -69.5, -72.5, -71.5, -70.5, -69.5, -71.5, -70.5, -69.5,
-69.5, -68.5, -67.5, -69.5), y = c(-40.5, -40.5, -40.5, -40.5,
-40.5, -40.5, -40.5, -40.5, -40.5, -40.5, -41.5, -41.5, -41.5,
-41.5, -41.5, -41.5, -41.5, -42.5, -42.5, -42.5, -42.5, -42.5,
-42.5, -43.5, -43.5, -43.5, -43.5, -43.5, -43.5, -44.5, -44.5,
-44.5, -44.5, -44.5, -44.5, -45.5, -45.5, -45.5, -45.5, -45.5,
-45.5, -46.5, -46.5, -46.5, -46.5, -46.5, -47.5, -47.5, -47.5,
-47.5, -47.5, -48.5, -48.5, -48.5, -48.5, -48.5, -49.5, -49.5,
-49.5, -49.5, -49.5, -50.5, -50.5, -50.5, -50.5, -51.5, -51.5,
-51.5, -51.5, -52.5, -52.5, -52.5, -53.5, -53.5, -54.5, -55.5,
-40.5, -40.5, -40.5, -40.5, -40.5, -40.5, -40.5, -40.5, -40.5,
-40.5, -41.5, -41.5, -41.5, -41.5, -41.5, -41.5, -41.5, -42.5,
-42.5, -42.5, -42.5, -42.5, -42.5, -43.5, -43.5, -43.5, -43.5,
-43.5, -43.5, -44.5, -44.5, -44.5, -44.5, -44.5, -44.5, -45.5,
-45.5, -45.5, -45.5, -45.5, -45.5, -46.5, -46.5, -46.5, -46.5,
-46.5, -47.5, -47.5, -47.5, -47.5, -47.5, -48.5, -48.5, -48.5,
-48.5, -48.5, -49.5, -49.5, -49.5, -49.5, -49.5, -50.5, -50.5,
-50.5, -50.5, -51.5, -51.5, -51.5, -51.5, -52.5, -52.5, -52.5,
-53.5, -53.5, -54.5, -55.5, -40.5, -40.5, -40.5, -40.5, -40.5,
-40.5, -40.5, -40.5, -40.5, -40.5, -41.5, -41.5, -41.5, -41.5,
-41.5, -41.5, -41.5, -42.5, -42.5, -42.5, -42.5, -42.5, -42.5,
-43.5, -43.5, -43.5, -43.5, -43.5, -43.5, -44.5, -44.5, -44.5,
-44.5, -44.5, -44.5, -45.5, -45.5, -45.5, -45.5, -45.5, -45.5,
-46.5, -46.5, -46.5, -46.5, -46.5, -47.5, -47.5, -47.5, -47.5,
-47.5, -48.5, -48.5, -48.5, -48.5, -48.5, -49.5, -49.5, -49.5,
-49.5, -49.5, -50.5, -50.5, -50.5, -50.5, -51.5, -51.5, -51.5,
-51.5, -52.5, -52.5, -52.5, -53.5, -53.5, -54.5, -55.5), sum = c(0,
9.46581738186483e-05, 0.00696497150559271, 0.0137849902303819,
0.00643740569455534, 0.00199816358740475, 0, 0, 0, 5.06412179534512e-05,
0, 0.0398551254689963, 0.0310856391742892, 0.0195084547186265,
0.00122115866765995, 0.000262981842424393, 0.000728947548683327,
0.0180714589486645, 0.0217375647896753, 0.0302549061176794, 0.0121843041040885,
0.000681282606889494, 4.35093747206288e-05, 0.0529538781527676,
0.0273285713315873, 0.0110374092101145, 0.00537239572379284,
0.00472400342189244, 0.00147365976111469, 0.0259683795649571,
0.0463436806229692, 0.0267969428070688, 0.020294577628928, 0.00821922974468169,
0.00233230625628307, 0.00387696973516025, 0.0514313688652839,
0.0645759934429092, 0.0239569529899347, 0.027534701081013, 0.00534849512202001,
0.00128844338962426, 0.0426685507779651, 0.0284580643386483,
0.00971006844234354, 0.00420984206165692, 0.053985050339934,
0.0583984918343995, 0.0172826529268439, 0.0121423976240239, 0.00547793750320507,
0.0585686295112969, 0.0340311779773938, 0.0311015513254945, 0.0200707300297805,
0.00957150788475225, 0.00361466936278744, 0.0527542952718002,
0.0282832440286811, 0.0177806525711058, 0.00560265899046886,
0.0024828404080448, 0.0544720394354926, 0.0186537760879564, 0.0192277515814812,
0.00863003055518693, 0.0249790371500353, 0.015538215176512, 0.0348873548749255,
0.0206759639364579, 0.0139234477581141, 0.0205576837132205, 0.0404500818602786,
0.00653946544041233, 0.0223237332992225, 0.0075707047472664,
0.55973614025667, 0.5970202227548, 0.563477650463872, 0.513046833650201,
0.633380146860311, 0.653109185283395, 0.704255523998351, 0.596896242072055,
0.520747918302268, 0.526828987547501, 0.592212337251761, 0.590540320713732,
0.480335397306457, 0.631079557708342, 0.6667829974047, 0.64403959660305,
0.591513715061531, 0.572154253402492, 0.506765134717002, 0.664156380590966,
0.67826734460919, 0.649022944246854, 0.651215734871819, 0.488798113249405,
0.458045272783363, 0.693687132763554, 0.700613923064021, 0.751238707050007,
0.712027653396489, 0.532585134221796, 0.476479299998232, 0.490080331523103,
0.705251451061017, 0.735714385706559, 0.607922959590343, 0.613134683073939,
0.487692533841504, 0.548015750900016, 0.509952441369207, 0.74439726199284,
0.690392471171559, 0.588966236272818, 0.659362140614673, 0.646180042358758,
0.631416450548492, 0.585094155496678, 0.604202144807992, 0.647823791170224,
0.599329297279972, 0.725121813884829, 0.650627029395497, 0.542872019089378,
0.65883061118346, 0.66671626220512, 0.639294598531845, 0.622768680020925,
0.479380596605895, 0.580708446644663, 0.579879141984736, 0.617496249696512,
0.483541553869077, 0.474759409900493, 0.42759500296535, 0.444245282360467,
0.491426697536639, 0.543068445843191, 0.354391820625675, 0.285449114670143,
0.230667732146117, 0.545516189964805, 0.549909364018411, 0.518094993608081,
0.580561287344482, 0.591327797599958, 0.52693632685116, 0.64278661995468,
0.44026385974333, 0.402885119071382, 0.429557378030536, 0.473168176119417,
0.360182447445134, 0.3448926511292, 0.295744476001649, 0.403103757927945,
0.479252081697732, 0.473120371234546, 0.407787662748239, 0.369604553817271,
0.488578963519254, 0.349411987573031, 0.33199584392764, 0.355697421554526,
0.407757337389786, 0.409774287648843, 0.471497300493323, 0.305588713291354,
0.309548351286721, 0.350295773146257, 0.34874075575346, 0.458248008597827,
0.514626155885049, 0.295275458026332, 0.294013681212186, 0.244037289528101,
0.286498686842396, 0.441446486213247, 0.477177019378799, 0.483122725669829,
0.274453971310055, 0.256066384548759, 0.389744734153374, 0.3829883471909,
0.460876097293212, 0.387408255657075, 0.466090605640859, 0.228068036926147,
0.304259033706421, 0.409745320337558, 0.297969308607362, 0.325361893302593,
0.358873481009164, 0.410696002441665, 0.341812804852074, 0.293777716995377,
0.383388049793184, 0.262735788491147, 0.343895033101298, 0.398559351399325,
0.307138210839147, 0.302182186469385, 0.340634671438375, 0.367659812094323,
0.517004734031317, 0.366537258083536, 0.391837613986583, 0.364723097732382,
0.510855787140454, 0.522757749691462, 0.517932957599158, 0.537100941551577,
0.48934555088188, 0.448301523601622, 0.62062914222429, 0.699012670153345,
0.734444912978957, 0.433807846098737, 0.436167188223475, 0.461347322678698,
0.37898863079524, 0.40213273695963, 0.450739939849617, 0.349642675298054
)), row.names = c(NA, -228L), class = "data.frame")


[1]: https://i.stack.imgur.com/Muaaq.png
</details>
# 答案1
**得分**: 2
这是您提供的代码部分,我将为您翻译其中的文本部分:
"One way to do this is via color mixing. You assign a, b and c to red, green and blue, and calculate the RGB colour that represents the weighting of a, b and c."
"例如,在您的数据中,参数 a 的贡献非常小,因此我们预期大多数单元格应该位于绿色和蓝色之间:"
"如果我们将 `a` 乘以 15,我们可以更好地了解通常情况下这种图表可能会看起来如何(尽管与上面的图表不同,这 _不_ 代表您的数据 - 仅用于说明)"
"我们可以通过将填充颜色映射到 `parameter`,将 alpha 美学映射到 `sum` 并使用 `scale_alpha_identity` 来以较少的工作获得非常相似(尽管不够准确)的结果"
"另一种选择是使用定义在[此处](https://stackoverflow.com/questions/76709204/using-hexbins-to-show-proportion-of-categorical-variables-like-in-hextri/76750172#76750172)的 `geom_hextri`"
"**编辑**"
"要在图例中添加一个颜色键,您可能需要使用 patchwork 将三元图绘制到侧面:"
请注意,这是代码中的注释和说明部分的翻译,如果您需要进一步的翻译或解释,请告诉我。
<details>
<summary>英文:</summary>
One way to do this is via color mixing. You assign a, b and c to red, green and blue, and calculate the RGB colour that represents the weighting of a,b and c. 
For example, in your data there is very little contribution from parameter a, so we would expect most cells to be somewhere between green and blue:
``` r
library(tidyverse)
df %&gt;%
pivot_wider(names_from = parameter, values_from = sum) %&gt;%
mutate(col = rgb(a, b, c)) %&gt;%
ggplot(aes(x, y)) +
geom_point(aes(color = parameter), data = df) +
geom_tile(aes(fill = col), color = NA) +
scale_fill_identity() +
scale_color_manual(values = c(&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;)) +
guides(colour = guide_legend(override.aes = list(size = 10, shape = 15))) +
coord_equal() +
theme_minimal()

如何在单个图/地图中可视化三个参数的相对贡献?

If we were to multiply a by 15, we get a better idea of how such a plot might look in general (though unlike the plot above, this is not representative of your data - purely for illustration)

如何在单个图/地图中可视化三个参数的相对贡献?

We get a very similar (though less accurate) result with somewhat less effort by mapping the fill color to parameter and the alpha aesthetic to sum with scale_alpha_identity

df %&gt;%
  ggplot(aes(x, y)) +
  geom_tile(aes(fill = parameter, alpha = sum), color = NA) +
  scale_fill_manual(values = c(&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;)) +
  scale_alpha_identity() +
  coord_equal() +
  theme_minimal()

如何在单个图/地图中可视化三个参数的相对贡献?

An alternative is to use geom_hextri as defined here

df %&gt;%
  uncount(weights = round(sum * 10)) %&gt;%
  ggplot(aes(x, y, fill = parameter, group = parameter)) +
  geom_hextri(linewidth = 0.3, bins = 13, color = &#39;gray50&#39;) +
  scale_fill_brewer(palette = &quot;Spectral&quot;) +
  scale_alpha_identity() +
  theme_minimal() +
  scale_x_continuous(limits = c(-76, -62)) +
  scale_y_continuous(limits = c(-57, -39)) +
  theme(aspect.ratio = 1)

如何在单个图/地图中可视化三个参数的相对贡献?


Edit

To add a color key to the legend you might need to stitch a ternary plot onto the side using patchwork:

library(tidyverse)
library(ggtern)

main_plot &lt;- df %&gt;%
  pivot_wider(names_from = parameter, values_from = sum) %&gt;%
  mutate(col = rgb(a, b, c)) %&gt;%
  ggplot(aes(x, y)) +
  geom_tile(aes(fill = col), color = NA) +
  scale_fill_identity() +
  coord_equal() +
  theme_minimal()

df2 &lt;- expand.grid(a = seq(0, 1, 0.01), b = seq(0, 1, 0.01), c = seq(0, 1, 0.01))
df2 &lt;- df2[rowSums(df2) == 1,]
df2$rgb &lt;- rgb(df2$a, df2$b, df2$c)

legend_plot &lt;- ggtern(df2, aes(a, b, z = c, color = rgb)) +
  geom_point() +
  scale_color_identity() +
  scale_L_continuous(labels = function(x) character(length(x))) +
  ggtitle(&quot;Relative values&quot;) +
  theme(plot.title = element_text(hjust = 0.5),
        axis.text = element_blank())

library(patchwork)

main_plot + 
  wrap_elements(ggplot_gtable(ggplot_build(legend_plot))) +
  plot_layout(design = &quot;AAAA#
                        AAAAB
                        AAAA#&quot;)

如何在单个图/地图中可视化三个参数的相对贡献?

huangapple
  • 本文由 发表于 2023年8月4日 22:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定