You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为plotrix包twoord.plot绘图的双Y轴添加单位的技术问询

Adding Units to Dual Y-Axes in plotrix's twoord.plot

Hey there! Let's get those Y-axis units sorted for your plot. Looking at your code, you already included units in the axis titles (like (B) and (BTC)), but if you want to add units directly to the tick marks (so every number on the axis shows its unit), here are two straightforward approaches:

Approach 1: Custom Tick Labels Directly in twoord.plot

You can use the yaxlabels and ryaxlabels parameters to pass pre-formatted labels with units. First, calculate clean tick values based on your data, then append the units to each label:

library(plotrix)
mempool <- read.csv("mempool-size.csv")
mempool$date <- as.Date(mempool$date)
fees <- read.csv("transaction-fees.csv")
fees$date <- as.Date(fees$date)

# Generate tick labels with units for left Y-axis (Unconfirmed transactions)
left_y_ticks <- pretty(mempool$value) # Gets evenly spaced tick values
left_y_labels <- paste(left_y_ticks, "B", sep = " ")

# Generate tick labels with units for right Y-axis (Fees)
right_y_ticks <- pretty(fees$value)
right_y_labels <- paste(right_y_ticks, "BTC", sep = " ")

# Create the plot with custom labeled ticks
twoord.plot(
  x1 = mempool$date, y1 = mempool$value,
  x2 = fees$date, y2 = fees$value,
  type = "b",
  ylab = "Unconfirmed transactions", # Removed unit here since ticks display it
  rylab = "Fees paid per day",       # Same for right axis title
  main = "Fees and mempool size since 2017",
  yaxlabels = left_y_labels,
  ryaxlabels = right_y_labels
)

Approach 2: Manually Add Axes After Plotting

If you want to keep the default tick positions but just add units, turn off the default axes and re-add them with custom labels:

library(plotrix)
mempool <- read.csv("mempool-size.csv")
mempool$date <- as.Date(mempool$date)
fees <- read.csv("transaction-fees.csv")
fees$date <- as.Date(fees$date)

# Create the plot without default axes
twoord.plot(
  x1 = mempool$date, y1 = mempool$value,
  x2 = fees$date, y2 = fees$value,
  type = "b",
  ylab = "Unconfirmed transactions",
  rylab = "Fees paid per day",
  main = "Fees and mempool size since 2017",
  axes = FALSE # Disable default axes to customize
)

# Add left Y-axis with units
axis(side = 2, at = axTicks(2), labels = paste(axTicks(2), "B"))
# Add right Y-axis with units
axis(side = 4, at = axTicks(4), labels = paste(axTicks(4), "BTC"))
# Add X-axis (default date axis)
axis(side = 1)
# Add a box around the plot to complete the frame
box()

Quick Note:

If you actually just wanted to confirm the units are visible in the axis titles (which your original code already handles), double-check that your CSV data loads correctly—missing or malformed data can sometimes cause unexpected label behavior. But the above methods will ensure units are clearly displayed either in the ticks or the titles.

内容的提问来源于stack exchange,提问作者Gecko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:38:34