-2

I have this ggplot :plot1

I've obtained this plot using this code :

   g<- ggplot(base__, aes(x=Race_name, y=ec_prem, color=nouv_grp))+scale_color_brewer(palette = "Paired")+
  geom_jitter(position=position_jitter(0.2))+xlab("Course")+ylab("Ecart / 1er (secondes)")+ylim(-1,120)+labs(colour = "Groupe PC1")+theme_minimal()+theme(axis.text.x = element_text(size = 7, angle = 90))
g

I want to divide it into 2 plots to make vizualisation more understandable. So I used facet_grid() :

g=ggplot(base__, aes(x=Race_name, y=ec_prem, color=nouv_grp))+scale_color_brewer(palette = "Paired")+
  geom_jitter(position=position_jitter(0.2))+xlab("Course")+ylab("Ecart / 1er (secondes)")+ylim(-1,120)+labs(colour = "Groupe PC1")+theme_minimal()+theme(axis.text.x = element_text(size = 7, angle = 90))
g+facet_grid(haha~.)

and I get this plot :

plot2

But I want to get 2 differents x axis and I want my jitter plot to be less concentrated (less tight).

I hope that someone can give me a solution.

Thanks in advance :)

maarvd
  • 1,254
  • 1
  • 4
  • 14
chlooo
  • 11
  • 2
  • 1
    It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Mar 29 '21 at 10:57
  • 2
    `facet_grid()` cannot have seperate x-axis in a single column. Probably `facet_wrap()` is more appropriate for that. – teunbrand Mar 29 '21 at 11:25
  • without seeing the data, I would say of course it can: try `facet_grid(haha~., scales = "free_y")` – Roman Mar 29 '21 at 14:26

1 Answers1

1

As mentioned by teunbrand, facet_grid() won't allow different scales for an aligned axis (i.e. uniform x in a column, uniform y in a row) but that facet_wrap() can help get you around this. However, in order to get the axis text to print for each facet, I think lemon::facet_rep_wrap() is what you need. See below for a little example.

library(tidyverse)
library(lemon)
#> 
#> Attaching package: 'lemon'
#> The following object is masked from 'package:purrr':
#> 
#>     %||%
#> The following objects are masked from 'package:ggplot2':
#> 
#>     CoordCartesian, element_render

mtcars %>%
  rownames_to_column(var = "car_name") %>%
  ggplot(aes(x = car_name, y = mpg)) +
  geom_col() +
  facet_rep_wrap(
    facets = vars(cyl),
    ncol = 1,
    repeat.tick.labels = TRUE,
    scales = "free"
  ) +
  theme(axis.text.x = element_text(
    angle = 90,
    hjust = 1,
    vjust = 0.5
  ))

Created on 2021-03-29 by the reprex package (v1.0.0)

Dan Adams
  • 4,971
  • 9
  • 28
  • thanks Dan Adams, I think that this is the solution I need. The variable corresponding to "car_name" in my data frame is "Race_name" and I get this error : "Column name `Race_name` must not be duplicated." I will try to see what's wrong – chlooo Mar 30 '21 at 08:51