gpt4 book ai didi

r - 在 R 的同一图中为一个变量创建两个条形图

转载 作者:行者123 更新时间:2023-12-01 23:42:02 24 4
gpt4 key购买 nike

我有以下数据框:

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

在这个框架中,我为每个 STATE 列出了“脱盐”、“地表水”和“地下水”的比率,我为两个名为 USGS世界资源研究所。我能够独立绘制条形图,但不能一起绘制。

所以我想要的是为每个状态绘制两个彼此相邻的条形图,以便您可以比较它们。它应该看起来像这样: example

最佳答案

这很有趣...我试着用 ggplot2 来做(这就是我所知道的),并发现了这个限制:决定条形应该如何呈现的部分(堆叠,并排等)只接受参数——一个配置——但有两个分组。我能做的最好的事情是:

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我发布这个的原因是,如果不可能的话,这已经完成了一半。你可以看到它确实 split 了,但正如我告诉它的那样,它们是堆叠的——全部。如果这是供您个人使用,也许这可以给您一些见解,但这并不适合向其他人展示——这相当令人困惑。另外,我希望这个“答案”可能会给其他人带来洞察力。毕竟,这将问题简化为一个参数:position = "stack"

顺便说一下,如果您想知道,这就是 position = "dodge" 的样子: enter image description here所以不是那样。

编辑:抱歉,我只是以美国地质调查局为来源完成的;现在改变了。

关于r - 在 R 的同一图中为一个变量创建两个条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64875855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com