简体   繁体   中英

Change y-axis center in ggplot

Is there anyway to move the "center" of the y-axis to 1 in ggplot instead of 0? I have ratio data so I'm trying to visualize it in a way where the the values are above or below 1, not above or below 0. So far I just subtracted 1 from the data and have it centered around 0 - but I've been unsuccessful on relabeling the y-axis so that it reflects the numbers I want.

chi_ratios_plot  <-  ggplot(data=chi_ratios, aes(x = metric, y =ratio2, fill=coral_cover))+
  geom_bar(stat="identity", position="dodge")+
  theme_classic()+
  coord_flip()

there's my code. Here's what I have so far: 在此处输入图片说明 the 0 should really be a 1, the -1 should really be a 0, etc.

Using your manually shifted ratio values, you can fix up their labels by providing a labels argument to scale_y_continuous and shifting them back up to 1:

chi_ratios = expand.grid(
    metric = c("A", "B", "C"),
    coral_cover = c("Low", "Med", "High")
) %>%
    # ratio: actual ratios
    # ratio2: shifted so they're centered at 0
    mutate(ratio = rnorm(n(), mean = 1, sd = 2),
           ratio2 = ratio - 1)


ggplot(data=chi_ratios, aes(x = metric, y = ratio2, fill = coral_cover)) +
    geom_col(position="dodge") +
    scale_y_continuous(labels = function(breaks) { breaks + 1}) +
    theme_classic() +
    coord_flip()

I can't see any option to override the 0 baseline in either geom_bar() or geom_col() , and achieving the same thing with geom_rect() would be fiddly, so this seems like the easiest option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM