2

I have the following dataframe:

structure(list(STATE = c("AL", "AL", "AL", "AR", "AR", "AR", 
"AZ", "AZ", "AZ", "CA", "CA", "CA", "CO", "CO", "CO", "CT", "CT", 
"CT", "DC", "DC", "DC", "DE", "DE", "DE", "FL", "FL", "FL", "GA", 
"GA", "GA", "IA", "IA", "IA", "ID", "ID", "ID", "IL", "IL", "IL", 
"IN", "IN", "IN", "KS", "KS", "KS", "KY", "KY", "KY", "LA", "LA", 
"LA", "MA", "MA", "MA", "MD", "MD", "MD", "ME", "ME", "ME", "MI", 
"MI", "MI", "MN", "MN", "MN", "MO", "MO", "MO", "MS", "MS", "MS", 
"MT", "MT", "MT", "NC", "NC", "NC", "ND", "ND", "ND", "NE", "NE", 
"NE", "NH", "NH", "NH", "NJ", "NJ", "NJ", "NM", "NM", "NM", "NV", 
"NV", "NV", "NY", "NY", "NY", "OH", "OH", "OH", "OK", "OK", "OK", 
"OR", "OR", "OR", "PA", "PA", "PA", "RI", "RI", "RI", "SC", "SC", 
"SC", "SD", "SD", "SD", "TN", "TN", "TN", "TX", "TX", "TX", "UT", 
"UT", "UT", "VA", "VA", "VA", "VT", "VT", "VT", "WA", "WA", "WA", 
"WI", "WI", "WI", "WV", "WV", "WV", "WY", "WY", "WY"), STATEFP = c(1, 
1, 1, 5, 5, 5, 4, 4, 4, 6, 6, 6, 8, 8, 8, 9, 9, 9, 11, 11, 11, 
10, 10, 10, 12, 12, 12, 13, 13, 13, 19, 19, 19, 16, 16, 16, 17, 
17, 17, 18, 18, 18, 20, 20, 20, 21, 21, 21, 22, 22, 22, 25, 25, 
25, 24, 24, 24, 23, 23, 23, 26, 26, 26, 27, 27, 27, 29, 29, 29, 
28, 28, 28, 30, 30, 30, 37, 37, 37, 38, 38, 38, 31, 31, 31, 33, 
33, 33, 34, 34, 34, 35, 35, 35, 32, 32, 32, 36, 36, 36, 39, 39, 
39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 44, 44, 44, 45, 45, 45, 
46, 46, 46, 47, 47, 47, 48, 48, 48, 49, 49, 49, 51, 51, 51, 50, 
50, 50, 53, 53, 53, 55, 55, 55, 54, 54, 54, 56, 56, 56), USGS_source = c("desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS", "desalinatedRatio_USGS", 
"surfacewaterRatio_USGS", "groundwaterRatio_USGS"), USGS_ratio = c(0, 
0.952570177680636, 0.0474298223193641, 0, 0.320802874858715, 
0.679197125141285, 0.00118726161868536, 0.547498049247416, 0.451314689133899, 
0.413200045045025, 0.395270309964684, 0.191529644990291, 0.00049905298794486, 
0.828135128137654, 0.171365818874401, 0.738849458064565, 0.217035114820704, 
0.0441154271147311, 0, 1, 0, 0.219319890968065, 0.671092726949002, 
0.109587382082933, 0.656684963919327, 0.126404575163429, 0.216910460917244, 
0.00721908685014584, 0.783558213835537, 0.209222699314317, 0, 
0.770320218152936, 0.229679781847064, 0, 0.797942191638031, 0.202057808361969, 
0.00232043824409752, 0.916470894622416, 0.0812086671334866, 0, 
0.92906391704582, 0.0709360829541797, 0, 0.459616928200932, 0.540383071799068, 
0, 0.962210220307363, 0.0377897796926367, 0.00791452667669612, 
0.874049963130776, 0.118035510192528, 0.74784949701709, 0.18016831212937, 
0.0719821908535392, 0.851982222716137, 0.120827902353936, 0.0271898749299266, 
0.258014163251347, 0.563891506873498, 0.178094329875155, 9.97557746663829e-05, 
0.911527674866152, 0.0883725693591818, 0, 0.768152826816007, 
0.231847173183993, 0, 0.825116927277958, 0.174883072722042, 0.0182684128797566, 
0.218908120521597, 0.762823466598647, 0.00100194111547312, 0.970001756537505, 
0.028996302347022, 0.0579634145123682, 0.893375986555591, 0.0486605989320407, 
0, 0.921918131723707, 0.0780818682762933, 5.75248309686158e-06, 
0.445872145108443, 0.55412210240846, 0.692910205041557, 0.231516980236702, 
0.0755728147217416, 0.750863026988866, 0.189239607882342, 0.0598973651287914, 
0, 0.467477518628117, 0.532522481371883, 0, 0.55491909790311, 
0.44508090209689, 0.628459254436162, 0.314583822850021, 0.0569569227138165, 
0, 0.906722882768507, 0.093277117231493, 0.115084195289688, 0.629475609683959, 
0.255440195026353, 0, 0.74212722621406, 0.257872773785939, 6.09613637066308e-05, 
0.952408988267458, 0.0475300503688354, 0.639749970231766, 0.276824377473905, 
0.0834256522943283, 0, 0.951950168353979, 0.0480498316460212, 
0, 0.398457112334072, 0.601542887665928, 0, 0.959432337096909, 
0.0405676629030905, 0.153598289646049, 0.595004829227447, 0.251396881126504, 
0.0783655626387765, 0.758621255425437, 0.163013181935787, 0.190351227202889, 
0.788069145339214, 0.0215796274578968, 0, 0.915768779128974, 
0.0842312208710265, 0.00575793385656153, 0.699925282668951, 0.294316783474487, 
0, 0.848775758095281, 0.151224241904719, 8.38774046111902e-05, 
0.963858608369192, 0.0360575142261969, 0.0286812819524878, 0.820399654511115, 
0.150919063536397), WRI_source = c("desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI", "desalinatedRatio_WRI", "surfacewaterRatio_WRI", 
"groundwaterRatio_WRI"), WRI_ratio = c(0.000121328257814379, 
0.998940499339981, 0.000937910403304167, 0, 0.459016827498477, 
0.5409825553241, 2.43726652962837e-06, 0.814878333653747, 0.185120417731901, 
0.0176738871632668, 0.747059657005734, 0.235265995759084, 0, 
0.755264172166005, 0.244735037321488, 0.0136792616673887, 0.986317316975938, 
4.12235078989939e-06, 0, 0.999983837513132, 1.72500606266885e-05, 
0.00119822246884765, 0.995129671967869, 0.00367248916610427, 
0.00801816908927423, 0.876126253651518, 0.115856429745128, 0.000204308622609226, 
0.994406751933573, 0.00538913923949892, 0, 0.970800913622919, 
0.02919938144767, 0, 0.871221550565094, 0.128778228509228, 0, 
0.992998153798294, 0.00700178079496587, 0, 0.911679353258201, 
0.0883206934410107, 0, 0.515499446287411, 0.484500601921754, 
0, 0.941805757958306, 0.0581938130936716, 0.00315353217890028, 
0.766806130611631, 0.230040646422116, 0.00903108075751884, 0.990967848177376, 
1.24654781412517e-06, 8.48967030897762e-05, 0.997559582820827, 
0.0023555303238953, 0.00300967332512225, 0.996814645915884, 0.000177167053493157, 
0, 0.994364317281905, 0.00563575067091436, 0, 0.804239269566506, 
0.195760424483809, 0, 0.857754236535189, 0.142245746763031, 0.00010202884802059, 
0.821662494903841, 0.178235682561108, 0, 0.922485930166355, 0.0775143905070514, 
0.000597154463178288, 0.983992471125954, 0.0154104814338138, 
0, 0.924354227491903, 0.0756460863254646, 0, 0.235422477301902, 
0.764577512939301, 0.00231812515926591, 0.997679424958524, 1.13465977591128e-06, 
0.0217065482219761, 0.977821850191643, 0.000472106176686492, 
0, 0.580660508153354, 0.419340115052779, 0, 0.943344103307458, 
0.0566558644034664, 0.0192939670972122, 0.980690469353689, 1.54195901368175e-05, 
0, 0.784813721665075, 0.215186540007308, 0, 0.943375854048229, 
0.0566241219758288, 0.0015568786724111, 0.720358355194738, 0.278085078389351, 
0.00101906528187232, 0.997848102155814, 0.00113208539240895, 
0.0384737852504148, 0.961517317604918, 8.87923464564338e-06, 
0.000436929294591165, 0.998179330023561, 0.00138420373292034, 
0, 0.682087200302912, 0.31791248897456, 0, 0.99451921813481, 
0.00548055031152585, 0.00129762636349505, 0.692153736338985, 
0.306548773260811, 0, 0.770805009200711, 0.229195605033762, 0.0015375476408586, 
0.987438130351313, 0.0110244961564555, 0, 0.999999997292395, 
1.38871204486976e-08, 0.000452986743040026, 0.888655145906048, 
0.110892099480045, 0, 0.964371860007403, 0.0356278179174755, 
0, 0.999369010158845, 0.000630722833214213, 0, 0.866720460964825, 
0.133279676689783)), row.names = c(NA, -147L), class = c("tbl_df", 
"tbl", "data.frame"))

In this frame I listed for each STATE the ratios for "desalinated", "surface water" and "ground water" and I did this for two different datasets named USGS and WRI. I was able to plot the bar charts independently but not together.

So what I want is for each state the two bar charts plotted next to each other, so that you can compare them. And it should then look like this: example

Abdessabour Mtk
  • 3,895
  • 2
  • 14
  • 21
Roelalex1996
  • 53
  • 1
  • 7

2 Answers2

3

That's interesting... I tried doing it with ggplot2 (it's what I know), and found this limitation: the part that determines how the bars should present (stacked, side-by-side, etc.) only takes only argument – one configuration –, but there are two groupings. The best I could do was:

library(dplyr)
library(ggplot2)
df %>%
  pivot_longer(ends_with("ratio"), names_to = "Dataset", values_to = "Ratio") %>%
  pivot_longer(ends_with("source"), names_to = "Source", values_to = "Type") %>%
  mutate(Type = gsub("_USGS", "", gsub("_WRI", "", Type))) %>%
  ggplot(aes(x = STATE, y = Ratio, fill = Type, group = Source))+
  geom_col(position = "dodge")+
  theme_classic()

enter image description here The reason I'm posting this is that, if it's not possible, this is half-way done. You can see that it does split, but as I told it to, they're stacked – all of it. If this is for your personal use, maybe this can give you some insight, but this isn't ideal to show other people – it's rather confusing. Also, I hope this "answer" might give insight to someone else. Afterall, this reduces the problem to one single argument: position = "stack".

By the way, if you're wondering, this is what position = "dodge" looks like: enter image description here So it's not that.

EDIT: Sorry, I had done just by USGS as source; changed that now.

Érico Patto
  • 1,015
  • 4
  • 18
3

Check your columns have the same information:

table(gsub("_USGS","",df$USGS_source) == gsub("_WRI","",df$WRI_source))
TRUE 
147

So we keep one of them and pivot them long:

library(ggplot2)
library(tidyr)
library(dplyr)

df %>%
mutate(source = gsub("_USGS","",USGS_source)) %>%
select(STATE,source,WRI_ratio,USGS_ratio) %>%
pivot_longer(cols=c("USGS_ratio","WRI_ratio"),
names_sep="_",names_to=c("data","var1"))



   # A tibble: 294 x 5
   STATE source            data  var1     value
   <chr> <chr>             <chr> <chr>    <dbl>
 1 AL    desalinatedRatio  USGS  ratio 0       
 2 AL    desalinatedRatio  WRI   ratio 0.000121
 3 AL    surfacewaterRatio USGS  ratio 0.953   
 4 AL    surfacewaterRatio WRI   ratio 0.999   
 5 AL    groundwaterRatio  USGS  ratio 0.0474  
 6 AL    groundwaterRatio  WRI   ratio 0.000938
 7 AR    desalinatedRatio  USGS  ratio 0       
 8 AR    desalinatedRatio  WRI   ratio 0       
 9 AR    surfacewaterRatio USGS  ratio 0.321   
10 AR    surfacewaterRatio WRI   ratio 0.459  

You can see now every row is an observation with the corresponding category. Might have a better way to do the above.. So below is a quick solution to have them side by side, which is to use a facet:

g = df %>%
mutate(source = gsub("_USGS","",USGS_source)) %>%
pivot_longer(cols=c("USGS_ratio","WRI_ratio"),
names_sep="_",names_to=c("data","var1")) %>%
ggplot(aes(x=data,y=value,fill=source)) +
geom_col() + facet_wrap(~STATE,nrow=1) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

ggsave(g,file="test.png",width=30,height=5)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72