首页 > 解决方案 > 如何在 plotrix 或 ggplot2 上绘制 3 个 y 轴图?

问题描述

我想在三个 Y 轴和一个 X 轴图中绘制如下示例中的值:

Name Replication ratio Growth rate Abundance(%)
Bin1      1.3             4.45       45
Bin2      1.2             5.66       12  
Bin3      1.1            16.34       15 
Bin4      1.5            11.45       3
Bin5      1.5             1.34       2
Bin6      1.9             2.37       32

“复制率”和“增长率”数据应显示为(不同的)彩色条形图,而丰度数据应显示为同一图上的线+点图。总共将有一个 X 轴和三个 Y 轴。任何一段 R 代码都会受到高度赞赏,一定会让我很开心!

标签: rggplot2plot

解决方案


你可能想试试plotly。我自己对此几乎没有经验。这是我尝试过的,只是为了给你一些想法开始:

library(plotly)
plot_ly(data = df) %>% 
  add_lines(x = ~Names, y = ~Replication, 
            color = I("red"), name = "name01") %>% 
  add_markers(x = ~Names, y = ~Growth, yaxis = "y2", 
              color = I("blue"), name = "name02") %>%
  add_bars(x = ~Names, y = ~Abundance, yaxis = "y3", 
           color = I("purple"), name = "name03") %>%
  layout(
    yaxis = list(showline = TRUE, side = "left", color = "red",  title = "Replication rate"),
    yaxis2 = list(showline = TRUE, overlaying = "y", anchor = "free", color = "blue", title = "Growth rate"), 
    yaxis3 = list(showline = TRUE, side = "right", overlaying = "y", color = "purple", title = "Abundance"),
    xaxis = list(showline = FALSE, zeroline = FALSE, dtick = 1, title = ""), 
    showlegend = FALSE, 
    margin = list(pad = 50, b = 90, l = 150, r = 90),
    legend = list(orientation = "h")

数据:

df <- data.frame(
  Names = c("Bin1", "Bin2", "Bin3", "Bin4", "Bin5", "Bin6"), 
  Replication = c(1.3, 1.2, 1.1,1.5,1.5,1.9), 
  Growth = c(4.45, 5.65, 16.34, 11.45, 1.34, 2.37), 
  Abundance = c(45, 12, 15, 3, 2, 32)
)

在此处输入图像描述


推荐阅读