为plotrix包twoord.plot绘图的双Y轴添加单位的技术问询
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

