0

I need to plot a y axes on the right(inflation_rate) and another y axes on the left(price) using the data frame below.

I have a dataframe which consists of a price and inflation rate over 10 years:

    Year        Price   inflation_rate
     1          59424    9
     2          64344    7
     3          73200    6
     4          72072    5
     5          76104    4
     6          84444   -2
     7          90792    3
     8          94464    0
     9          99504    8
    10         103992    1

The code to generate the above is:

library(dplyr)
set.seed(300)
Price<-c(
                  59424,
                  64344,
                  73200,
                  72072,
                  76104,
                  84444,
                  90792,
                  94464,
                  99504,
                  103992
                  )
year<-data.frame(c(seq(1:10)))
names(year)<-"Year"
priceinflation<-cbind(year, Price)
priceinflation<-priceinflation%>%
mutate(inflation_rate=c(sample(c(-2:10),10)))

I have used the below code to plot my dual axes chart:

library(ggplot2)
library(gtable)
library(grid)

grid.newpage()

# two plots
#just do the normal plots here
p1 <- ggplot(priceinflation, aes(Year, Price)) + 
  geom_line(colour = "blue") + 
  theme(panel.background = element_blank())+
  scale_y_continuous(labels=comma) +
  scale_x_discrete(limits=(-3:10))
p2 <- ggplot(priceinflation, aes(x=Year,y=inflation_rate)) + 
  geom_line(colour = "red") + 
  theme(panel.background = element_blank())+
  scale_y_discrete(limits=(-3:10))
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)

# extract gtable
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

# overlap the panel of 2nd plot on that of 1st plot
pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

# axis tweaks
ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

# draw it
grid.draw(g)

This is my plot:

There are various problem here:
1. The x axis is offscaled, the 0 is not within the chart.
2. The secondary y axis does not show up till 10, it cuts at 9.
3. The line chart have many white gridlines.
4. No legends to distinguish the 2 charts

Kindly seek advices to solve my 4 problems above.

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
R_abcdefg
  • 145
  • 1
  • 11
  • 1
    `object incomeinflation not found`; `object comma not found` – pogibas Sep 19 '17 at 17:48
  • 1
    Do all the plotting in a single ggplot object. Then you won't need to worry about aligning axes or manipulating grobs or gtables. Here are examples: [(1)](https://rpubs.com/MarkusLoew/226759); [(2)](https://stackoverflow.com/a/45777841/496488); [(3)](https://stackoverflow.com/a/46047003/496488). – eipi10 Sep 19 '17 at 17:57
  • @PoGibas Thanks for pointing out. Edited as above – R_abcdefg Sep 19 '17 at 18:01
  • @eipi10 How do I use it on my plot? I don't understand. Could you type the full code for me? thanks. – R_abcdefg Sep 19 '17 at 18:02
  • @rnoobie object 'gp1' not found – pogibas Sep 19 '17 at 18:06
  • @PoGibas thanks once again! removed the chunk and it should work well now! :) – R_abcdefg Sep 19 '17 at 18:17
  • This is unsolvable. multiple y-axis aren't implemented into `ggplot2` (and it's a good thing). There's just to much manual tweaking. Either you: 1. Switch to other package (maybe base R); 2. Use single Y-axis (which I and probably 90% off all the researches recommend). – pogibas Sep 19 '17 at 19:14
  • @PoGibas, multiple y-axes *are* implemented in ggplot2. See the linked examples in my comment. I agree they're generally inadvisable, but for those who want to live dangerously, it can be done. – eipi10 Sep 19 '17 at 19:29
  • @eipi10 to clarify. dual y-axes are implemented in `ggplot2` but _only if_ the second axis is a one-to-one transformation of the first. – acylam Sep 19 '17 at 21:12

0 Answers0