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

5
  • 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?. Commented Jun 3, 2016 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 :) Commented Jun 3, 2016 at 15:45
  • ggplot2 does not have a method to create dual-y-axis plots (see here for the reasons for this). However, if you're set on creating one, this might help. Commented Jun 3, 2016 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... Commented Jun 3, 2016 at 15:57
  • That sounds like relevant information that should be included in your question - with your code of the attempt. Commented Jun 3, 2016 at 16:03

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
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 and then use grid.arrange{gridExtra} as I proposed here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.