zeehio/facetscales

Different axis titles for different facets

Make42 opened this issue · 4 comments

I would like to be able to also use different names for the different scales, e.g. because I need to write "xyz-value" and another time "xyz-value (log)". This does not work yet:

 library(ggplot2)
 library(scales)
 library(facetscales)
 mydf <- data.frame(
   Subject = rep(c("A", "B", "C", "D"), each = 3),
   Magnitude = rep(c("SomeValue", "Percent", "Scientific"), times = 4),
   Value = c(c(170,0.6,2.7E-4),
             c(180, 0.8, 2.5E-4),
             c(160, 0.71, 3.2E-4),
             c(159, 0.62, 3E-4)))
 
 scales_y <- list(
   Percent = scale_y_continuous(name='AAA', labels = percent_format()),
   SomeValue = scale_y_continuous(name='BBB'),
   Scientific = scale_y_continuous(name='CCC', labels = scientific_format())
 )
 
 ggplot(mydf) +
   geom_point(aes(x = Subject, y = Value)) +
   facet_grid_sc(rows = vars(Magnitude), scales = list(y = scales_y))

image

This is something different than the "Percent", "Scientific" and "Some Value" texts!

Hello,

I normally define the "label" of each of the variable in a separate data frame:
library(dplyr)

Magnitude_names <- data.frame(Magnitude = c("SomeValue", "Percent", "Scientific"), Label = c("Some scale", "This is Percentage", paste("xyz", "[meters]", sep = "\n")))

Second, you define the scales and change the name of the list to the labels:

scales_y <- list( Percent = scale_y_continuous(labels = percent_format()), SomeValue = scale_y_continuous(), Scientific = scale_y_continuous(labels = scientific_format()) )

names(scales_y) <- Magnitude_names$Label

Third, join the original data frame with the names:

mydf2 <- mydf %>% left_join(Magnitude_names, by = "Magnitude")

And finally plot the new data frame. Notice that now the faceting variable is no longer Magnitude, but is now Label:

ggplot(mydf2) + geom_point(aes(x = Subject, y = Value)) + facet_grid_sc(rows = vars(Label), scales = list(y = scales_y), switch = "y")

The switch = "y" allow to have the strips on the same side of the scale, which make more sense.
Notice the trick of defining the Label using paste("xyz", "[meters]", sep = "\n") which allow to have the label separated in different lines.

Hope this helps.
bye

Fixing this would require changes to the ggplot2 package, and those changes would be probably not be accepted.

As a workaround, you can use something like cowplot or do like @ravenroadresources said: use the facet strips with the labeller = argument.

See https://ggplot2.tidyverse.org/reference/labeller.html

To control appearance, ggplot2 offers many theme options to customize the facet strips. See the strip options at https://ggplot2.tidyverse.org/reference/theme.html

Some of the available options are:

  • choose to place the strip on the outside of the axis
  • choose the orientation of the text in the strip
  • choose the background color of the strip

I will leave the issue open for future reference (Question 3 on issue #1)

I don't think maintaining this project is a good idea for reasons outlined in the readme. Therefore I am closing all issues and pull requests and archiving the project. Feel free to fork and maintain this if you like.