Home > Back-end >  Can get ggplot2 bar chart to display direct values for Y axis?
Can get ggplot2 bar chart to display direct values for Y axis?

Time:11-14

When I plot a my barchart, the chart is putting out values on the Y-axis I don't understand. How can I get the barchart to use actual values?

#Here is the code for my graph

stock %>%
  #Tidy data to be handled correctly
  group_by(year) %>%
  filter(year == "2017") %>%
  pivot_longer(bio_sus:bio_notsus) %>%
  mutate(value2 = ifelse(name=="bio_sus",-1*value, value)) %>%
  #make the graph
  ggplot(aes(ocean_whole, value2/100, fill=name))  
  geom_bar(stat = "identity")

The bar chart is putting out values between 2.5 and -2.5 when my value 2 values range between 100 and - 100

ocean_sub                   code   year ocean_whole   name       value value2
   <chr>                       <chr> <dbl> <chr>         <chr>      <dbl>  <dbl>
 1 Eastern Central Atlantic    NA     2017 atlantic      bio_sus     57.1  -57.1
 2 Eastern Central Atlantic    NA     2017 atlantic      bio_notsus  42.9   42.9
 3 Eastern Central Pacific     NA     2017 pacific       bio_sus     86.7  -86.7
 4 Eastern Central Pacific     NA     2017 pacific       bio_notsus  13.3   13.3
 5 Eastern Indian Ocean        NA     2017 indian        bio_sus     68.6  -68.6

How can I get the chart to display the actual values?

#My code is from TidyTuesdays Global seafood:
stock <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-12/fish-stocks-within-sustainable-levels.csv')

#transformed in the following way

oceans <- c("pacific", "atlantic", "indian", "mediterranean")

lu <- stack(sapply(oceans, grep, x = stock$entity, ignore.case = TRUE))

stock$oceans <-  stock$entity
stock$oceans[lu$values] <- as.character(lu$ind)

stock %>%
  group_by(oceans) %>%
  summarise(across(matches("^share"), sum))
colnames(stock) <- (c("ocean_sub", "code", "year", "bio_sus", "bio_notsus", "ocean_whole"))

CodePudding user response:

Your tibble contains multiple values for ocean_whole before you give it over to ggplot(). The sums of these values amount the unexpected values. Check:

stock %>%
group_by(year) %>%
filter(year == "2017") %>%
pivot_longer(bio_sus:bio_notsus) %>%
mutate(value2 = ifelse(name=="bio_sus",-1*value, value)) %>%
group_by(ocean_whole, name) %>%
summarise(sum(value2))
  • Related