The ggplot plot below has the second x-axis with custom labels.
dat <- data.frame(f=seq(0.01,0.5,by=0.01),y=rnorm(50))
fPretty <- pretty(dat$f)
pPretty <- round(1 / fPretty, 2)
p1 <- ggplot(data=dat, aes(x=f,y=y))
p1 <- p1 + geom_line(color="red")
p1 <- p1 + labs(x = "Frequency")
p1 <- p1 + scale_x_continuous(expand = c(0,0),
sec.axis=sec_axis(~., breaks=fPretty,
labels = pPretty,
name="Period (1/f)"))
p1
I'd like to transform the x-axis to a log10 scale and plot the second axis to match:
p2 <- ggplot(data=dat, aes(x=f,y=y))
p2 <- p2 + geom_line(color="red")
p2 <- p2 + labs(x = "Frequency")
p2 <- p2 + scale_x_continuous(expand = c(0,0), trans="log10",
sec.axis=sec_axis(~., breaks=fPretty,
labels = pPretty,
name="Period (1/f)"))
p2
How can I set fPretty and pPretty in p2 to give the same effect?
I.e., in this case:
fPretty <- c(0.01, 0.1)
pPretty <- round(1 / fPretty, 2)
Do I use scales::log_breaks() and scales::trans_format()?
The aim is to specify custom tick marks for both x-axis's.


p1I usefPretty <- pretty(dat$f)to calculate where the tick marks will be and use that for the second axis. How can I getfPrettyforp2which has the transformed axis? - user111024