As you can see in the picture, y2(on the right) is showing values from 1 to -6 but since my data set includes values that are 0 or very close to 0 (like -1.20E-05) it’s looking like a flat line.
I want to scale y 2 so it includes values from 0.0005 to -0.0005 without affecting the blue line or Y1.
Here is the code
library(ggplot2)
library(scales)
df <- read.csv("Fig 1 Uncovered IRP.csv")
# Convert XAxisDate to Date format
df$XAxisDate <- as.Date(df$XAxisDate, format = "%m/%d/%Y")
y1 <- df$YAxis1InterestRateDifference..RMB.USD.
y2 <- df$YAxis2Uncovered.IRP
x <- df$XAxisDate
# Plot
ggplot(df, aes(x)) +
geom_line(aes(y = y1, color = "Interest Rate Difference (RMB-USD)")) +
geom_line(aes(y = y2, color = "Uncovered IRP")) +
scale_y_continuous(
name = "Interest Rate Difference (RMB-USD)",
breaks = pretty_breaks(n = 10),
limits = c(min(c(y1, y2)), max(c(y1, y2), na.rm = TRUE))
) +
scale_y_continuous(
name = "Interest Rate Difference (RMB-USD)",
sec.axis = sec_axis(~., name = "Uncovered IRP",
breaks = pretty_breaks(n = 10), labels = scales::number_format(accuracy = 0.1)),
breaks = pretty_breaks(n = 10),
limits = c(min(c(y1, y2)), max(c(y1, y2), na.rm = TRUE))
) +
scale_color_manual(values = c("blue", "red")) +
labs(x = "Date") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 12),
axis.text.y.right = element_text(size = 12),
axis.title.y.right = element_text(size = 12),
legend.title = element_text(size = 12),
legend.text = element_text(size = 12),
legend.position = "top"
)