I'm having a huge data table with many different columns. Now I want to plot for some columns (EV
, EH
, ...) the densities based on a specific tolerance band (column pBu
). In my sample below you can see that I'm having five different tolerance bands (0.00, 0.05, 0.10, 0.15 and 0.20), so for my density plot EV I would have five different densities in one plot. In addition, the y
-axis should be normalized (range from 0 to 1).
How can I handle this with plotly
?
Here my data structure:
structure(list(Year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L), GPNRPlan = c(100236L, 101554L,
111328L, 124213L, 127434L, 128509L, 130058L, 130192L, 130224L,
130309L, 130342L, 130351L, 130392L, 130402L, 130843L, 132067L,
132597L, 133874L, 133943L, 133950L), TB.Info = c("Below TB",
"Over TB", "In TB", "In TB", "In TB", "Below TB", "Over TB",
"Below TB", "Below TB", "Below TB", "Over TB", "Over TB", "In TB",
"In TB", "In TB", "In TB", "In TB", "Over TB", "In TB", "Over TB"
), EV = c(-33498.61, 0, -31117.17, 70950.43, -37984.99, 0, 0,
-93820830.01, -7852622.13, -245712.8, 1800050, 320263371.77,
-803069.3, -731544.77, -45841.06, -19931.59, 62286.48, 14652139.32,
296249.92, 173414.12), EK = c(311746.83, 0, 408503.01, 965723.51,
447176.86, 0, 0, 237703401.51, 11650300.16, 761470.17, 15514898.49,
791067269.75, 35591131, 10754272.33, 9496742.11, 512370.9, 1134032.95,
35318984.4, 5630139.9, 1111511.07), EH = c(345245.44, 0, 439620.18,
894773.08, 485161.85, 0, 0, 331524231.52, 19502922.3, 1007182.97,
13714848.49, 470803897.97, 36394200.3, 11485817.1, 9542583.17,
532302.49, 1071746.46, 20666845.08, 5333889.99, 938096.94), Peff = c(104.78,
0, 91.52, 112.18, 113.39, 0, 0, 86.18, 101.04, 104.39, 106.23,
86.4, 96.19, 86.38, 113.5, 115.88, 104.61, 96.31, 95.6, 101.71
), Qeff = c(-0.01, 0, 0, 0, 0, 0, 0, 0, -0.01, -0.01, 0, 0, 0,
0, 0.01, 0, 0, 0, 0, 0), Meff = c(-7.35, 0, -2.16, -2.35, 1.9,
0, 0, 0.94, -8.28, -6.9, -0.57, 0, -0.63, -0.21, 10.89, -3.73,
-1.63, 0.69, 0.88, 3.29), G = c(104.7, 0, 91.51, 112.14, 113.46,
0, 0, 86.19, 100.99, 104.37, 106.2, 86.4, 96.19, 86.38, 113.67,
115.83, 104.61, 96.29, 95.61, 101.76), D = c(3262.07, 0, 4770.44,
7947.26, 4254.06, 0, 0, 3818668.12, 191836.45, 9612.18, 127995.67,
5426568.38, 377446.8, 132384.92, 83629.36, 4575.32, 10225.66,
212630.83, 55721.94, 9204.12), pP = c(24.86, 0, 28.34, 37.62,
30.53, 0, 0, 27.95, 28.54, 37.74, 26.96, 32.78, 37.09, 32.87,
29.64, 36.81, 39.14, 24.61, 40.53, 38.33), rP = c(1.05, 0, 0.64,
0.4, 0.65, Inf, Inf, 0.64, 0.63, 0.39, 0.92, 0.36, 0.23, 0.38,
0.6, 0.46, 0.2, 0.89, 0.12, 0.21), rQ = c(-0.11, Inf, -0.08,
0.07, -0.09, -0.04, 0.05, -0.29, -0.41, -0.25, 0.12, 0.66, -0.03,
-0.08, -0.02, -0.05, 0.05, 0.69, 0.04, 0.17), rE = c(0.91, 0,
0.6, 0.22, 0.52, Inf, Inf, 0.61, 0.59, 0.22, 0.76, 0.36, 0.21,
0.36, 0.54, 0.26, 0.15, 0.85, 0.1, 0.17), dEHdD = c(105.84, 0,
92.15, 112.59, 114.05, 0, 0, 86.82, 101.66, 104.78, 107.15, 86.76,
96.42, 86.76, 114.11, 116.34, 104.81, 97.2, 95.72, 101.92), dEHdzH = c(149.99,
0, 174.85, 250, 157.6, 0, 0, 140353.13, 6999.39, 299.99, 5499.95,
165545.1, 10399.12, 4079.6, 2999.81, 150, 274.98, 8998.95, 1399.89,
249.98), dEHdrP = c(3262.07, 0, 4770.44, 7947.26, 4254.06, 0,
0, 3818668.12, 191836.45, 9612.18, 127995.67, 5426568.38, 377446.8,
132384.92, 83629.36, 4575.32, 10225.66, 212630.83, 55721.94,
9204.12), dEKdzV = c(135, 0, 160.54, 267.07, 143.61, 0, 0, 133335.47,
6649.42, 284.99, 6049.94, 198654.12, 10058.17, 3772.52, 2951.88,
142.86, 287.99, 9448.9, 1461.51, 249.98), dEKdzTB = c(-1.13,
0, 0, 0, 0, 0, 0, -34015.93, -2514.12, -60.93, 109.72, 76326.42,
0, 0, 0, 0, 0, 5769.55, 0, 42.97), dEKdzH = c(135, 0, 160.54,
267.07, 143.61, 0, 0, 133335.47, 6649.42, 284.99, 6049.94, 198654.12,
10058.17, 3772.52, 2951.88, 142.86, 287.99, 9448.9, 1461.51,
249.98), dEKdrQ = c(348451.86, 0, 441620.17, 900981.67, 487701.61,
0, 0, 335596984.05, 19696287.61, 1017097.6, 13847581.34, 476478784,
36707738.02, 11575841.25, 9599815.68, 535717.91, 1080845.93,
20888792.33, 5386259.63, 948299.85), dEKdrP = c(3262.07, 0, 4770.44,
7947.26, 4254.06, 0, 0, 3818668.12, 191836.45, 9612.18, 127995.67,
5426568.38, 377446.8, 132384.92, 83629.36, 4575.32, 10225.66,
212630.83, 55721.94, 9204.12), dEKdrE = c(-24.56, 0, 0, 0, 0,
0, 0, -925490.96, -68906.02, -1952.36, 2553.36, 2501979.97, 0,
0, 0, 0, 0, 136325.23, 0, 1582.17), dEKdpP = c(133.87, 0, 160.54,
267.07, 143.61, 0, 0, 99319.54, 4135.3, 224.06, 6159.66, 274980.54,
10058.17, 3772.52, 2951.88, 142.86, 287.99, 15218.45, 1461.51,
292.95), dEKdD = c(95.57, 0, 85.63, 121.52, 105.12, 0, 0, 62.25,
60.73, 79.22, 121.21, 145.78, 94.29, 81.23, 113.56, 111.99, 110.9,
166.1, 101.04, 120.76), dEKdf = c(3327.91, 0, 4549.69, 10047.22,
4384.35, 0, 0, 2775981.18, 118021.52, 8456.02, 166064.35, 9013862.02,
373057.45, 124002.65, 87493.72, 5258.83, 11271.93, 374526.13,
59234.84, 11228.85), dEKdavgPh = c(-162.25, 0, -171.47, -328.66,
-170.41, 0, 0, -107505.38, -4532.49, -274.4, -7350.79, -287563.54,
-10693.86, -3995.68, -3297.26, -179.43, -314.73, -16536.87, -1543.22,
-317.73), dEVdzV = c(135, 0, 160.54, 267.07, 143.61, 0, 0, 133335.47,
6649.42, 284.99, 6049.94, 198654.12, 10058.17, 3772.52, 2951.88,
142.86, 287.99, 9448.9, 1461.51, 249.98), dEVdzTB = c(-1.13,
0, 0, 0, 0, 0, 0, -34015.93, -2514.12, -60.93, 109.72, 76326.42,
0, 0, 0, 0, 0, 5769.55, 0, 42.97), dEVdzH = c(-15, 0, -14.32,
17.07, -14, 0, 0, -7017.66, -349.97, -15, 549.99, 33109.02, -340.95,
-307.09, -47.93, -7.13, 13.01, 449.95, 61.62, 0), dEVdrQ = c(348451.86,
0, 441620.17, 900981.67, 487701.61, 0, 0, 335596984.05, 19696287.61,
1017097.6, 13847581.34, 476478784, 36707738.02, 11575841.25,
9599815.68, 535717.91, 1080845.93, 20888792.33, 5386259.63, 948299.85
), dEVdrE = c(-24.56, 0, 0, 0, 0, 0, 0, -925490.96, -68906.02,
-1952.36, 2553.36, 2501979.97, 0, 0, 0, 0, 0, 136325.23, 0, 1582.17
), dEVdpP = c(133.87, 0, 160.54, 267.07, 143.61, 0, 0, 99319.54,
4135.3, 224.06, 6159.66, 274980.54, 10058.17, 3772.52, 2951.88,
142.86, 287.99, 15218.45, 1461.51, 292.95), dEVdD = c(-10.27,
0, -6.52, 8.93, -8.93, 0, 0, -24.57, -40.93, -25.56, 14.06, 59.02,
-2.13, -5.53, -0.55, -4.36, 6.09, 68.91, 5.32, 18.84), dEVdf = c(3327.91,
0, 4549.69, 10047.22, 4384.35, 0, 0, 2775981.18, 118021.52, 8456.02,
166064.35, 9013862.02, 373057.45, 124002.65, 87493.72, 5258.83,
11271.93, 374526.13, 59234.84, 11228.85), dEVdavgPh = c(-162.25,
0, -171.47, -328.66, -170.41, 0, 0, -107505.38, -4532.49, -274.4,
-7350.79, -287563.54, -10693.86, -3995.68, -3297.26, -179.43,
-314.73, -16536.87, -1543.22, -317.73), dPeffdrP = c(-1.04, 0,
-0.92, -1.08, -1.11, -0.94, -1.13, -0.88, -1.03, -1.02, -1.04,
-0.88, -0.98, -0.88, -1.16, -1.11, -1.06, -0.96, -0.97, -1.04
), dMeffdrP = c(0.04, 0.82, 0.01, 0.03, -0.04, -0.05, -0.04,
-0.01, 0.03, 0.01, 0.01, 0, 0, 0, -0.11, 0.03, 0, 0.01, -0.01,
-0.04), type = c("J/B", "SPOT", "J/B", "J/B", "J/B", "J/B", "J*/B",
"KEINE", "J*/B", "J/B", "J*/B", "KEINE", "J*/B", "J/B", "J*/B",
"J/B", "J/B", "J*/B", "J*/B", "J/B"), pBu = c(0.1, 0, 0.05, 0.1,
0.1, 0.05, 0.1, 0, 0.1, 0.15, 0.1, 0, 0.1, 0.1, 0.2, 0.15, 0.1,
0.05, 0.2, 0.2), pBo = c(0.1, 0, 0.05, 0.1, 0.1, 0.05, 0.1, 0,
0.1, 0.15, 0.1, 0, 0.1, 0.1, 0.2, 0.15, 0.1, 0.05, 0.2, 0.2),
zTBu = c(1.5, 0, 1.5, 1.5, 1.5, 1.5, 1.5, 0, 1.5, 5.4, 1.5,
0, 1.5, 3, 1.5, 6.5, 1.5, 1.5, 1.5, 1.5), zTBo = c(1.5, 0,
1.5, 1.5, 1.5, 1.5, 1.5, 0, 1.5, 5.4, 1.5, 0, 1.5, 3, 1.5,
6.5, 1.5, 1.5, 1.5, 1.5), zV = c(1.5, 0, 1.5, 1.5, 1.5, 1,
0.8, 0, 0.5, 1.5, 0.8, 0, 1.5, 0, 0.8, 1.5, 1.5, 0.7, 1.5,
1.5), TP = c(35.19, 0, 37.24, 31.29, 28.73, 27.37, 23.83,
33.99, 27.28, 33.36, 30.41, 29.61, 31.41, 28.22, 23.92, 30.64,
32.98, 31.39, 25.77, 27.42)), class = c("data.table", "data.frame"
), row.names = c(NA, -20L), .internal.selfref = <pointer: 0x0000016781401ef0>)
library(ggplot2)
library(dplyr)
library(plotly)
(df %>%
mutate(pBu=as.factor(pBu)) %>%
ggplot(aes(x=EV)) +
geom_density(aes(y=..scaled..,color=pBu))) %>%
ggplotly()