0

How can I change the deviation point of ggpubr::ggbarplot graph from 0 to 1. All the bars with values <1 face down with blue color using the example below

library(ggpubr)
# Load data
data("mtcars")
dfm <- mtcars
# Convert the cyl variable to a factor
dfm$cyl <- as.factor(dfm$cyl)
# Add the name colums
dfm$name <- rownames(dfm)

# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), 
                      levels = c("low", "high"))
ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",           # Sort the value in ascending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          xlab = FALSE,
          legend.title = "MPG Group"
)
Keniajin
  • 1,649
  • 2
  • 20
  • 43

1 Answers1

1

If I understand what you would like, what I did was subtract 1 from "dfm$mpg_z" which gives

Example of Result

library(ggpubr)
# Load data
data("mtcars")
dfm <- mtcars
# Convert the cyl variable to a factor
dfm$cyl <- as.factor(dfm$cyl)
# Add the name colums
dfm$name <- rownames(dfm)

# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)-1
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), 
                      levels = c("low", "high"))
ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",           # Sort the value in ascending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score - 1",
          xlab = FALSE,
          legend.title = "MPG Group"
)

Below is an edit to the original answer, reflecting changes as discussed in the comments below.

library(ggpubr)
library(ggplot2)
# Load data
data("mtcars")
dfm <- mtcars
# Convert the cyl variable to a factor
dfm$cyl <- as.factor(dfm$cyl)
# Add the name colums
dfm$name <- rownames(dfm)

# Function to re-value things, increasing them by 1.

plus1f <- function(x){x+1}

# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)-1
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), 
                      levels = c("low", "high"))
ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",           # Sort the value in ascending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          xlab = FALSE,
          legend.title = "MPG Group",


)+scale_y_continuous(labels=plus1f)

with this code giving the following graph:

enter image description here

  • thanks Frithjof , but how can I maintain the scale on the y-axis to be on the same range – Keniajin Oct 20 '18 at 08:02
  • I'm unsure what you would like... you would like the scale of the y axis to be exactly as it is in the original post but the bars to look exactly like they do in mine? – Frithjof Herb Oct 22 '18 at 11:57
  • 1
    Sure, I would like the bars to look like yours but the scale remain as the original. i.e on your graph the 0 should be 1 and 1 be 2 – Keniajin Oct 23 '18 at 10:47
  • I have edited my answer to reflect what I understand to be what you wanted. – Frithjof Herb Oct 24 '18 at 12:06