0

When I uses the position = "stack", I get the wrong addition of the salmon species in my plot. I should have ~350 Chinook Salmon in 2015, but the plot shows ~1500. I am unable to include a picture in the question but here is a link https://i.stack.imgur.com/8RWRv.jpg

intabun <- ggplot(abundlong, aes(year, abundance, fill = species)) +
geom_bar(stat = "identity", width = 0.5, position = "stack") +
scale_fill_brewer(palette = "Set1", name = "") +
theme_classic() +
scale_y_continuous(expand = c(0,0),limits = c(0,1500), breaks = c(100,500,1000,1500))+
ylab("Number of salmon") +
theme(legend.position = "bottom", legend.text = element_text(size = 12)) +
xlab("Year") +
theme(axis.text.x = element_text(size = 14)) +
theme(axis.text.y = element_text(size = 14)) +
theme(axis.title.x = element_text(size = 14)) +
theme(axis.title.y = element_text(size = 16)) 


intabun


[1]: https://i.stack.imgur.com/8RWRv.jpg
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's unclear what your plot looks like at the moment since we can't see it. – MrFlick Mar 15 '21 at 17:54
  • It would be helpful to see your data as well. Right now your plot seems to be showing what you say it should - the red bar for `chinook.abundance` seems to extend from about 800 to 1150 or so - about the 350 figure you're asking for. `position = "stack"` is adding the four species types together for the full height of the bar. – Andy Baxter Mar 15 '21 at 19:59
  • @AndrewBaxter you're right it does show the 350 figure I am asking for, is my Y axis scale off then? I could potentially use position = "dodge" but i thought the stack looked nicer.. Is there a function where it doesn't add the four species types together for the full height of the bar? – failedhighschool Mar 15 '21 at 20:04
  • @AndrewBaxter I want the Y axis to be the correct value for each represented amount. example, the chinook.abundance extends from 800-1150, but i would like it to extend from 0-350 to have an accurate Y axis – failedhighschool Mar 15 '21 at 20:18
  • example below - you can use `position = "identity"`: – Andy Baxter Mar 15 '21 at 20:19

1 Answers1

0

Here's a way to solve it, use position = "identity":

library(ggplot2)
library(forcats)
library(dplyr)

gapminder::gapminder %>% 
  filter(continent %in% c("Asia", "Europe"),
         year == 2007) %>% 
  mutate(country = fct_reorder(country, -gdpPercap)) %>% 
  group_by(continent) %>% 
  sample_n(5) %>% 
  arrange(country) %>% 
  ggplot(aes(continent, gdpPercap, fill = country)) +
  geom_bar(stat = "identity", position = "identity")

Hopefully does what you're looking for - each country's bar goes up to its own gdpPercap (not adding on to countries below it):

You'll maybe want to try with your data to see if it's clear, but perhaps a partial position = "dodge" would help show that the bars are independent and each go all the way down to zero?

Created on 2021-03-15 by the reprex package (v1.0.0)

Slight edit - other possibility:

You could use position = position_dodge(width = 0.7) (not just "dodge") to make them overlap a little but still show that each bar goes down to zero:

library(ggplot2)
library(forcats)
library(dplyr)

gapminder::gapminder %>% 
  filter(continent %in% c("Asia", "Europe"),
         year == 2007) %>% 
  mutate(country = fct_reorder(country, -gdpPercap)) %>% 
  group_by(continent) %>% 
  sample_n(5) %>% 
  arrange(country) %>% 
  ggplot(aes(continent, gdpPercap, fill = country)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.7))

Created on 2021-03-15 by the reprex package (v1.0.0)

Andy Baxter
  • 5,833
  • 1
  • 8
  • 22
  • 1
    This is exactly what I was looking for, Thanks! – failedhighschool Mar 15 '21 at 20:39
  • Fab! glad it worked. Do mark question as closed if you get a chance :) – Andy Baxter Mar 15 '21 at 21:00
  • Hi @JustinGross - do mark answer as 'accepted' if it works for you. Good way of getting rep points for your first question and for the person giving working answer. More here on how to do that - https://stackoverflow.com/help/someone-answers – Andy Baxter Mar 22 '21 at 10:37