简体   繁体   中英

how to set dual Y axis in geom_bar plot in ggplot2?

I'd like to draw bar plot like this but in dual Y axis

( https://i.stack.imgur.com/ldMx0.jpg )

the first three indexs range from 0 to 1, so I want the left y-axis (corresponding to NSE, KGE, VE) to range from 0 to 1, and the right y-axis (corresponding to PBIAS) to range from -15 to 5.

the following is my data and code:

library("ggplot2")

## data
data <- data.frame(
  value=c(0.82,0.87,0.65,-3.39,0.75,0.82,0.63,1.14,0.85,0.87,0.67,-7.03),
  sd=c(0.003,0.047,0.006,4.8,0.003,0.028,0.006,4.77,0.004,0.057,0.014,4.85),
  index=c("NSE","KGE","VE","PBIAS","NSE","KGE","VE","PBIAS","NSE","KGE","VE","PBIAS"),
  period=c("all","all","all","all","calibration","calibration","calibration","calibration","validation","validation","validation","validation")
)

## fix index sequence
data$index <- factor(data$index, levels = c('NSE','KGE','VE',"PBIAS"))
data$period <- factor(data$period, levels = c('all','calibration', 'validation'))


## bar plot
ggplot(data, aes(x=index, y=value, fill=period))+ 
  geom_bar(position="dodge", stat="identity")+
  geom_errorbar(aes(ymin=value-sd, ymax=value+sd),
                position = position_dodge(0.9), width=0.2 ,alpha=0.5, size=1)+
  theme_bw()

I try to scale and shift the second y-axis, but PBIAS bar plot was removed because of out of scale limit as follow:

( https://i.stack.imgur.com/n6Jfm.jpg )

the following is my code with dual y axis:

## bar plot (scale and shift the second y-axis with slope/intercept in 20/-15)
ggplot(data, aes(x=index, y=value, fill=period))+ 
  geom_bar(position="dodge", stat="identity")+
  geom_errorbar(aes(ymin=value-sd, ymax=value+sd),
                position = position_dodge(0.9), width=0.2 ,alpha=0.5, size=1)+
  theme_bw()+
  scale_y_continuous(limits = c(0,1), name = "value", sec.axis = sec_axis(~ 20*.- 15, name="value")) 

Any advice for move bar_plot or other solution?

Taking a different approach, instead of using a dual axis one option would be to make two separate plots and glue them together using patchwork . IMHO that is much easier than fiddling around with the rescaling the data (that's the step you missed, ie if you want to have a secondary axis you also have to rescale the data) and makes it clearer that the indices are measured on a different scale:

library(ggplot2)
library(patchwork)

data$facet <- data$index %in% "PBIAS"

plot_fun <- function(.data) {
  ggplot(.data, aes(x = index, y = value, fill = period)) +
    geom_bar(position = "dodge", stat = "identity") +
    geom_errorbar(aes(ymin = value - sd, ymax = value + sd),
                  position = position_dodge(0.9), width = 0.2, alpha = 0.5, size = 1
    ) +
    theme_bw()
}

p1 <- subset(data, !facet) |> plot_fun() + scale_y_continuous(limits = c(0, 1))
p2 <- subset(data, facet) |> plot_fun() + scale_y_continuous(limits = c(-15, 15), position = "right")

p1 + p2 +
  plot_layout(guides = "collect", width = c(3, 1))

在此处输入图像描述

A second but similar option would be to use ggh4x which via ggh4x::facetted_pos_scales allows to set the limits for facet panels individually. One drawback, the panels have the same width. (I failed in making this approach work with facet_grid and space="free" )

library(ggplot2)
library(ggh4x)

data$facet <- data$index %in% "PBIAS"

ggplot(data, aes(x = index, y = value, fill = period)) +
  geom_bar(position = "dodge", stat = "identity") +
  geom_errorbar(aes(ymin = value - sd, ymax = value + sd),
    position = position_dodge(0.9), width = 0.2, alpha = 0.5, size = 1
  ) +
  facet_wrap(~facet, scales = "free") +
  facetted_pos_scales(
    y = list(
      facet ~ scale_y_continuous(limits = c(-15, 15), position = "right"),
      !facet ~ scale_y_continuous(limits = c(0, 1), position = "left")
    )
  ) +
  theme_bw() +
  theme(strip.text.x = element_blank())

在此处输入图像描述

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