0

I'm trying to plot a line and a barchat using ggplot2 package but it's seems to be hard to get two different y-axis when using facet_grid() function...

I'd like to add to my current plot a barchart with the Frequency of each product (variable Freq) in the data frame. Any help would be really awesome!!

temp = data.frame(Product=as.factor(c("L","P","41","43")),
              Freq = c(0.2,0.8,0.7,0.3),
              rate = c(14,17,12,20),
              var= c("QUAL","QUAL","OCCU","OCCU"))

temp %>%  ggplot() + theme_grey(base_size=20) + 
geom_line(aes(x=Product, y=rate, group=var))+  
geom_point(aes(x=Product, y=rate, group=var))+ 
geom_label( aes(x=Product,y=rate,label=paste0(rate,"%") ))  +
  facet_grid(.~ var, scales = "free") +
 theme(legend.position="none", axis.text.x=element_text(angle=45, vjust=0.1)) -> p2

Image

H_el
  • 1
  • 1
  • 3
  • Are you saying you want two different y-axes on each facet (one for the bars and one for the other geoms) or are you saying you want each facet to have a different y-scale? Also, the plot you linked to is different than the plot your data and code produces. There is also no column `N` in your data--do you mean `Freq`?. – eipi10 Jun 03 '16 at 15:38
  • Thanks for the answer. I mean that i want two different y-axes on each facet ! And you're right, the column is not N but Freq :) – H_el Jun 03 '16 at 15:45
  • `ggplot2` does not have a method to create dual-y-axis plots (see [here](http://stackoverflow.com/a/3101876/496488) for the reasons for this). However, if you're set on creating one, [this](https://rpubs.com/kohske/dual_axis_in_ggplot2) might help. – eipi10 Jun 03 '16 at 15:51
  • I tried to use this link to make a dual y-axis but it doesn't work when I'm using the facet_grid function... – H_el Jun 03 '16 at 15:57
  • That sounds like relevant information that should be included in your question - with your code of the attempt. – Gregor Thomas Jun 03 '16 at 16:03

1 Answers1

1

One alternative would be to use grid.arrange{gridExtra}

library(gridExtra)

### 1. create a plot function

plotfunc <- function(Data, xxx , ymin, ymax) {

   ggplot(data=subset(temp, var==xxx)) + theme_grey(base_size=20) + 
    geom_line(aes(x=Product, y=rate, group=var))+  
    geom_point(aes(x=Product, y=rate, group=var))+ 
    geom_label( aes(x=Product,y=rate,label=paste0(rate,"%") ))  +
    facet_grid(.~ var, scales = "free") +
    theme(legend.position="none", axis.text.x=element_text(angle=45, vjust=0.1)) +
    ylim(ymin, ymax) 
    } 

### 2. Generate the plots with different axis limits
occuplot <- plotfunc(temp, "OCCU", 10, 20)
qualplot <- plotfunc(temp, "QUAL", 12, 18)

### 3. Arrange the separate plots into one single chart
grid.arrange( occuplot, qualplot, nrow=1, ncol=2) 

enter image description here

rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
  • Well, I think I was not clear enough but I want two different y-axes on each facet and it`s a kind of difficult with ggplot2 apparently – H_el Jun 04 '16 at 09:41
  • I see. I'd say the best alternative would be to produce two separate plots with dual axis (one for `OCCU` and one for `QUAL`) following @eipi10's [suggestion here](https://rpubs.com/kohske/dual_axis_in_ggplot2) and then use `grid.arrange{gridExtra}` as I proposed here. – rafa.pereira Jun 04 '16 at 10:36