如何在ggplot2中设置geom_bar图中双Y轴?

qoefvg9y  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(219)

我想画一个像这样的条形图,但是是双Y轴的
https://i.stack.imgur.com/ldMx0.jpg
前三个索引的范围为0到1,因此我希望左侧y轴(对应于NSE、KGE、VE)的范围为0到1,右侧y轴(对应于PBIAS)的范围为-15到5。
下面是我的数据和代码:

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()

我尝试缩放和移动第二个y轴,但由于超出比例限制,PBIAS条形图被删除,如下所示:
https://i.stack.imgur.com/n6Jfm.jpg
下面是我的代码与双y轴:

## 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"))

移动bar_plot或其他解决方案有何建议?

qojgxg4l

qojgxg4l1#

另一种方法是,不使用双轴,而是制作两个单独的图,然后使用patchwork.IMHO将它们粘合在一起,这比摆弄数据的重标度要容易得多(这是您错过的步骤,即如果您想要有一个次轴,则还必须重标度数据),并且更清楚地表明指数是在不同的标度上测量的:

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))

第二个类似的选择是使用ggh4x,它允许通过ggh4x::facetted_pos_scales单独设置面板的限制。一个缺点是,面板具有相同的宽度。(我未能使这种方法适用于facet_gridspace="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())

相关问题