r - How to graph top most important categories among 25 with stacked bar chart -


my problem bit more complicated 1 this question.

i wanted stack 10 abundant species per each rot.herb (18 of them in total) , group other species 2 big categories, other monocots , other dicots. think need manually assign monocot dicot. tricky part 10 abundant species group unique every rot. herb.

here graph of stacked:

enter image description here

and here code:

weedweights<-weeds%>%     select(-ends_with("no"))%>%     gather(key=species, value=speciesmass, digsawt:pollawt)%>%     mutate(realmass=speciesmass * samplearea.m.2.)%>%     group_by(rot.herb, species)%>%     summarize(avgrealmass=mean(realmass, na.rm=true))%>%     filter(avgrealmass != "nan")%>%     ungroup()  ggplot(weedweights, aes(x=rot.herb, y=avgrealmass, fill=species))+         geom_bar(stat="identity") 

you can see data here

structure(list(rot.herb = structure(c(1l, 1l, 1l, 1l, 1l, 1l,   1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l,   2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l,   4l, 4l, 4l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 6l, 6l, 6l, 6l,   6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 7l, 7l, 7l, 7l, 7l, 7l, 7l,   7l, 7l, 7l, 7l, 7l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l, 8l,   8l, 8l, 8l, 9l, 9l, 9l, 9l, 9l, 9l, 9l, 9l, 9l, 9l, 10l, 10l,   10l, 10l, 10l, 10l, 10l, 10l, 11l, 11l, 11l, 11l, 11l, 11l, 11l,   11l, 11l, 11l, 11l, 11l, 11l, 12l, 12l, 12l, 12l, 12l, 12l, 12l,   12l, 12l, 12l, 12l, 12l, 12l, 12l, 13l, 13l, 13l, 13l, 13l, 13l,   13l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l,   15l, 15l, 15l, 15l, 15l, 15l, 15l, 15l, 16l, 16l, 16l, 16l, 16l,   16l, 16l, 17l, 17l, 17l, 17l, 17l, 17l, 17l, 17l, 17l, 17l, 17l,   18l, 18l, 18l, 18l, 18l), .label = c("a4conv", "a4low", "c2conv",   "c2low", "c3conv", "c3low", "c4conv", "c4low", "o3conv", "o3low",   "o4conv", "o4low", "s2conv", "s2low", "s3conv", "s3low", "s4conv",   "s4low"), class = "factor"), species = c("digsa", "setfa", "setlu",   "amata", "cheal", "physu", "tarof", "ephht", "sonar", "moral",   "oxast", "unknownmonocot", "digsa", "setfa", "setlu", "amata",   "cheal", "solpt", "tarof", "ephht", "sonar", "unknowndicot",   "unknownmonocot", "setfa", "amata", "solpt", "physu", "tarof",   "ephht", "moral", "setfa", "amata", "cheal", "solpt", "physu",   "polpy", "abuth", "tarof", "ephht", "sonar", "ascsy", "setfa",   "setlu", "amata", "cheal", "solpt", "physu", "abuth", "tarof",   "moral", "digsa", "setfa", "setlu", "erbvi", "amata", "cheal",   "solpt", "physu", "abuth", "tarof", "moral", "unknowndicot",   "unknownmonocot", "digsa", "setfa", "setlu", "amata", "solpt",   "physu", "abuth", "tarof", "ephht", "sonar", "moral", "oxast",   "digsa", "setfa", "setlu", "echcg", "amata", "cheal", "solpt",   "physu", "polpy", "abuth", "tarof", "ephht", "oxast", "polla",   "setfa", "setlu", "amata", "cheal", "solpt", "polpy", "tarof",   "polav", "plama", "unknownmonocot", "setfa", "setlu", "amata",   "cheal", "solpt", "physu", "tarof", "unknownmonocot", "digsa",   "setfa", "setlu", "panca", "cypes", "amata", "cheal", "solpt",   "physu", "tarof", "ephht", "cirar", "oxast", "digsa", "setfa",   "setlu", "panca", "cypes", "amata", "cheal", "solpt", "physu",   "tarof", "ephht", "sonar", "moral", "unknownmonocot", "amata",   "cheal", "solpt", "physu", "polpy", "tarof", "moral", "digsa",   "setfa", "panca", "echcg", "erbvi", "amata", "cheal", "solpt",   "physu", "abuth", "tarof", "moral", "amata", "cheal", "solpt",   "physu", "abuth", "tarof", "moral", "unknowndicot", "setfa",   "amata", "cheal", "solpt", "abuth", "tarof", "ephht", "setlu",   "echcg", "amata", "cheal", "solpt", "physu", "abuth", "tarof",   "ephht", "moral", "unknowndicot", "digsa", "setfa", "amata",   "cheal", "tarof"), avgrealmass = c(6.25, 26.35, 58.35, 13.4666666666667,   17.1, 1.15, 28.75, 0.45, 0, 0.2, 1.2, 0, 6.425, 18.65, 6.63333333333333,   3.475, 6.11666666666667, 16.1, 41.9625, 0.9, 0, 0, 0, 0.0809410748974746,   0.237427153032592, 0.0917332182171379, 0.0647528599179797, 0.105223397366717,   0, 0.0539607165983164, 0.795920569825167, 5.7818907835096, 13.3822577163825,   1.62151953377941, 3.1099359666163, 0.388517159507878, 0.0539607165983164,   0.0845384560040291, 0.0701489315778114, 0.0539607165983164, 0.0215842866393266,   0.0539607165983164, 54.8240880638895, 0, 0.0269803582991582,   0.102525361536801, 0.0215842866393266, 0.0647528599179797, 0.0404705374487373,   0.0161882149794949, 0.485646449384848, 11.2103388733002, 86.4990287071012,   22.9333045542845, 13.9218648823656, 49.6798330815167, 0.0944312540470537,   0.661018778329376, 0.410101446147205, 0.399309302827542, 0, 1.18173969350313,   0.0161882149794949, 0.134901791495791, 1.24649255342111, 1.95877401251889,   0.00269803582991582, 0.364234837038636, 0.555795380962659, 0.356140729548888,   0.0350744657889057, 0.0944312540470537, 0.00809410748974746,   0.00539607165983164, 0, 1.42186488236564, 15.7794625512627, 0.584574429815095,   11.7094755018347, 1.75372328944528, 2.4552126052234, 0.50992877185409,   0.0863371465573063, 0.221238938053097, 9.53305993236924, 0.106572415281675,   0.117364558601338, 0.075545003237643, 1.40297863155623, 31.45,   14.0666666666667, 18.7375, 15.225, 22.3166666666667, 24.05, 8.775,   1.05, 0.4, 0, 8.55, 35.475, 31.4375, 35.4375, 15.4, 16.55, 7.15,   0, 105.05, 5.775, 0.8, 0.1, 37.85, 23.3375, 6.35, 97.4, 22.925,   138.2875, 8.26666666666667, 0.2, 16.25, 8.075, 28.9, 10.1, 1.05,   8.85, 34.6375, 59.425, 87.7, 4.45, 179.9875, 1.8, 34.45, 0, 0,   0.585473775091733, 0.0161882149794949, 0, 0, 0.113317504856464,   0.305777394057126, 0, 1.61342542628966, 1.62961364126916, 2.36887545866609,   7.94301748327218, 17.4832721778545, 30.8034750701489, 3.40761925318368,   0.627743003093748, 0.582775739261817, 1.46773149147421, 0.0575580977048708,   0.00899345276638607, 0.539607165983164, 0.364234837038636, 0.0431685732786531,   0, 0.407403410317289, 0.0229333045542845, 0, 0, 21.8540902223182,   43.1591301532484, 57.2172458450248, 0.793222533995251, 1.14396719188431,   0.215842866393266, 0.113317504856464, 0.0647528599179797, 0.0917332182171379,   0.453270019425858, 0.0431685732786531, 0.0485646449384848, 0.0161882149794949,   0.879559680552558, 0.00269803582991582, 0.0161882149794949, 0.0143895244262177,   0.0215842866393266, 0.075545003237643, 5.71983595942154, 34.9719404273689,   4.31685732786531, 0)), .names = c("rot.herb", "species", "avgrealmass"  ), class = c("tbl_df", "tbl", "data.frame"), row.names = c(na,   -184l))

if i'm understanding right (assuming data posted processed weedweights frame), need use top_n builtin in dplyr.

topweights <- weedweights %>% group_by(rot.herb) %>% top_n(10, avgrealmass) 

in addition, can clean visualization sorting abundance %>% arrange(-avgrealmass) before plotting, abundant @ base.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -