ggplot2 with 2 panels with a log scale on only 1

  Kiến thức lập trình

I want to plot Gross Domestic Product per capita (gdppc) and growthRate = diff(log(gdppc)) in 2 panels with gdppc on a log scale and growthRate on a linear scale.

EXAMPLE:

set.seed(1)
growthRate <- rnorm(100, 1)
Y <- exp(cumsum(growthRate))
DF <- data.frame(year=seq(1901, 2000), growthRate, Y)

DFlong <- pivot_longer(DF, -1)

plot0 <- ggplot(DFlong, 
    aes(x = year, y = value))
(plot1 <- (plot0 + geom_line() + 
    facet_grid(rows = vars(name), scales='free')))

(plot2 <- plot1 + scale_y_continuous(trans='log10'))

How can I apply trans=’log10′ to only the “Y” panel
and not ‘growthRate’?

LEAVE A COMMENT