Density Plot based on specific tolerance bands (normalized) - r

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()

Related

CDF is not starting from zero

I have a data without any infinite value like below:
data<-c(6.87, 0.11, 0, 0.03, 0.08, 0, 0.01, 0, 0, 0.13, 0.17, 0, 0.53,
0.01, 2.69, 0, 0, 0, 0, 0, 0, 5.44, 4.71, 3.57, 0.38, 0.31, 0.45,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0.09, 0.43, 0, 0.23, 2.31, 0,
0.01, 0.96, 1.28, 0.07, 0, 0, 0, 0, 0.07, 0, 0, 0.03, 5.99, 0.08,
6.23, 2.95, 0.04, 0, 0.98, 0.03, 17.2, 0, 0, 0.25, 1.99, 0, 0,
0, 0, 0, 0, 0, 0, 0.01, 0, 0, 0, 1.94, 1.06, 0.05, 0, 0, 0, 0,
0, 0.02, 0, 0.02, 0.05, 0.66, 0.57, 0.54, 0, 0.11, 0.75, 0.65,
0, 7.35, 0.22, 0, 1.97, 0, 0, 0, 0, 0, 0, 0, 3.78, 0.28, 0.06,
0.09, 0.03, 0.01, 0.14, 0.02, 0, 1.82, 0.04, 0, 0.01, 0, 0, 0,
9.52, 10.38, 29.09, 0.1, 0.4, 0, 0.97, 0.57, 0.33, 0.16, 40.19,
1.06, 5.02, 0.01, 0.79, 4.78, 0.44, 15.29, 7.26, 0.05, 0.01,
6.47, 37.66, 0, 0.4, 0.15, 1.42, 0, 0.07, 0.01, 0, 0, 0.01, 7.43,
0, 0.03, 0.02, 0.15, 0.09, 0.44, 1.45, 0.03, 0, 0.01, 0.01, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.09, 0, 0.01, 0, 0, 0, 0, 0, 0, 0.04,
1.52, 0.01, 0, 0.27, 0.01, 1.11, 2.41, 0, 0, 0.15, 0, 0, 1.28,
0.02, 0.16, 0.17, 1.74, 3.75, 0.05, 0.18, 0.07, 0.39, 0, 0, 0,
0.45, 0.78, 0, 0, 0, 0.01, 0, 0, 0, 0, 0, 0.05, 0, 0, 0, 0, 0.09,
0.17, 0, 0.27, 0.04, 0.02, 0.88, 1.23, 0.01, 0.01, 0.13, 0.6,
0.02, 0.4, 0, 0, 0.09, 0, 0, 0, 0, 0, 0, 0, 0.12, 0, 0.02, 0.02,
0, 0.01, 0, 0, 0, 0, 0.05, 0, 0, 0, 2.71, 0.49, 0, 0, 0, 0.84,
0.04, 0, 0, 0.02, 0.06, 0, 0, 0.02, 0.15, 0, 0.48, 5.34, 5.23,
0.22, 2.37, 1.55, 3.29, 0, 0, 0.08, 0.03, 0.05, 0.06, 7.62, 0.84,
0.48, 0.04, 0, 0, 0, 0, 0.6, 0, 0.02, 0.08, 0, 0, 0, 0, 0, 0,
0, 0, 0.11, 0.1, 0, 0.01, 0, 0.24, 0, 2.4, 1.57, 0.12, 0.03,
0, 0, 0, 0, 0.08, 0.56, 0, 0.09, 0.01, 0.09, 0.01, 0, 0, 0, 0,
0, 0, 0, 0.01, 0, 0, 0, 0, 0, 0.03, 0, 0, 0, 0.38, 0.01, 0.03,
0.14, 0.03, 0, 0, 0, 0, 0, 0, 0, 0, 0.08, 0, 6.1, 0, 0, 0, 0.31,
0.01, 0.03, 0.2, 0.03, 0.02, 0.08, 0.12, 0, 5.79, 0.91, 0.95,
0, 0.39, 1.3, 0, 0, 0, 0, 0.01, 0, 0.64, 0.02, 0, 1.14, 0.24,
0, 0, 0.04, 0, 0, 0, 1.28, 0, 0.01, 0.02, 2.18, 0.05, 0, 0, 0,
0, 0, 0.01, 0.02, 0.19, 3.9, 0.01, 0.03, 0, 5.93, 0.88, 0, 0,
0, 0, 0.07, 0, 4.06, 0.41, 0.04, 0, 0, 0.08, 2.21, 0.03, 0, 8.07,
0, 0, 0.66, 1.73, 0.17, 0.02, 1.37, 4.68, 0.26, 0.09, 0.2, 0.03,
0, 0, 0, 0, 2.74, 0.1, 0.42, 0.65, 0.53, 0, 0, 0, 0, 0, 0.01,
0.16, 0.17, 0, 0, 0.01, 0, 0, 0, 0.6, 0, 0, 0.09, 0.01, 0, 0,
0.69, 0, 0, 0, 0, 0, 0, 0, 0, 0.01, 0.13, 0.54, 0, 0.16, 1.57,
0.12, 0, 0, 0, 0, 0.15, 0.18, 0, 0.86, 0.01, 0.01, 0, 0, 0, 0.01,
0, 0, 0, 0, 0, 0, 0, 0.03, 0.37, 0.18, 0, 0, 0, 0.08, 5.25, 1.1,
0.68, 0.14, 0.01, 0, 0, 0, 0, 0, 0, 0.02, 0, 0.04, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.95, 0.28, 0, 0, 0, 0, 0, 5.78,
3.05, 0.39, 1.65, 0, 0.03, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0.25, 0.78, 0, 0, 0, 0.85, 0, 0, 0, 0, 0, 0, 0, 0.34, 0.45, 0,
0, 0.02, 0.02, 0, 0, 0, 0.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.7,
0.69, 0, 0, 0, 0, 0, 0.02, 2.01, 0.05, 0, 0.77, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.01, 0.01, 0.02, 0.01, 0.79, 0.01, 0, 0.97, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0.69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.47, 0.02, 0, 0, 0, 0.13, 0.01, 0, 0, 0, 0, 0.33,
0.01, 0, 0, 0, 0, 0, 0, 0, 0.03, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0.01, 0, 0, 0.02, 0, 0.01, 12.31, 7.54, 0, 0,
0, 0.05, 0, 0, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 0, 0.01, 1.02, 0,
0, 0.54, 0.03, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0.01, 0.08, 0.03,
0, 0, 0, 0, 0, 0.03, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0.11, 0.25, 0.03, 0, 0, 0.09, 0, 0, 0.01,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.15, 1.41, 0, 0, 0, 0, 0)
When I try to plot CDF, curve is not starting from zero. It starts with 0.57. I read that infinite and zero values can cause this problem. To overcome this I replaced zeros with very small values.
data[data==0]<-0.000001
plot(ecdf(data), xlim = c(min(data),max(data)))
But still I am getting same result. Why is that happening?
I'm not sure it makes much sense to show empirical cdf for values outside of observed data. All values that are lower than the minimum of data will have ecdf of zero. You could try something like:
f = ecdf(data)
curve(f, min(data) - 0.01, max(data))
Like it is said in comments to the question 1 and 2, there are many zeros in your data, representing 57% of the total data points.
mean(data == 0)
#[1] 0.5794045
If you plot the ECDF and an horizontal line at that ordinate, you will see that the ECDF starts there.
plot(ecdf(data))
abline(h = mean(data == 0))

can I plot the dataframe cells as they are with colors?

I have the following dataframe (df). I want to make a graph that shows the same positions of the cells in the dataframe and having each cell with a color depending on the value (-1:1). if the value is zero then the color is blue, if the value is 1 or -1 then the value is red.
df<- structure(list(`0` = 1:8, `1` = c(0.885, 0.695, 0.828, 0.888,
0.823, 0.231, 0.599, 0.153), `2` = c(0.834, 0.68, 0.857, 0.802,
0.734, 0.205, 0.62, 0.044), `3` = c(0.854, 0.66, 0.83, 0.829,
0.729, 0.159, 0.559, 0.081), `4` = c(0.87, 0.583, 0.778, 0.853,
0.75, 0.087, 0.515, -0.011), `5` = c(0.922, 0.739, 0.787, 0.805,
0.635, -0.017, 0.498, -0.204), `6` = c(0.815, 0.535, 0.833, 0.784,
0.803, 0.092, 0.502, -0.419), `7` = c(0.859, 0.517, 0.8, 0.829,
0.557, 0.22, 0.368, -0.42), `8` = c(0.86, 0.701, 0.701, 0.786,
0.567, 0.414, 0.324, -0.396), `9` = c(0.774, 0.781, 0.805, 0.862,
0.405, 0.852, 0.1, -0.448), `10` = c(0.869, 0.788, 0.837, 0.838,
0.481, 0, -0.072, -0.48), `11` = c(0.816, 0.795, 0.807, 0.744,
0.217, 0, 0.096, -0.346), `12` = c(0.829, 0.792, 0.774, 0.778,
0.003, 0, 0, 0), `13` = c(0.799, 0.84, 0.775, 0.66, -0.024, 0,
0, 0), `14` = c(0.842, 0.765, 0.852, 0.679, 0, 0, 0, 0), `15` = c(0.804,
0.811, 0.818, 0.468, 0, 0, 0, 0), `16` = c(0.801, 0.757, 0.715,
0.091, 0, 0, 0, 0), `17` = c(0.807, 0.786, 0.799, -0.042, 0,
0, 0, 0), `18` = c(0.595, 0.795, 0.73, 0, 0, 0, 0, 0), `19` = c(0.822,
0.789, 0.623, 0, 0, 0, 0, 0), `20` = c(0.829, 0.822, 0.048, 0,
0, 0, 0, 0), `21` = c(0.805, 0.788, -0.205, 0, 0, 0, 0, 0), `22` = c(0.788,
0.791, -0.065, 0, 0, 0, 0, 0), `23` = c(0.839, 0.786, -0.217,
0, 0, 0, 0, 0), `24` = c(0.804, 0.815, 0, 0, 0, 0, 0, 0), `25` = c(0.789,
0.784, 0, 0, 0, 0, 0, 0), `26` = c(0.754, 0.787, 0, 0, 0, 0,
0, 0), `27` = c(0.832, 0.741, 0, 0, 0, 0, 0, 0), `28` = c(0.846,
0.778, 0, 0, 0, 0, 0, 0), `29` = c(0.797, 0.69, 0, 0, 0, 0, 0,
0), `30` = c(0.843, 0.644, 0, 0, 0, 0, 0, 0), `31` = c(0.825,
0.622, 0, 0, 0, 0, 0, 0), `32` = c(0.824, 0.726, 0, 0, 0, 0,
0, 0), `33` = c(0.749, 0.493, 0, 0, 0, 0, 0, 0), `34` = c(0.774,
-0.082, 0, 0, 0, 0, 0, 0), `35` = c(0.652, -0.255, 0, 0, 0, 0,
0, 0), `36` = c(0.833, 0, 0, 0, 0, 0, 0, 0), `37` = c(0.795,
0, 0, 0, 0, 0, 0, 0), `38` = c(0.864, 0, 0, 0, 0, 0, 0, 0), `39` = c(0.226,
0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -8L), class = "data.frame")
You don't have any cells that are -1 or 1, so there aren't any that would be red. You didn't mention what color the cells should be if they are neither -1, 0 or 1, so I have left these white:
library(ggplot2)
library(tidyr)
library(dplyr)
pivot_longer(df, -1) %>%
mutate(name = factor(as.numeric(name),
sort(unique(as.numeric(name))))) %>%
ggplot(aes(name, `0`, fill = ifelse(value == 0, "blue",
ifelse(abs(value) == 1, "red", "white")))) +
geom_tile(color = "black") +
scale_y_reverse(breaks = seq(nrow(df)), expand = c(0, 0)) +
scale_x_discrete(position = "top") +
scale_fill_identity() +
coord_equal() +
theme_classic() +
theme(axis.line = element_blank(),
axis.title = element_blank(),
legend.position = "none")
Edit
Update based on comment from OP:
library(ggplot2)
library(tidyr)
library(dplyr)
pivot_longer(df, -1) %>%
mutate(name = factor(as.numeric(name),
sort(unique(as.numeric(name))))) %>%
ggplot(aes(name, `0`, fill = value)) +
geom_tile(color = "black") +
scale_y_reverse(breaks = seq(nrow(df)), expand = c(0, 0)) +
scale_x_discrete(position = "top") +
scale_fill_gradient2(low = "red", mid = "blue", high = "red",
breaks = c(-1, 0, 1), limits = c(-1, 1)) +
coord_equal() +
theme_classic() +
theme(axis.line = element_blank(),
axis.title = element_blank())
legend.position = "none")

Error when using for loop in R: 'plot.new has not been called yet'

I have made a graph that works fine when I run the code without a for loop. When the same graph is a part of a for loop, I get the error message "plot.new has not been called yet" and no graph. How can I run this graph in a for loop?
There are very many questions about this error message. I have read more than 15 of them carefully and tried several things, but with no success.
For example, I looked at the problem described in plot.new has not been called yet. However, I cannot see how my code could close the device when I use a for loop, but not when it is run without the for loop.
The plot consists of one plot.xts() and then several lines().
GRA_events <- c("20080118/20080201", "20080123/20080206", "20080205/20080219", "20080210/20080224", "20080229/20080314", "20080912/20080926", "20081002/20081016", "20081206/20081220", "20081218/20090101", "20090209/20090223")
for (i in 1:length(GRA_events)) {
#i=1
period <- GRA_events[i]
png(paste("GRA_",i,".png", sep=""), width = 1200, height = 600)
windows(width = 16, height = 8)
# plot(0, 0, xlab="", ylab="")
par(mfrow=c(1,1), oma=c(4.5, 1.5, 4, 2.5), mar=c(0.1,4,0.1,0.1), cex=1, las=1)
plot.xts(GRA_mod$Qprecip, on=1,
main =period, ylim=c(0,10),
col = "blue", type="l", lwd=2,
ylab="Precip. & flow [m3/10min]",
yaxis.right = FALSE)
lines(GRA_mod$Qin, on=1, col = "black", type="l", lwd = 2)
addLegend("topright", on=1,
legend.names = c("Flow (obs)", "Precipitation", "Flow (model)"),
lty=c(1, 1, 1), lwd=c(2, 2, 1),
col=c("black", "blue", "red"))
lines(GRA_all_part$Air.temp, on=NA, main =period, ylim=c(-15,25),
col = "red", type="l", lwd=2, ylab="Air temp. [degree C]")
lines(GRA_all_part$zero_line, on=2, col = "grey", lwd=2, lty=2)
lines(GRA_all_part$SnowDepth, on=2, main =period, col = "blue", lwd=2,
ylab="Snow depth")
addLegend("topright", on=2,
legend.names = c("Snow depth", "Air temperature"),
lty=c(1,1), lwd=c(2,2),
col=c("blue","red"))
lines(GRA_all_part$LW_ground, on=NA, main =period, col = "green",
lwd=2, ylim=c(165,400))
lines(GRA_all_part$LW_in, on=3, main =period, col = "black", lwd=2)
addLegend("topright", on=3,
legend.names = c("Longwave in", "Longwave ground"),
lty=c(1,1), lwd=c(1,1),
col=c("black","green"))
lines(GRA_all_part$SW_ground, on=NA, main =period, col = "red", lwd=2,
ylim=c(-2,425))
lines(GRA_all_part$Tot_rad, on=4, main =period, col = "black", lwd=3)
addLegend("topright", on=4,
legend.names = c("Total radiation", "Shortwave ground"),
lty=c(1,1), lwd=c(3,1),
col=c("black","red"))
title(main = "Available data", outer=TRUE)
dev.copy(which = 2)
dev.list()
dev.off()
}
When I skip the first (for (i in 1:length(GRA_events)) {) and the last row (}) in the code and instead start at i=1, the graph is nicely plotted both on screen and on a .png-file, see figure below. When I use the for loop, I get the error message.
Finally, some data to reproduce the error (much shorter, but it gives the same problem):
GRA_mod <- structure(c(0.05856, -0.07752, 0.099, -0.16374, 0.26532, -0.22224,
-0.05352, -0.06132, 0.18216, 0.01536, -0.00222, 0.07116, -0.00996,
-0.04068, 0.14706, 0.00318, -0.04626, -0.0105, -0.09654, 0.00762,
0.1026, 0.01314, -0.08766, 0.03522, -0.03792, -0.00972, -0.00174,
-0.0354, 0.07428, -0.1035, -0.00468, 0.05442, 0.01062, 0.06954,
-0.04374, -0.02568, -0.0435, -0.02772, 0.05766, 0.03222, -0.02136,
-0.04164, 0.10182, -0.01296, 0.0675, 0.021, 0.08514, 0.02994,
-0.02862, 0.03162, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), class = c("xts",
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "GMT", tclass = c("POSIXct",
"POSIXt"), tzone = "GMT", index = structure(c(1202169600, 1202170200,
1202170800, 1202171400, 1202172000, 1202172600, 1202173200, 1202173800,
1202174400, 1202175000, 1202175600, 1202176200, 1202176800, 1202177400,
1202178000, 1202178600, 1202179200, 1202179800, 1202180400, 1202181000,
1202181600, 1202182200, 1202182800, 1202183400, 1202184000, 1202184600,
1202185200, 1202185800, 1202186400, 1202187000, 1202187600, 1202188200,
1202188800, 1202189400, 1202190000, 1202190600, 1202191200, 1202191800,
1202192400, 1202193000, 1202193600, 1202194200, 1202194800, 1202195400,
1202196000, 1202196600, 1202197200, 1202197800, 1202198400, 1202199000
), tzone = "GMT", tclass = c("POSIXct", "POSIXt")), .Dim = c(50L,
2L), .Dimnames = list(NULL, c("Qin", "Qprecip")))
GRA_all_part <- structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, -0.12,
-0.12, -0.12, -0.12, -0.12, -0.12, -0.07, -0.07, -0.07, -0.07,
-0.07, -0.07, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, -0.08, -0.08,
-0.08, -0.08, -0.08, -0.08, -0.22, -0.22, -0.22, -0.22, -0.22,
-0.22, -0.18, -0.18, -0.18, -0.18, -0.18, -0.18, -0.2, -0.2,
-0.2, -0.2, -0.2, -0.2, -0.25, -0.25, 298.07, 298.07, 298.07,
298.07, 298.07, 298.07, 299.35, 299.35, 299.35, 299.35, 299.35,
299.35, 299.98, 299.98, 299.98, 299.98, 299.98, 299.98, 300.38,
300.38, 300.38, 300.38, 300.38, 300.38, 300.98, 300.98, 300.98,
300.98, 300.98, 300.98, 302.35, 302.35, 302.35, 302.35, 302.35,
302.35, 302.07, 302.07, 302.07, 302.07, 302.07, 302.07, 297.35,
297.35, 297.35, 297.35, 297.35, 297.35, 297.93, 297.93, 278.25,
278.25, 278.25, 278.25, 278.25, 278.25, 283.78, 283.78, 283.78,
283.78, 283.78, 283.78, 286.83, 286.83, 286.83, 286.83, 286.83,
286.83, 286.68, 286.68, 286.68, 286.68, 286.68, 286.68, 287.82,
287.82, 287.82, 287.82, 287.82, 287.82, 289.92, 289.92, 289.92,
289.92, 289.92, 289.92, 286.13, 286.13, 286.13, 286.13, 286.13,
286.13, 259.12, 259.12, 259.12, 259.12, 259.12, 259.12, 271.43,
271.43, -0.98, -0.98, -0.98, -0.98, -0.98, -0.98, -0.83, -0.83,
-0.83, -0.83, -0.83, -0.83, -0.67, -0.67, -0.67, -0.67, -0.67,
-0.67, -0.97, -0.97, -0.97, -0.97, -0.97, -0.97, -0.77, -0.77,
-0.77, -0.77, -0.77, -0.77, -0.85, -0.85, -0.85, -0.85, -0.85,
-0.85, -0.97, -0.97, -0.97, -0.97, -0.97, -0.97, -1, -1, -1,
-1, -1, -1, -0.97, -0.97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2.865, -2.73,
-2.704, -2.703, -2.67, -2.487, -2.447, -2.443, -2.303, -2.406,
-2.507, -2.506, -2.499, -2.475, -2.459, -2.383, -2.372, -2.425,
-2.512, -2.566, -2.457, -2.41, -2.413, -2.393, -2.393, -2.377,
-2.383, -2.366, -2.316, -2.3, -2.175, -2.022, -1.99, -1.914,
-1.898, -1.87, -1.845, -1.812, -1.841, -1.93, -1.998, -1.995,
-2.083, -2.253, -2.463, -2.633, -2.647, -2.446, -2.376, -2.596,
0.874, 0.874, 0.874, 0.873, 0.873, 0.873, 0.872, 0.873, 0.873,
0.872, 0.871, 0.871, 0.871, 0.871, 0.87, 0.87, 0.87, 0.87, 0.871,
0.87, 0.869, 0.87, 0.869, 0.869, 0.869, 0.868, 0.868, 0.868,
0.869, 0.868, 0.868, 0.868, 0.868, 0.868, 0.869, 0.869, 0.868,
0.868, 0.867, 0.869, 0.869, 0.868, 0.867, 0.866, 0.865, 0.864,
0.864, 0.865, 0.865, 0.866, -2.567, -2.48, -2.47, -2.474, -2.373,
-2.207, -2.245, -2.133, -2.059, -2.3, -2.35, -2.335, -2.313,
-2.308, -2.245, -2.165, -2.16, -2.255, -2.336, -2.334, -2.183,
-2.197, -2.149, -2.171, -2.135, -2.143, -2.114, -2.064, -1.999,
-1.955, -1.768, -1.659, -1.657, -1.625, -1.633, -1.597, -1.543,
-1.508, -1.531, -1.595, -1.532, -1.524, -1.67, -1.799, -1.9,
-2.025, -2.051, -2.01, -1.998, -2.136, 0.976, -1.292, 1.65, -2.729,
4.422, -3.704, -0.892, -1.022, 3.036, 0.256, -0.037, 1.186, -0.166,
-0.678, 2.451, 0.053, -0.771, -0.175, -1.609, 0.127, 1.71, 0.219,
-1.461, 0.587, -0.632, -0.162, -0.029, -0.59, 1.238, -1.725,
-0.078, 0.907, 0.177, 1.159, -0.729, -0.428, -0.725, -0.462,
0.961, 0.537, -0.356, -0.694, 1.697, -0.216, 1.125, 0.35, 1.419,
0.499, -0.477, 0.527, 0.195, 0.27, 0.199, 0.247, 0.237, 0.203,
0.224, 0.187, 0.191, 0.256, 0.242, 0.256, 0.242, 0.271, 0.22,
0.276, 0.215, 0.233, 0.251, 0.22, 0.26, 0.237, 0.232, 0.233,
0.224, 0.228, 0.233, 0.228, 0.215, 0.228, 0.237, 0.238, 0.216,
0.191, 0.22, 0.187, 0.203, 0.207, 0.199, 0.203, 0.22, 0.216,
0.229, 0.211, 0.216, 0.22, 0.229, 0.22, 0.211, 0.211, 1873, 1872,
1873, 1871, 1874, 1871, 1871, 1870, 1872, 1872, 1872, 1872, 1872,
1871, 1873, 1873, 1872, 1872, 1871, 1871, 1871, 1871, 1870, 1871,
1870, 1870, 1870, 1869, 1870, 1869, 1868, 1869, 1869, 1869, 1869,
1868, 1868, 1867, 1868, 1868, 1868, 1867, 1868, 1868, 1868, 1869,
1869, 1869, 1869, 1869, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), class = c("xts",
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "GMT", tclass = c("POSIXct",
"POSIXt"), tzone = "GMT", index = structure(c(1202169600, 1202170200,
1202170800, 1202171400, 1202172000, 1202172600, 1202173200, 1202173800,
1202174400, 1202175000, 1202175600, 1202176200, 1202176800, 1202177400,
1202178000, 1202178600, 1202179200, 1202179800, 1202180400, 1202181000,
1202181600, 1202182200, 1202182800, 1202183400, 1202184000, 1202184600,
1202185200, 1202185800, 1202186400, 1202187000, 1202187600, 1202188200,
1202188800, 1202189400, 1202190000, 1202190600, 1202191200, 1202191800,
1202192400, 1202193000, 1202193600, 1202194200, 1202194800, 1202195400,
1202196000, 1202196600, 1202197200, 1202197800, 1202198400, 1202199000
), tzone = "GMT", tclass = c("POSIXct", "POSIXt")), .Dim = c(50L,
15L), .Dimnames = list(NULL, c("SnowDepth", "SnowCover", "SnowCoverMountains",
"SW_ground", "LW_ground", "LW_in", "Tot_rad", "Precip", "Sun.temp",
"Soil.temp", "Air.temp", "Qin", "Qout", "V_med", "zero_line")))

How to get multiple sets of subset from a matrix for multiple conditions using loop?

I would like to ask a data manipulation question by R.
all_matrix<-structure(list(V1 = c(0.012, 0, 0, 0, 0.037, 0, 0, 0, 0.007, 0, 0.104, 0.149, 0.164, 0.258, 3.986, 0, 0.002, 0, 0, 0), V2 = c(0, 0.07, 0, 0, 0.017, 0, 0, 0, 0.025, 0, 2.322, 0.327, 0.134, 1.035, 2.732, 0.01, 1.097, 0.388, 0, 0), V3 = c(0, 0, 0, 0.005, 0, 0, 0, 0, 0, 0, 0.007, 0, 0, 0, 1.777, 0, 0.241, 0, 0, 0), V4 = c(0, 0, 0, 0.001, 0.003, 0, 0, 0, 0, 0, 0.207, 0.002, 0.003, 0.015, 0.032, 0, 0.007, 0, 0, 0), V5 = c(0, 0, 0, 0, 0.026, 0, 0, 0, 0.001, 0, 0.101, 0, 0, 0.005, 0.01, 0, 0, 0, 0, 0), V6 = c(0, 0, 0, 0.003, 0.009, 0, 0, 0, 0.076, 0, 0.01, 0.006, 0, 0.091, 0.829, 0, 0.002, 0, 0, 0), V7 = c(0, 0, 0, 0, 0.026, 0, 0, 0, 0.351, 0, 1.849, 0.003, 0, 0.005, 0.998, 0.009, 0.18, 0, 0, 0), V8 = c(0, 0, 0.002, 0.047, 0.01, 0.003, 0, 0, 0.021, 0, 0.848, 0.007, 0.005, 0.206, 0.023, 0, 0.025, 0, 0, 0), V9 = c(0, 0, 0, 0.02, 0.013, 0, 0, 0, 0, 0, 0.008, 0, 0, 0, 0, 0, 0, 0, 0, 0), V10 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.012, 9.895, 4.362, 0, 0, 0, 0, 0), P_diff = c(-4.051, -3.769, -3.602, -7.563, -6.398, -1.816, 0.84, -1.91, 3.095, -1.544, 6.068, 16.469, 6.403, 9.9, 9.089, 18.977, 14.123, 3.103, 1.527, -2.902), PH_fold = c(0.144, 0.511, 0.686, 0.372, 0.582, 0.325, 1.312, 0.436, 1.061, 0.371, 1.119, 1.298, 1.134, 1.146, 1.123, 1.484, 1.204, 1.263, 1.843, 0.423), PNH_fold = c(6.933, 1.955, 1.459, 2.69, 1.718, 3.081, 0.762, 2.291, 0.943, 2.696, 0.894, 0.77, 0.882, 0.872, 0.891, 0.674, 0.831, 0.792, 0.543, 2.366)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "P_diff", "PH_fold", "PNH_fold"), class = "data.frame", row.names = c("S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "S12", "S13", "S14", "S15", "S16", "S17", "S18", "S19", "S20"))
sig1<-data.frame(subset(all_matrix, all_matrix$PH_fold >= 1.1 & all_matrix$P_diff >=1))##conditions are in range like all_matrix$PH_fold >= 1.1,1.2,1.3,....,2 & all_matrix$P_diff >= 1,2,3,4......, upto 20###
sig2<-data.frame(subset(all_matrix, all_matrix$PNH_fold >= 1.1 & all_matrix$P_diff <= -1))##conditions are in range like all_matrix$PNH_fold >= 1.1,1.2,1.3,....,2 & all_matrix$P_diff >= -1,-2,-3,-4......, upto -20###
d <- function(x){sum((log(x[x>0]))*(x[x>0]))*(-1)}
sh1<-apply((sig1[,-c(5:7)]/100), 2, d)
sh2<-apply((sig2[,-c(5:7)]/100), 2, d)
count1<-apply(sig1[,-c(5:7)], 2, function(i) (sum(i > 0)))
count2<-apply(sig2[,-c(5:7)], 2, function(i) (sum(i > 0)))
G1<-(sig1*sh1)
G2<-(sig2*sh2)
G<-data.frame(G1/G2)
I would like to set a R code to calculate "G" for all subsets for all_matrix based on each condition as mentioned above.
So how can I get the subsets of a matrix using different conditions for each subset using loop for further process to calculate "G":
I want to use subset() function in loop:
Can anyone help me out?
Thank you!
I appreciate any replies!
If you have large dataframes, don't use subset, it's slower than any other option. The fastest is generally the indexing way.
I first create two indices with the conditions given. Then a list to hold the sub-dfs.
i1 <- data1$age < 15
i2 <- data1$age > 20
sub_data1 <- list()
sub_data1[['less_15']] <- data1[i1, ]
sub_data1[['over_20']] <- data1[i2, ]
Note that you can skip the indices creation code and simply do
sub_data1 <- list()
sub_data1[['less_15']] <- data1[data1$age < 15, ]
sub_data1[['over_20']] <- data1[data1$age > 20, ]

Scale is not shown on y-axis of plot

I am using the leaps package to generate the following plots:
> library(leaps)
>
>
> dput(datSel)
structure(list(oenb_dependent = c(1.0227039, -5.0683144, 0.6657713,
3.3161374, -2.1586704, -0.7833623, -0.2203209, 2.416144, -1.7625406,
-0.1565037, -7.9803936, 9.4594715, -4.8104584, 8.4827107, -6.1895262,
1.4288595, 1.4896459, -0.4198522, -5.1583964, 5.2502294, 1.0567102,
-1.0923342, -1.5852298, 0.6061936, -0.3752335, 2.5008664, -1.3999729,
2.2802166, -2.1468756, -1.4890328, -0.79254376, 3.21804705, -0.94407886,
-0.27802316, -0.20753079, -1.12610048, 2.0883735, -0.7424854,
0.44203729, -1.48905938, 1.39644424, -3.8917377, 11.25665848,
-9.22884035, 3.26856762, -0.00179541, -2.39664325, 4.00455574,
-5.60891295, 4.6556348, -4.40536951, 6.64234497, -7.34787319,
7.56303006, -8.23083674, 4.43247855, 1.31090412), gdp = c(-271.6,
-284.2, 34.3, -206, -253.1, -116.8, -169.9, -63.6, -174.2, -138.2,
-171.2, -198.2, -126.2, -222.5, -8.2, -172.5, -169.1, -207.5,
-114.6, -182.4, -43.7, 43.5, 166, 293.1, -30, -144.6, 16.9, -115.6,
-147.4, -189.1, -166.8, -157.9, -108.7, -150.9, -267.3, -176.2,
-231.3, -160.4, -251.5, -194.5, -186.3, -193, -171.6, -191.7,
-254.4, -140.6, -126.3, -66.7, -102.3, -100.4, -133.1, -61.8,
-1.1, -130.3, -35.8, -114.9, -79.1), employ = c(0.2237, -0.024,
0.0906, 0.2809, 0.0555, -0.2404, 0.1717, -0.1225, 0.0538, -0.1211,
-0.2819, 0.2998, -0.2625, -0.0808, 0.2338, -0.3807, -0.2774,
-0.0124, -0.2158, -0.1496, 0.0765, 0.2548, 0.2935, -0.129, 0.3021,
-0.2781, -0.4863, -0.0464, -0.5377, -0.0671, -0.5776, -0.1231,
-0.4383, -0.4593, -0.3337, -0.0388, -0.4048, -0.0609, -0.4173,
-0.1218, -0.1554, -0.1477, 2.4688, 0.1383, 0.1927, -0.1106, -0.1791,
-0.154, 0.1666, -0.0767, -0.3145, -0.1784, 0.2428, -0.0614, 0.0611,
-0.0804, 0.1366), atx = c(296.910157, 22.96997, 22.719932, -18.090049,
-304.469971, 128.03003, 49.19999, -311.47001, -114.390014, 183.710083,
-267.380059, 56.169976, 818.880004, 115.449952, 65.060068, -405.610117,
-262.829834, 355.199951, -138.44, 141.720029, -538.630127, -402.029907,
54.210005, 1016.93001, 1175.389892, -177.23999, 747.070088, 14.319805,
341.959961, -223.759766, -182.03, -595.19998, -122.550049, 394.110107,
-472.800078, -209.580049, -407.540039, -417.01001, -201.519902,
-388.510005, -53.469971, -122.640014, -321.61001, -193.259985,
-46.180054, -142.599976, -13.059985, -79.840039, 172.859985,
46.090054, -148.8, -56.290054, 122.75, -20.279907, -113.240039,
87.860034, -31.580078), un.employ = c(-0.0946, 0.0285, -0.1297,
-0.0563, -0.2938, 0.2474, -0.386, 0.1812, -0.2538, -0.4493, 0.4135,
-0.7771, 0.4232, 0.2375, -0.2525, 0.3409, 0.1633, -0.0739, 0.4948,
0.3698, -0.4075, -0.7342, -0.2505, -0.3096, -0.3006, 0.3804,
0.3246, 0.4871, 0.1521, -0.3552, 0.22, 0.0585, 0.2905, 0.1454,
0.2726, 0.0472, -0.0215, -0.6432, 0.4422, 0.0229, -0.0864, -0.35,
-0.7569, -0.2062, 0.0867, -0.1833, -0.2003, -0.0546, -0.1151,
0.3641, -0.3421, -0.1825, -0.023, -0.2115, -0.0344, 0.0293, -0.0332
), carReg = c(0.73435946, 0.24001161, 16.90532537, -14.60281976,
6.47603166, -8.35815849, 3.55576685, 7.10705794, -4.6955223,
10.9623709, 5.5801857, -6.4499936, -9.46196502, 9.36289122, -8.52630424,
5.45070994, -4.5346405, -2.26716538, 2.56870398, 0.013737, 5.7750101,
-27.1060826, 1.08977179, 4.94934712, 17.55391859, -13.91160577,
10.38981128, -11.81349246, -0.0831467, 2.79748237, 1.84865463,
-1.98736934, -6.24191695, 13.33602659, -3.86527871, 0.78720993,
4.73360651, -4.1674034, 9.37426802, -5.90660464, -0.4915792,
-5.84811629, 9.67648643, -6.96872719, -7.6535767, 0.24847595,
0.18685263, -2.28766949, 1.1544631, -3.87636933, -2.4731545,
4.33876671, 1.08836339, 5.64525271, 1.90743854, -3.94709355,
-0.84611324), cpi = c(1.16, -3.26, 0.22, -3.51, 0.84, -2.81,
-0.34, -4.57, -0.12, -3.95, -1.37, -2.73, 0.35, -5.38, -4.43,
-3.08, 0.74, -3.03, -1.09, -2, 0.35, -1.52, 1.28, 0.2, -0.25,
-4.55, -2.49, -4.24, -0.31, -2.96, -2.24, -0.46, -0.06, -2.67,
-1.27, -1.4, -0.7, -0.96, -2.18, -2.53, -0.52, -1.74, -2.18,
-1.4, -0.34, -0.09, -1.65, -1.15, -0.17, -2.01, -1.38, -1.24,
0.09, -2.44, -1.92, -2.61, -0.34), prodPrice = c(0.3, 0.8, 1.4,
0.5, 0, 2.3, 1, -0.1, 0.1, -0.4, -1.1, -0.4, -0.1, -3.9, -4.5,
-1.74, -3.48, -5.84, -1.92, 0.19, -1.1, 3.56, 3.57, 2.28, -4.11,
-3.01, -3.67, -1.74, -1.63, -2.02, -2.74, -0.73, -1.74, -3.19,
-1.56, -0.64, 1.36, 0.55, -5.38, -2.11, -3.37, -2.02, -1.74,
-0.01, 1.02, 1.73, -1.82, 0.36, 0.18, -0.64, 1.29, 2.1, 0.82,
-0.09, 1.83, -1.83, -2.83), productionConstr = c(0.7000584, 3.900325,
0.4000333, 1.0000834, -4.6003834, -6.50054172, 7.00058342, 3.2002667,
-4.6003834, 1.1000917, 1.3001083, -5.5004584, 2.3001917, -2.2001833,
-3.60030006, 2.70022502, 3.20026664, -2.0001666, 2.340195, 0.8700725,
0.8700725, 0.2900242, -1.740145, 0.6800566, -1.4501208, 9.8508209,
-6.5705476, -1.2501041, 2.8002333, 1.2501042, -1.3501125, -1.0600884,
-4.9304108, -3.28027339, 4.15034589, -4.34036172, 0.87007251,
-9.85082091, 3.81031753, 2.70022502, -3.96033003, -3.86032169,
2.12017668, -1.93016085, -0.3900325, 3.58029836, -12.66105509,
2.03016918, -0.3900325, -2.22018502, -0.0900075, 0.87007251,
-0.78006501, -0.67005584, 7.44062006, -6.48054005, -1.25010417
), constrPriceIndex = c(-0.3, -0.3, -1.42e-14, 0.2, 0.5, -0.7,
0.3, -0.1, 0.3, -0.9, -0.1, 0.8, -0.2, -0.2, -0.3, 0.2, -0.1,
-0.1, -0.16686, 0.41673, -0.08334, 0.16669, 0.25004, -0.33339,
-0.41673, -0.50009, 0.25004, 0.83348, -0.08335, -0.08334, -0.3334,
0.33339, 1e-05, 0.08334, -0.08335, 0.41674, 0.16669, -0.16669,
-0.13514, 0.15617, 1e-05, -0.46855, 0.15619, 0.54662, 0, -0.23426,
0.07808, 0.07809, 0.15618, -0.31236, 0.0781, 0.31235, -0.15618,
-0.23427, 0.07809, 1e-05, -0.0781), constrCostTotal = c(-0.5,
-0.7, -0.1, -0.06667, -0.16667, -0.6, -0.83333, -0.2, -0.33333,
-1, -1.06667, 0.16667, -0.36667, -0.23333, -1.18893, -0.30742,
-0.05589, -1.92836, -1.0061, -0.25153, -0.67073, -0.02795, 0.0559,
1.62094, -1.62094, -2.51526, -2.06809, 0.02795, -0.16769, -1.45325,
-1.14584, -0.41921, -1.64889, -1.87246, -1.03405, -0.67073, 0.11179,
-0.13974, -0.36695, -0.61157, -0.51373, -1.73687, -1.49225, -0.44033,
-0.48926, -0.88067, -0.6605, -0.04893, -0.12231, -0.83175, -0.34248,
0.1957, -0.12231, -0.78282, -0.29355, -0.44034, -0.39141), primConstTot = c(-0.33334,
-0.93333, -0.16667, -0.33333, -0.16667, -0.86666, -0.3, -0.4,
-0.26667, -1.56667, -0.73333, 0.1, -0.23333, -0.26667, -1.5774,
-0.19284, 0.38568, -2.42423, -0.93663, 0.08265, -0.63361, 0.0551,
-0.49587, 2.39668, -1.70798, -3.36085, -2.56196, 0.16529, 0,
-1.84572, -1.3774, -0.49586, -1.70798, -1.90081, -0.55096, -0.77134,
-0.16529, -0.30303, -0.17066, -0.23853, -0.64401, -1.52657, -1.57426,
-0.28623, -0.54861, -1.07336, -0.71558, 0.02385, -0.38164, -1.09721,
0, 0.14311, -0.38164, -1.02566, -0.42934, -0.35779, -0.4532),
baumeisterarbeit = c(-177L, -499L, -88L, -176L, -91L, -460L,
-160L, -213L, -142L, -835L, -391L, 54L, -125L, -143L, -831L,
-102L, 205L, -1291L, -501L, 45L, -338L, 30L, -264L, 1278L,
-911L, -1791L, -1365L, 87L, -9L, -974L, -734L, -264L, -910L,
-1013L, -317L, -382L, -102L, -165L, -89L, -127L, -344L, -812L,
-840L, -151L, -293L, -572L, -381L, 12L, -203L, -584L, -1L,
77L, -204L, -546L, -207L, -205L, -248L), gesamtbaukost = c(-274L,
-384L, -55L, -38L, -90L, -329L, -457L, -110L, -183L, -547L,
-586L, 92L, -202L, -127L, -676L, -168L, -30L, -1057L, -552L,
-138L, -368L, -15L, 32L, 887L, -888L, -1379L, -1134L, 16L,
-92L, -800L, -625L, -261L, -949L, -950L, -559L, -348L, 54L,
-93L, -214L, -336L, -282L, -953L, -816L, -242L, -268L, -483L,
-362L, -27L, -66L, -456L, -189L, 108L, -68L, -429L, -156L,
-235L, -225L), lohn = c(66831L, 66966L, 68594L, 69408L, 69408L,
69408L, 70858L, 71583L, 71583L, 71583L, 73167L, 73959L, 73959L,
73959L, 74575L, 74883L, 74883L, 74883L, 75983L, 76533L, 76533L,
76749L, 78321L, 79107L, 79107L, 79107L, 80423L, 81081L, 81081L,
81081L, 83007L, 83970L, 83970L, 83970L, 85794L, 86706L, 86706L,
86706L, 87566L, 87996L, 87996L, 87978L, 88270L, 88416L, 88416L,
88503L, 90779L, 91917L, 91917L, 91917L, 93727L, 94632L, 94632L,
94632L, 96090L, 96819L, 96819L), resProp.Dwell = c(0.8, -4,
-3.2, 2.7, -1.6, -1, -2.4, -0.4, -0.8, 1, -12.1, 0.2, -5.2,
3.7, -2.7, -1.7, 1.5, 0.7, -7.9, 0.3, 0.3, 1.4, -3.3, -1,
-1.6, 1.5, 0.5, 1.5, -1, -2.2, -3.5, 0.5, 0.5, -0.9, -0.4,
-3.4, 0.9, 0.1, -0.2, -2.8, -0.8, -6.2, 11.3, -4.6, 1, 1.1,
-1.7, 4.1, -5, 2.3, -2.3, 4.6, -6.3, 6.3, -6.9, 0, 2.4),
resProp.Dwell.1 = c(-0.4, -7.5, -1, -2.4, 0, 1.2, 0.7, -4.3,
0, 3.3, -18.3, 11.2, -4.9, 4.9, -0.3, -1.8, 2.7, 0.9, -10.8,
-2.6, 6.1, -0.1, -6.3, 1.2, 0.8, 4.1, -3.5, 4.6, -0.2, -2.7,
-15, 8, -0.1, -0.1, 0.4, -4.9, 0.5, 2.7, -2.5, 1.9, -4.6,
-1, 8.1, -4.5, 0.3, 0.7, 2.2, -0.5, -3.8, 1.8, -4.7, 5.9,
-2, 2.3, -0.4, -1.4, 2.3), resProp.Dwell.2 = c(1.3, -2.5,
-4.2, 5, -2.3, -1.9, -3.7, 1.2, -1.2, 0.1, -9.6, -4.4, -5.4,
3.2, -3.8, -1.5, 0.9, 0.7, -6.7, 1.5, -2.2, 2, -2, -1.8,
-2.7, 0.3, 2.3, 0.1, -1.2, -2.1, 1.5, -2.7, 0.7, -1.3, -0.7,
-2.7, 1, -1, 0.9, -4.9, 0.9, -8.5, 12.7, -4.7, 1.4, 1.2,
-3.4, 6.1, -5.4, 2.4, -1.2, 4, -8.1, 7.9, -9.6, 0.6, 2.4),
resProp.Dwell.3 = c(1.4, -2.5, -5.1, 3.6, -3, -3, -2.6, 1.5,
-1.3, -0.3, -9.2, -6, -6.6, 3, -4.4, -1.2, 1.1, 0.5, -7.1,
1.3, -1.3, 1.7, -1.6, -2.8, -3.5, 0.8, 2.9, 0, -0.3, -2.7,
2.3, -3.3, 1.8, -2, 0.4, -3.8, 1.1, -1.5, 1.3, -5.6, 2.2,
-9.7, 14.3, -5.7, 1.4, 1, -3.6, 7.3, -6.5, 3.1, -2.4, 4.2,
-7.9, 8.3, -10.2, 0.2, 3.6), resProp.Dwell.4 = c(0.9, -2.9,
2.7, 13.7, 3, 5.6, -12, -0.5, -0.2, 2.5, -12, 6.7, 3.1, 4.7,
0.2, -3.6, -0.6, 1.8, -3.6, 2.6, -8.2, 3.8, -4.5, 4.7, 3.4,
-3.4, -1.6, 1.1, -8.5, 2.4, -3.8, 1.4, -7.1, 3.4, -7.5, 4.3,
0.3, 1.9, -1.8, 0.7, -8.5, -0.7, 2.5, 2, 1.4, 2.7, -2.2,
-2, 1.7, -2.2, 6.9, 2.7, -8.8, 4.4, -5.4, 3.8, -5.7), cbre.indu.primeRent = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.05, 0.05, 0, 0.1, 0, 0.1,
0, 0.1, 0.05, 0, 0.05, 0.1, 0.1, 0, -0.25, -0.25, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.09, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), cbre.office.Capital.Value....m.. = c(-64.516129,
-133.83665, -67.861143, -128.947368, -64.43299, -63.11803,
-122.44898, -178.217822, -57.076296, -114.220445, -169.716206,
-52.197802, -57.142858, 0, -114.285714, -257.142857, -189.189189,
-42.953668, -42.193426, -41.453191, 11.441648, 232.919255,
198.701298, 687.160263, -3.906674, -126.31579, -126.315789,
-126.31579, -126.315789, -126.31579, -126.315789, -52.631579,
0, 0, 0, 0, 0, 0, -200, 0, 0, 0, 0, 0, 0, 0, -160, 0, 93.714286,
0, 0, 0, 0, 0, 0, 0, 0), cbre.office.PrimeRent = c(-0.25,
-0.25, 0, -0.25, 0, 0, -0.25, -0.25, 0, -0.25, -0.5, 0, -0.25,
0, -0.5, -0.5, -0.25, 0, 0, 0, 0.25, 0.5, 0.5, 0, -0.5, -0.5,
-0.5, -0.5, -0.5, -0.5, -0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.3, 0, 0.41, 0, 0, 0, 0, 0, 0, 0, 0),
cbre.office.primeYield = c(0, 0, 0.15, 0.15, 0.2, 0.2, 0.2,
0.25, 0.25, 0.25, 0.25, 0.2, 0.15, 0.1, 0.05, 0.15, 0.3,
0.35, 0.4, 0.3, 0.2, 0, -0.15, -0.85, -1, -0.85, -0.75, -0.1,
0, 0, 0, 0.05, 0.05, 0.05, 0.05, 0, 0, 0, 0.2, 0.2, 0.2,
0.2, 0, 0, 0, 0, 0.25, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0, 0,
0), cbre.retail.primeRent = c(0, 0, -5, -5, -2, -3, -4, -4,
-2, -1, 0, 0, -1, -1, -1, -3, -5, -5, -5, -5.5, -5, -5, -5,
-7.5, -8, -11, -13, -10, -9, -6, -4, -2, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, -0.33, -0.33, -0.33, -0.33, 0, 0,
0, 0, -7.26, -7.26, -7.26), cbre.retail.primeYield = c(5.25,
5.2, 5.25, 5.3, 5.35, 5.4, 5.4, 5.4, 5.45, 5.5, 5.5, 5.6,
5.65, 5.7, 5.85, 5.95, 6, 6.1, 6.2, 6.25, 6.25, 6, 5.75,
5.25, 5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.75, 4.8, 4.8, 5,
5, 5, 5, 5, 5, 5.25, 5.25, 5.75, 5.75, 5.75, 5.75, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6), cbre.retail.capitalValue = c(-1882.35294,
230.76923, -230.76923, -226.41509, -670.78117, -436.13707,
-222.22223, 0, -205.91233, -202.16847, 0, -393.5065, -403.91909,
-186.30647, -539.81107, -748.11463, -764.70588, -311.47541,
-301.42782, -627.09677, -480, 720, 782.6087, 645.96273, 251.42857,
1386.66667, -533.33334, -533.33333, -533.33333, 0, 0, -1024.56141,
-192.10526, 0, -730, 0, 0, 0, 0, 0, -834.28571, 0, -1450.93168,
0, 0, 0, -700.78261, 0, 0, 0, 0, 0, 0, 0, -1452, 0, 0)), .Names = c("oenb_dependent",
"gdp", "employ", "atx", "un.employ", "carReg", "cpi", "prodPrice",
"productionConstr", "constrPriceIndex", "constrCostTotal", "primConstTot",
"baumeisterarbeit", "gesamtbaukost", "lohn", "resProp.Dwell",
"resProp.Dwell.1", "resProp.Dwell.2", "resProp.Dwell.3", "resProp.Dwell.4",
"cbre.indu.primeRent", "cbre.office.Capital.Value....m..", "cbre.office.PrimeRent",
"cbre.office.primeYield", "cbre.retail.primeRent", "cbre.retail.primeYield",
"cbre.retail.capitalValue"), row.names = c(NA, -57L), class = "data.frame")
> leaps=regsubsets(datSel$oenb_dependent~.,
+ data=datSel, nbest=10)
> plot(leaps, scale="adjr2")
As you can see the text on the y-axis cannot be read. Any suggestions, how to change my plot that the numbers can be read?
I appreciate your replies!
The problem is in the leaps:::plot.regsubsets function that gets called when you plot the regsubsets object.
Line 30 is:
axis(2, at = 1:nmodels, labels = signif(yscale[index], 2))
Aside from modifying the source of the package there is not much to do.
A quick way is to use
fixInNamespace(plot.regsubsets, ns = "leaps")
And edit the function, for instance changing the line above to:
num.labs <- 10
at <- seq(1, nmodels, length.out = num.labs)
lab <- signif(yscale[index], 2)
lab <- lab[seq(1, length(lab), length.out = num.labs)]
axis(2, at = at, labels = lab)

Resources