I am looking for a way to plot a bar chart of two variables in which the beginning of y-axis must be at 100. Also, I need to color every bar that is under the value of 100 as a different color than the others.
Any comments about how can I do this?
ggplot(df, aes(fill=Year, y=variation, x=Year))
geom_bar(position="dodge", stat="identity", fill="yellowgreen")
theme_minimal(base_size = 15)
xlab("Year") ylab ("Variation (%)")
scale_x_continuous(breaks=seq(1995, 2021, 1))
Data:
dput(df$Year)
c(2016L, 2016L, 2016L, 2021L, 2021L, 2018L, 2018L, 2004L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2015L, 2015L, 2021L, 2021L,
2017L, 2017L, 2017L, 2017L, 2017L, 2021L, 2017L, 2017L, 2017L,
2020L, 2020L, 2020L, 2017L, 2017L, 2021L, 2021L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2011L, 2017L, 2017L, 2017L, 2017L,
2020L, 2018L, 2017L, 2021L, 2021L, 2021L, 2021L, 2021L, 2020L,
2020L, 2011L, 2018L, 2021L, 2014L, 2020L, 2008L, 2021L, 2021L,
2015L, 2015L, 2015L, 2015L, 2006L, 2006L, 2006L, 2006L, 2006L,
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2019L,
2019L, 2012L, 2014L, 2014L, 2014L, 2015L, 2015L, 2016L, 2016L,
2016L, 2015L, 2015L, 2021L, 2021L, 2021L, 2011L, 2009L, 2009L,
2020L, 2020L, 2020L, 2020L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2000L, 2000L,
2000L, 2000L, 2021L, 2021L, 2021L, 2021L, 2019L, 2016L, 2012L,
2012L, 2012L, 2012L, 2012L, 2012L, 2009L, 2009L, 2020L, 2020L,
2020L, 2020L, 2020L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2018L, 2018L, 2018L, 2007L, 2007L, 2007L, 2007L,
2021L, 2021L, 2021L, 2021L, 2011L, 2020L, 2014L, 2014L, 2007L,
2020L, 2020L, 2020L, 2020L, 2010L, 2010L, 2019L, 2019L, 2013L,
2017L, 2017L, 2017L, 2011L, 2011L, 2011L, 2011L, 2011L, 1995L,
2019L, 2019L, 2019L, 2005L, 2011L, 2020L, 2009L, 2009L, 2009L,
2009L, 2009L, 2009L, 2009L, 2012L, 2012L, 2016L, 2016L, 2021L,
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 1997L, 2003L, 2020L,
2002L, 2019L, 2006L, 2006L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2012L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L,
2017L, 2017L, 2014L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
2020L, 2019L, 1996L, 2018L, 2018L, 2018L, 2018L, 2018L, 2021L,
2018L, 2017L, 2017L, 2010L, 2010L, 2021L, 2013L, 2017L, 2017L,
2020L, 2020L, 2020L, 2017L, 2017L, 2017L, 2007L, 2017L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2019L, 2021L, 2021L, 2010L,
2010L, 2010L, 2014L, 2012L, 2012L, 2018L, 2007L, 2018L, 2020L,
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2018L, 2016L, 2016L,
2016L, 2021L, 2021L, 2018L, 2018L, 2004L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2015L, 2015L, 2021L, 2021L, 2017L, 2017L,
2017L, 2017L, 2017L, 2021L, 2017L, 2017L, 2017L, 2020L, 2020L,
2020L, 2017L, 2017L, 2021L, 2021L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2011L, 2017L, 2017L, 2017L, 2017L, 2020L, 2018L,
2017L, 2021L, 2021L, 2021L, 2021L, 2021L, 2020L, 2020L, 2011L,
2018L, 2021L, 2014L, 2020L, 2008L, 2021L, 2021L, 2015L, 2015L,
2015L, 2015L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L,
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2019L, 2019L, 2012L,
2014L, 2014L, 2014L, 2015L, 2015L, 2016L, 2016L, 2016L, 2015L,
2015L, 2021L, 2021L, 2021L, 2011L, 2009L, 2009L, 2020L, 2020L,
2020L, 2020L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2000L, 2000L, 2000L, 2000L,
2021L, 2021L, 2021L, 2021L, 2019L, 2016L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2009L, 2009L, 2020L, 2020L, 2020L, 2020L,
2020L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2018L, 2018L, 2018L, 2007L, 2007L, 2007L, 2007L, 2021L, 2021L,
2021L, 2021L, 2011L, 2020L, 2014L, 2014L, 2007L, 2020L, 2020L,
2020L, 2020L, 2010L, 2010L, 2019L, 2019L, 2013L, 2017L, 2017L,
2017L, 2011L, 2011L, 2011L, 2011L, 2011L, 1995L, 2019L, 2019L,
2019L, 2005L, 2011L, 2020L, 2009L, 2009L, 2009L, 2009L, 2009L,
2009L, 2009L, 2012L, 2012L, 2016L, 2016L, 2021L, 2009L, 2009L,
2009L, 2009L, 2009L, 2009L, 1997L, 2003L, 2020L, 2002L, 2019L,
2006L, 2006L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2012L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2017L, 2017L,
2014L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2020L, 2019L,
1996L, 2018L, 2018L, 2018L, 2018L, 2018L, 2021L, 2018L, 2017L,
2017L, 2010L, 2010L, 2021L, 2013L, 2017L, 2017L, 2020L, 2020L,
2020L, 2017L, 2017L, 2017L, 2007L, 2017L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2019L, 2021L, 2021L, 2010L, 2010L, 2010L,
2014L, 2012L, 2012L, 2018L, 2007L, 2018L, 2020L, 2009L, 2009L,
2009L, 2009L, 2009L, 2009L, 2018L)
> dput(df$variation)
c(170.3703704, 145.035461, 60251.77305, 162.8295484, 177.0086139,
545.2523364, 517.9754761, 306.2049062, 97.23113287, 134.2267248,
69.67656362, 87.18901916, 121.8741099, 74.09803434, 1955.555556,
399.7729484, 1388.058283, 317.3871431, 388.7711864, 640.8898305,
589.0889831, 466.1016949, 361.0169492, 16.97, 140, 6560, 2704.938272,
500, 15.79100992, 45.15469936, 194.2307692, 137.3333333, 1550,
14780, 275, 166.6666667, 120.8333333, 117.8571429, 71.42857143,
51.78571429, 613.3333333, 161.5384615, 129.3103448, 17739.13043,
9105.063291, 40186.2069, 201.4705882, 357.1428571, 295.9048027,
447.733022, 96.78120922, 121.9849836, 40.02713704, 28529.3765,
3895, 208.8757396, 272.972973, 46.15384615, 51.70575693, 225.0368583,
95.63953488, 149.6078431, 182.6923077, 36.85279188, 126.7676768,
140.8036589, 88.07424594, 590, 930, 268.1818182, 422.7272727,
223.0769231, 292.3076923, 241.6666667, 316.6666667, 285.8921162,
569.2946058, 150.7658643, 300.2188184, 56.1465721, 162.5, 170.2290076,
43.90243902, 276.5753425, 301.8867925, 408.3613289, 65.09287926,
299.3670886, 428.1481481, 541.5730337, 691.3580247, 472.2222222,
711.7647059, 67.68392371, 103.9509537, 59.59128065, 46.29032258,
398.2857143, 466.1904762, 1820.083682, 3392.547033, 4166.546763,
6263.285024, 2892.732139, 195.5684008, 925.9373079, 1216.662401,
3616.613924, 2696.190102, 1410.658307, 90.90909091, 146.3949843,
551.7241379, 1032.110092, 66.51376147, 107.1100917, 403.6697248,
3237.410072, 208.6330935, 335.971223, 1266.18705, 500, 25, 250,
250, 666.6666667, 33.33333333, 333.3333333, 333.3333333, 200,
10, 100, 100, 43.5483871, 42.1875, 43.5483871, 42.1875, 211.2745098,
133.8235294, 375.7142857, 242.8571429, 11824.14809, 111.7647059,
113.3507853, 75.39267016, 45.28795812, 48.16753927, 37.95811518,
42.67015707, 146.8899522, 133.3333333, 420.8333333, 820.8333333,
3066.666667, 979.1666667, 641.6666667, 144.6679317, 25.04743833,
49.88614801, 134.345351, 169.8056801, 58.89387145, 50.07473842,
201.1958146, 153.9896373, 112.3670213, 68.51088876, 27.83934267,
69.60376237, 281.0043786, 188.4642413, 749.7570457, 202.6239067,
1732.142857, 820.2380952, 78.93852872, 261.8798956, 553.9877301,
756.9832402, 122.8373702, 140.7407407, 396.2962963, 250.5154639,
939.1752577, 3097.902098, 1594.405594, 70.93596059, 40.40932889,
46.66666667, 439.7069597, 178.7545788, 77.28937729, 45.16129032,
57.14285714, 177.9661017, 131.8681319, 41.81484456, 279.714577,
32.30769231, 56.13540197, 74.85448196, 261.0983982, 194.7058824,
256.8, 156.25, 456.0096154, 659.8557692, 1049.278846, 76.92307692,
253.8461538, 430.6490385, 147.6043277, 109.3692649, 8507.633588,
538.2008155, 114.8288973, 379.6257796, 293.970894, 264.2766497,
146.0025381, 125.6339327, 93.31489166, 95.25756337, 43.758967,
586.7132867, 35.01723523, 146.8468468, 158.0152672, 34.35114504,
333.3333333, 833.3333333, 433.3333333, 666.6666667, 266.6666667,
294.1176471, 1076.470588, 488.2352941, 294.1176471, 294.1176471,
161.0655738, 291.5692554, 274.3684497, 4.255319149, 3.191489362,
1.861702128, 0.265957447, 0.265957447, 2.659574468, 4.787234043,
3.723404255, 204.5039164, 189.4345238, 1425.531915, 3051.06383,
453.9886773, 131.5789474, 281.5699659, 162.7906977, 174.1935484,
286.2745098, 109.5238095, 65.56286365, 206.7375887, 323.2, 214.9253731,
97.61388286, 160.4938272, 67.48148148, 72.53164557, 83.36162988,
159.4580511, 3.751133878, 5.202497199, 201.3636364, 299.0909091,
992.776541, 200, 1081.818182, 3372.727273, 1338.537344, 752.7251185,
112.8191489, 544, 188, 780, 585.5987796, 54.29782082, 377.3799127,
29.47598253, 151.3973799, 173.4061135, 131.1353712, 1697.8389,
132.6129666, 681.1394892, 780.1571709, 589.9803536, 442.9523321,
34.59764223, 177.7037417, 203.5366479, 153.9210661, 197.5609756,
2431.25, 3881.818182, 616.9341317, 4551.435407, 3759.482393,
300, 331.3555361, 367.3716012, 814.6445131, 320.6690562, 967.4074074,
56.52173913, 65.70935342, 101.5447154, 24587.5, 14475, 11975,
19162.5, 201.4606742, 170.3703704, 145.035461, 60251.77305, 162.8295484,
177.0086139, 545.2523364, 517.9754761, 306.2049062, 97.23113287,
134.2267248, 69.67656362, 87.18901916, 121.8741099, 74.09803434,
1955.555556, 399.7729484, 1388.058283, 317.3871431, 388.7711864,
640.8898305, 589.0889831, 466.1016949, 361.0169492, 16.97, 140,
6560, 2704.938272, 500, 15.79100992, 45.15469936, 194.2307692,
137.3333333, 1550, 14780, 275, 166.6666667, 120.8333333, 117.8571429,
71.42857143, 51.78571429, 613.3333333, 161.5384615, 129.3103448,
17739.13043, 9105.063291, 40186.2069, 201.4705882, 357.1428571,
295.9048027, 447.733022, 96.78120922, 121.9849836, 40.02713704,
28529.3765, 3895, 208.8757396, 272.972973, 46.15384615, 51.70575693,
225.0368583, 95.63953488, 149.6078431, 182.6923077, 36.85279188,
126.7676768, 140.8036589, 88.07424594, 590, 930, 268.1818182,
422.7272727, 223.0769231, 292.3076923, 241.6666667, 316.6666667,
285.8921162, 569.2946058, 150.7658643, 300.2188184, 56.1465721,
162.5, 170.2290076, 43.90243902, 276.5753425, 301.8867925, 408.3613289,
65.09287926, 299.3670886, 428.1481481, 541.5730337, 691.3580247,
472.2222222, 711.7647059, 67.68392371, 103.9509537, 59.59128065,
46.29032258, 398.2857143, 466.1904762, 1820.083682, 3392.547033,
4166.546763, 6263.285024, 2892.732139, 195.5684008, 925.9373079,
1216.662401, 3616.613924, 2696.190102, 1410.658307, 90.90909091,
146.3949843, 551.7241379, 1032.110092, 66.51376147, 107.1100917,
403.6697248, 3237.410072, 208.6330935, 335.971223, 1266.18705,
500, 25, 250, 250, 666.6666667, 33.33333333, 333.3333333, 333.3333333,
200, 10, 100, 100, 43.5483871, 42.1875, 43.5483871, 42.1875,
211.2745098, 133.8235294, 375.7142857, 242.8571429, 11824.14809,
111.7647059, 113.3507853, 75.39267016, 45.28795812, 48.16753927,
37.95811518, 42.67015707, 146.8899522, 133.3333333, 420.8333333,
820.8333333, 3066.666667, 979.1666667, 641.6666667, 144.6679317,
25.04743833, 49.88614801, 134.345351, 169.8056801, 58.89387145,
50.07473842, 201.1958146, 153.9896373, 112.3670213, 68.51088876,
27.83934267, 69.60376237, 281.0043786, 188.4642413, 749.7570457,
202.6239067, 1732.142857, 820.2380952, 78.93852872, 261.8798956,
553.9877301, 756.9832402, 122.8373702, 140.7407407, 396.2962963,
250.5154639, 939.1752577, 3097.902098, 1594.405594, 70.93596059,
40.40932889, 46.66666667, 439.7069597, 178.7545788, 77.28937729,
45.16129032, 57.14285714, 177.9661017, 131.8681319, 41.81484456,
279.714577, 32.30769231, 56.13540197, 74.85448196, 261.0983982,
194.7058824, 256.8, 156.25, 456.0096154, 659.8557692, 1049.278846,
76.92307692, 253.8461538, 430.6490385, 147.6043277, 109.3692649,
8507.633588, 538.2008155, 114.8288973, 379.6257796, 293.970894,
264.2766497, 146.0025381, 125.6339327, 93.31489166, 95.25756337,
43.758967, 586.7132867, 35.01723523, 146.8468468, 158.0152672,
34.35114504, 333.3333333, 833.3333333, 433.3333333, 666.6666667,
266.6666667, 294.1176471, 1076.470588, 488.2352941, 294.1176471,
294.1176471, 161.0655738, 291.5692554, 274.3684497, 4.255319149,
3.191489362, 1.861702128, 0.265957447, 0.265957447, 2.659574468,
4.787234043, 3.723404255, 204.5039164, 189.4345238, 1425.531915,
3051.06383, 453.9886773, 131.5789474, 281.5699659, 162.7906977,
174.1935484, 286.2745098, 109.5238095, 65.56286365, 206.7375887,
323.2, 214.9253731, 97.61388286, 160.4938272, 67.48148148, 72.53164557,
83.36162988, 159.4580511, 3.751133878, 5.202497199, 201.3636364,
299.0909091, 992.776541, 200, 1081.818182, 3372.727273, 1338.537344,
752.7251185, 112.8191489, 544, 188, 780, 585.5987796, 54.29782082,
377.3799127, 29.47598253, 151.3973799, 173.4061135, 131.1353712,
1697.8389, 132.6129666, 681.1394892, 780.1571709, 589.9803536,
442.9523321, 34.59764223, 177.7037417, 203.5366479, 153.9210661,
197.5609756, 2431.25, 3881.818182, 616.9341317, 4551.435407,
3759.482393, 300, 331.3555361, 367.3716012, 814.6445131, 320.6690562,
967.4074074, 56.52173913, 65.70935342, 101.5447154, 24587.5,
14475, 11975, 19162.5, 201.4606742)
Here's an example of what I'm looking for, but with 100 instead of 0 on the y-axis.
Edit:
I edited the axis breaks and limits to make the axis clearer but I'm still not sure if the info I need to be shown is accurately displayed. I have some data that are low as 1%, and these bars should be "hanging" upside down at the x-axis, showing values that are much lower than the 100% parameter at y axis, however, I can only visualize small-sized different colored lines (which I imagine that are these data). I suspect that data lower than 100% are not being proportionally shown. Is there another way to do it?
df %>%
mutate(var_tr = variation- 100, fill_color = as.factor(sign(var_tr))) %>%
ggplot()
geom_bar(aes(x=Year, y=var_tr, fill = fill_color), position="dodge", stat="identity")
guides(fill="none")
theme_minimal(base_size = 15)
xlab("Year")
ylab ("Variation (%)")
scale_y_continuous(breaks = seq(-100, 62000, 5000), labels = seq(-100, 62000, 5000))
scale_x_continuous(breaks=seq(1995, 2021, 1))
expand_limits(y = c(-100, 62000), x = c(1995, 2021))
And this is the plot I got:
CodePudding user response:
You can transform your variation
values by subtracting 100 from them, and then manipulating your axis to show the original values. Here, I made an example dataset. For the actual dataset, instead of hardcoding the break
, labels
, and limits
, you can use range()
, min()
, and max()
functions to have the plot set up.
data.frame(Year = c(2010, 2011, 2012, 2013, 2014),
variation = c(400, 200, 20, 100, 50)) -> df
library(dplyr)
library(ggplot2)
df %>%
mutate(var_tr = variation - 100,
fill_color = as.factor(sign(var_tr))) %>%
ggplot()
geom_bar(aes(x=Year, y=var_tr, fill = fill_color),
position="dodge", stat="identity")
guides(fill="none")
theme_minimal(base_size = 15)
xlab("Year")
ylab ("Variation (%)")
scale_y_continuous(breaks = seq(-100, 300, 100),
labels = seq(0, 400, 100))
scale_x_continuous(breaks=seq(2010, 2014, 1))
expand_limits(y = c(-100, 400),
x = c(2010, 2014))
For this specific case, where the data is skewed, we can use transformed scales on the y-axis. See the answer below for the data that OP provided in the question.
library(dplyr)
library(ggplot2)
library(ggallin)
threshold <- 100
df %>%
mutate(var_tr = variation - threshold,
fill_color = as.factor(sign(var_tr))) %>%
ggplot()
geom_bar(aes(x=Year, y=var_tr, fill = fill_color),
position="dodge", stat="identity")
guides(fill="none")
theme_classic(base_size = 15)
xlab("Year")
ylab ("Variation (%)")
scale_y_continuous(trans = ggallin::pseudolog10_trans,
breaks = c(-1e2, -10, 0, 10^seq(1,5,1)))
scale_x_continuous(breaks=seq(1995, 2020, 5))
expand_limits(x = c(1994, 2021))