r - 如何在 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 代码都会受到高度赞赏,一定会让我很开心!
解决方案
你可能想试试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)
)
推荐阅读
- javascript - 我如何使用 Tampermonkey 更改文本?
- java - QuerySyntaxException: 表未映射 [更新表 BOMmodel 设置 PA13 = Materialvalue + ReqQTY ]
- arrays - 之前引用的局部变量
- python - 使用正则表达式从标题格式化文本的问题
- c# - 如果我只是阅读它,我应该锁定字典吗?
- python - 将 pandas DataFrame 转换为任意嵌套的 JSON 数据
- python - 在提出是或否问题时遇到困难
- node.js - 如何仅在第一次在 MongoDB 中填充一些集合
- arrays - 嵌套表中的 LUA 模式匹配
- php - 无法使用 Woocommerce 类别列表设置对象属性,显示为 NULL