1
votes

I am trying to adjust the y-axis of this graph so that it starts at 1 instead of 0. What's the best way to do this?

The solution offered here cuts off the bottom of the graph. I would like for the bars to look pretty much identical to the graph below, but with the lower y-limit at 1 and each bar moved down 1 unit to match. I would like to preserve the small amount of gray space below each bar.

enter image description here

Code:

groups %>% 
  ungroup() %>% 
  mutate(message = fct_relevel(message, "Personal", "General"),
         enviroattitudeshalf = fct_relevel(enviroattitudeshalf, "Low Environmental Attitudes", "High Environmental Attitudes")) %>% 
  ggplot(aes(x = message, y = mean)) + 
  geom_col(width = 0.5, fill = "003900") +
  geom_text(aes(label = round(mean, digits = 1), vjust = -2)) + 
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) + 
  labs(title = "Environment: Evaluations of Personal and General Convincingness",
       y = "Rating",
       x = "Personal evaluation or general evaluation") + 
  ylim(0, 8) +
  facet_wrap(~enviroattitudeshalf)

Data:

structure(list(enviroattitudeshalf = c("Low Environmental Attitudes", 
"Low Environmental Attitudes", "High Environmental Attitudes", 
"High Environmental Attitudes"), message = c("General", "Personal", 
"General", "Personal"), mean = c(3.89473684210526, 3.37894736842105, 
4.43636363636364, 5.10909090909091), se = c(0.145460372156746, 
0.19522803582675, 0.160549137262631, 0.171509247396541)), row.names = c(NA, 
-4L), groups = structure(list(enviroattitudeshalf = c("High Environmental Attitudes", 
"Low Environmental Attitudes"), .rows = structure(list(3:4, 1:2), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
2

2 Answers

1
votes

As an alternative to re-labeling the y-axis, you can cut it off at 1 by setting oob = scales::oob_squish. The out of bounds squish function sets the values that are out of bounds to the nearest limit. This preserves the upper part of the bar, giving the same interpretation, whereas relabeling would suggest the first bar exceeds the value 4, and it doesn't.

groups %>% 
  ungroup() %>% 
  mutate(message = fct_relevel(message, "Personal", "General"),
         enviroattitudeshalf = fct_relevel(enviroattitudeshalf, "Low Environmental Attitudes", "High Environmental Attitudes")) %>% 
  ggplot(aes(x = message, y = mean)) + 
  geom_col(width = 0.5, fill = "003900") +
  geom_text(aes(label = round(mean, digits = 1), vjust = -2)) + 
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) + 
  labs(title = "Environment: Evaluations of Personal and General Convincingness",
       y = "Rating",
       x = "Personal evaluation or general evaluation") + 
  facet_wrap(~enviroattitudeshalf) +
  scale_y_continuous(limits = c(1, 8), oob = scales::oob_squish)

enter image description here

0
votes

Using scale_y_continuous you can adjust breaks and labels.

library(tidyverse)

groups %>% 
  ungroup() %>% 
  mutate(message = fct_relevel(message, "Personal", "General"),
         enviroattitudeshalf = fct_relevel(enviroattitudeshalf, "Low Environmental Attitudes", "High Environmental Attitudes")) %>% 
  ggplot(aes(x = message, y = mean)) + 
  geom_col(width = 0.5, fill = "003900") +
  geom_text(aes(label = round(mean, digits = 1), vjust = -3)) + 
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) + 
  labs(title = "Environment: Evaluations of Personal and General Convincingness",
       y = "Rating",
       x = "Personal evaluation or general evaluation") + 
  scale_y_continuous(breaks = 0:8, labels = 1:9) + 
  facet_wrap(~enviroattitudeshalf, scales = 'free_y')

enter image description here