Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can get ggplot2 bar chart to display direct values for Y axis?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading