Expand x axis once you have negative value, but you want to show it as a positive value

Viewed 19

I have a data like this :

structure(list(name = structure(11:1, .Label = c("Control_over_life", 
"Access_mentalhealthcare", "Access_physicalhealthcare", "Use_health_care", 
"Use_mental_health_care", "Alcohol_consumption", "Fear_healthcare", 
"Anger", "Social_isolation", "Anxiety", "Stress"), class = c("ordered", 
"factor")), value = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L), .Label = c("Increased ", "No change", "Decreased "
), class = "factor"), n = c(152L, 148L, 128L, 88L, 76L, 56L, 
73L, 75L, 81L, 92L, 107L), per = c(58, 57, 49, 34, 29, 22, -28, 
-29, -31, -35, -41), group = c("Increased", "Increased", "Increased", 
"Increased", "Increased", "Increased", "Decreased", "Decreased", 
"Decreased", "Decreased", "Decreased")), row.names = c(NA, -11L
), class = c("tbl_df", "tbl", "data.frame"))

And to plot this data, I have used this code:

full$name <- factor(full$name , levels = rev(unique(full$name )), ordered=TRUE)

ggplot(full, aes(x = name, y = per, fill = group)) + 
  geom_bar(stat = "identity") +
  coord_flip() + 
  theme_bw() + 
  xlab("Stressor") + 
  ylab("Percentage") + 
  scale_y_continuous(breaks = pretty(full$per), labels = abs(pretty(full$per)))

And the outpot is: enter image description here

As you can see the x axis is (-40,60), how can I have (-60 to 60), but in the plot I want to show both positive.

1 Answers

I guess you need to set limits for the y-axis.

library(ggplot2)

ggplot(full,aes(x=name,y=per,fill=group))+
  geom_bar(stat="identity")+
  coord_flip()+
  theme_bw()+
  xlab("Stressor")+ylab("Percentage")+
  scale_y_continuous(breaks = pretty(full$per), 
                     labels = abs(pretty(full$per)), 
                     limits = c(-60, 60))

Created on 2022-08-12 by the reprex package (v2.0.1)

Related