Git Product home page Git Product logo

facetscales's Introduction

Project archived

I no longer think this is a good approach or idea, I suggest you use either cowplot or patchwork.

Fixing the multiple issues of this package requires changes to ggplot2 that are not going to happen.

So it is better to archive this project to avoid further confusion

If you still want access to this project, the last version is available at the archived branch.

facetscales, archived

facetscales's People

Contributors

zeehio avatar teunbrand avatar

Stargazers

Charlie Diamond avatar  avatar  avatar siman avatar  avatar  avatar Yinqi Zhao avatar Benedikt Langenberg avatar Adam Howes avatar  avatar Claudio Zandonella Callegher avatar Jingmeng Cui avatar Quentin Guilloteau avatar Alexandra Moskalewicz avatar Nhi Hin avatar Eric Largy avatar Kun Guo avatar Andy Garcia avatar onurcanbektas avatar Tania L. Maxwell avatar Masatoshi Katabuchi avatar Chris Kennedy avatar Jonathan Berrisch avatar  avatar Roman Cheplyaka avatar Greg Pilgrim avatar  avatar aaumaitre avatar  avatar Muad Abd El Hay avatar  avatar  avatar Felipe Mattioni Maturana, Ph.D. avatar Luke Smith avatar Susan VanderPlas avatar Nicolas Reigl avatar Srikanth K S avatar  avatar Joshua Kunst avatar Leo Lee avatar timelyportfolio avatar Ross    avatar Tara Sutjarittham avatar  avatar Nick Cauldron avatar Fabian Bernhard avatar  avatar David N. Matzig avatar Hendrik Raese avatar Clemens Schmid avatar Krzysztof Joachimiak avatar Paula Restrepo avatar Anthony Caravaggi avatar Gaotian Zhang avatar Justin Millar avatar Shawn Taylor avatar CamiloEr avatar  avatar Hongyuan Jia avatar Spencer Nystrom avatar Michael Kammer avatar Mary Fisher avatar Losses Don avatar Alexey Shiklomanov avatar Matthias Grenié avatar Oleksii Bashkanov avatar Martin Lukac avatar Norbert Köhler avatar Sebastian Schlag avatar Hongyang Li avatar Kathryn Cogert, PhD avatar Jason J. Benedict avatar Bryce Frank avatar Johannes Gruber avatar Daniel Wells avatar  avatar RavenRoad S.A.S. avatar tarko avatar Edmundo Peralta avatar Tung N avatar Omri Mendels avatar Stefan Avey avatar Jemma Stachelek avatar Matt Forshaw avatar Beatriz Milz avatar Rafael Veras avatar Roberto Salas avatar Christofer Bäcklin avatar Fumeng Yang avatar

Watchers

 avatar timelyportfolio avatar James Cloos avatar Matt Forshaw avatar Fumeng Yang avatar Jake Hosen avatar

facetscales's Issues

Scale transformations per facet don't work

Hi,
Thank you for your package! I try to set the x and y axes with:

scales_y <- list(
  `Score` = scale_y_continuous(),
  `Trial duration (s)` = scale_y_continuous(),
  `Accuracy (%)` = scale_y_continuous(limits=c(0.1, 50), trans='log2'))

scales_x <- list(
  `Score` = scale_x_continuous(),
  `Trial duration (s)` = scale_x_continuous(),
  `Accuracy (%)` = scale_x_continuous(limits=c(0.1, 50), trans='log2'))

facet_grid_sc(rows = vars(metric), scales = list(y = scales_y, x = scales_x))

But I get the following message:

Error in .subset2(x, i, exact = exact) : 
  attempt to select less than one element in get1index

Problem in ggplot2 3.2.1

In this version of ggplot2, when I used:

ggplot (mydf,
aes (x = X, y = Y))+
geom_boxplot()+
facet_grid(Z ~., scales = list (y = scale_y))

Having scale_y a list with names of each facet painel (as in example code), I had this error message:

Error in match.arg(scales, c("fixed", "free_x", "free_y", "free")) :
'arg' must be NULL or a character vector

Did not make change with coord_flip()

Great contribution. But I want to report that your package did not work with coord_flip(). I found the problem when I created my own plot with horizontal bars (which is why I flipped the axes). The new (varying) axes text is missing except for the leftmost panel/facet. But when I removed coord_flip() +, your package worked perfectly. So it seems internally it worked, but the changes are not displayed.

To reproduce the problem in your example data, you could try adding "coord_flip() +" to make the variable "Subject" on the y-axis, and renaming the subject names across different facet (e.g. A-D, a-d, 1-4, ID1-4, ...)

Some questions to answer

This is the feedback @clauswilke gave me at tidyverse/ggplot2#2747

I have a number of questions:

  1. Should this be implemented for facet_wrap() as well?

Tracked on #7

  1. What happens with coord_fixed()?

Error: coord_fixed doesn't support free scales

  1. What happens when people try to set individual titles for the different scales?

Tracked on #4

  1. When I have a grid of plots with multiple columns and rows, can I set a different scale for each plot?

Not yet addressed

  1. What happens when facets are renamed via a labeller function?

Facets are renamed on the plot. No apparent issue here.

  1. What happens when people define a scale outside of the facet_grid() call, and does it matter whether it's called before or after facet_grid()?

That scale should be used if it is not overriden by facet_grid_sc. This needs to be checked and tested.

This may be a case where implementing this first in a package to gain some experience with this feature and explore the best interface may be a good idea.

I plan to address and refine the facetscales API (as time permits) and then submit a pull request with those issues (and other issues that will appear) addressed. Discussion is welcome!

Error when using date column for x-axis

Shouldn't this work:

library(ggplot2)
library(scales)
library(facetscales)
mydf <- data.frame(
  Date = rep(as.Date(c("2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04")), each = 4),
  Magnitude = rep(c("SomeValue", "Percent", "Scientific", "LogScale"), times = 4),
  Value = c(c(170, 0.60, 2.7E-4, 10000),
            c(180, 0.80, 2.5E-4, 100),
            c(160, 0.71, 3.2E-4, 1000),
            c(159, 0.62, 3.0E-4, 10)))

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

ggplot(mydf) +
  geom_point(aes(x = Date, y = Value)) +
  facet_grid_sc(rows = vars(Magnitude), scales = list(y = scales_y))

Does it support scale_y_log10?

I'm new to both the package and scale_y_log10, so I may be doing something very inapt. I was essentially trying to apply instructions for annotation_logticks with the facetscales README example:

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(labels = percent_format()),
    SomeValue = scale_y_continuous(),
    logs = scale_y_log10(
        breaks = scales::trans_breaks("log10", function(x) 10^x),
        labels = scales::trans_format("log10", scales::math_format(10^.x))
    )
)

ggplot(mydf) +
    geom_point(aes(x = Subject, y = Value)) +
    facet_grid_sc(rows = vars(Magnitude), scales = list(y = scales_y))

and that gives

Error in x$clone() : attempt to apply non-function

What am I doing wrong? Thanks!

facet_grid_sc cols argument not working

In facet_grid_sc I get error when trying to plot the facet by columns using the cols argument:

Error in .subset2(x, i, exact = exact) : 
  attempt to select less than one element in get1index

It works if i specify the rows argument. Likewise, if i use formula type~. it works but I get the same error above with .~type.

Would be best, if like facet_grid, we could specify nrow & ncol to have more control of the facet plotting.

Many thanks!

match.arg: 'arg' must be NULL or a character vector

I'm seeing the same error as reported here:
https://stackoverflow.com/questions/56411786/adjust-x-and-y-scales-for-individual-facets-in-facet-grid-to-fit-geom-text-re by M--.

The exact error text is: "Error in match.arg: 'arg' must be NULL or a character vector."

In my code, the scale_y variable is:
$propofol\n(mcg/ml)

Range:
Limits: 0 -- 1

$remifentanil\n(ng/ml)

Range:
Limits: 0 -- 1

$fentanyl\n(ng/ml)

Range:
Limits: 0 -- 1

$rocuronium\n(mcg/ml)

Range:
Limits: 0 -- 1

The error is being thrown by
facet_grid(
Wrap ~ .,
scales = list(y=scales_y)
)

I can supply additional details, but the code is a fairly substantial shiny application (see steveshafer.shinyapps.io/stanpumpR).

I appreciate your work on this package!

Steve Shafer

Support multiple vars()

This seems to be a great package I have been searching for in a while.

I was wondering if it does support multiple variables in the same direction, e.g. facet_grid_sc(rows = vars(drv, cyl)), resulting in plots such as:
Rplot01
Is this currently supported? What would be the format of the scales = list(y= ...) ?
Below I have tried my luck with a nested list. Unfortunately this doesn't work.

library(ggplot2)
library(facetscales)

data_to_plot <- mpg
data_to_plot$repeated_drv <- paste0("repeated_", data_to_plot$drv)

scales_y_multiple <- list(
  `4` = list(`repeated_4`= scale_y_continuous(limits = c(5, 25))),
  `f` = list(`repeated_f`= scale_y_continuous(limits = c(-40, 40))),
  `r` = list(`repeated_r`= scale_y_continuous(limits = c(10, 20))))

ggplot(data_to_plot, aes(displ, cty)) + 
  geom_point() + 
  facet_grid_sc(rows   = vars(drv, repeated_drv), 
                scales = scales_y_multiple) +
  theme_bw()

Unable to set variable scales of Y axis

Hello,

Thank you for making this functionality.
I am unable to set the Y axis scales while using facet_grid_sc. It just takes the values for the first category (limits=c(0,0.08)) for all. I am submitting the parameter as

scales_y <- list(
Category1" = scale_y_continuous(limits=c(0,0.08)),
Category2 = scale_y_continuous(limits=c(0,0.22)),
Category3 = scale_y_continuous(limits=c(0,0.08))
)
facet_grid_sc(Category ~ Sample, scales=list(y = scales_y))

The full command is:
ggplot(data=melted,aes(x=variable,y=value,fill=majortype, width=0.4))+geom_bar(stat="identity") + scale_x_discrete(labels=melted$minortype) + theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.title=element_blank(), legend.position="none", legend.text=element_text(size=0), axis.text.x=element_blank(), axis.text.y=element_text(angle = 90 , hjust = 0.5, size=5)) + guides(col = guide_legend(nrow = 1), fill = guide_legend(nrow = 1)) + facet_grid_sc(Category ~ Sample, scales=list(y = scales_y)) + theme(plot.margin = unit(c(1.2,0.4,0.9,0.5), "lines"))

Am I incorrectly plotting this?

Different axis titles for different facets

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!

facet_grid_sc does not seem to work with dates on x axis.

For just numerical data facet_grid and facet_grid_sc seem to work fine (i.e. both of these plots work):

test_df <- 
     tibble( date = seq(1, 16), 
             y1 = rnorm(n = 16), 
             y2 = seq(1, 16)
             ) %>% 
     melt(id.vars = c('date')) %>% 
     {.}

ggplot(data = test_df, aes( x = date, y = value)) +          
          geom_line() +          
          facet_grid(variable~.) +
          geom_blank()

scales_y <- list(
       `y1` = scale_y_continuous(),
       `y2` = scale_y_continuous()
               )

ggplot(data = test_df, aes( x = date, y = value)) +          
          geom_line() +    
          facet_grid_sc(rows = vars(variable), scales = list(y = scales_y )) +      
          geom_blank() 

But facet_grid_sc does not work for date data on the x axis (i.e. the second plot fails):

test_df <- 
     tibble( date = seq(as.Date('2015-09-15'), as.Date('2015-09-30'), by = "1 days"), 
             y1 = rnorm(n = 16), 
             y2 = seq(1, 16)
             ) %>% 
     melt(id.vars = c('date')) %>% 
     {.}

ggplot(data = test_df, aes( x = date, y = value)) +          
          geom_line() +          
          facet_grid(variable~.) +     
          geom_blank()

scales_y <- list(
       `y1` = scale_y_continuous(),
       `y2` = scale_y_continuous()
               )

ggplot(data = test_df, aes( x = date, y = value)) +          
          geom_line() +
          facet_grid_sc(rows = vars(variable), scales = list(y = scales_y )) +   
          geom_blank()

Does not load on mac

I'm running a mac (macos 10.13.5) with R 3.5.0. The install does not work correctly and in fact corrupts the existing installation of ggplot2. I had to uninstall facetscales and reinstall ggplot2 in order to get functionality restored.

I'm getting the following messages:

When I install:

** testing if installed package can be loaded
* DONE (facetscales)
Warning message:
In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1

So it says that it can load, but with a warning. However, when I go to load the library:

library(facetscales)
Error: package or namespace load failed for ‘facetscales’ in get(Info[i, 1], envir = env):
lazy-load database '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/ggplot2/R/ggplot2.rdb' is corrupt
In addition: Warning message:
In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1

Let me know if I can help with this issue.

Error in x$clone() : attempt to apply non-function

I'm running into the error Error in x$clone() : attempt to apply non-function

Below is a reprex. The chart renders fine when I remove the scales = list(y = scales_y, x = scales_x) portion from facet_grid_sc

Any ideas?

df <- data.frame(
  name = rep(c('A', 'B', 'C'), 2),
  type = rep(c('type_1', 'type_2', 'type_1'), 2),
  metric = rep(c('metric_1', 'metric_2'), 3),
  value = c(.01, 90, .03, 80, .09, 50)
)

scales_y <- list(
  metric_1 = scale_y_continuous(labels = scales::percent_format()),
  metric_2 = scale_y_continuous(labels = scales::comma_format())
)

scales_x <- list(
  name = scale_x_discrete()
)

df %>%
  ggplot(aes(x = name, y = value, fill = type)) +
  facet_grid_sc(cols = vars(metric), scales = list(y = scales_y, x = scales_x)) +
  geom_bar(stat = 'identity')

attempt to select less than one element in get1index

Hi! Thank you for the script! I needed something like this a lot of times in the past.

However, when I installed it using
devtools::install_github("zeehio/facetscales",force=T)
and using your sample code from this github page, I got an error:

Fehler in .subset2(x, i, exact = exact) :
attempt to select less than one element in get1index

Also, all the other tries to use it result in an error as well.
I use the default install of R (3.6.2) on a Win10 64bit machine.

Is the current version working? Or might there be a problem with my R configuration?

Boxplot looks different from facet_grid

Hi, I'm not sure why these two boxplots look different. In facet_grid_sc, y1
has shorter lines and doesn't have outliers.

set.seed(123)
y1 <- rlnorm(200, 1, 4)
y2 <- rlnorm(200, 3, 5)

dat <- tibble(y1, y2) %>%
  pivot_longer(1:2) %>%
  mutate(gr = rep(c("gr1", "gr2"), each = 200))

ggplot(dat, aes(x = name, y = value)) +
  geom_boxplot() +
  facet_grid(gr ~ .) +
  scale_y_log10()

unnamed-chunk-1-1

scales_y <- list(
 gr1 = scale_y_log10(),
 gr2 = scale_y_log10())

ggplot(dat, aes(x = name, y = value)) +
  geom_boxplot() +
  facetscales::facet_grid_sc(rows = vars(gr),
                             scales = list(y = scales_y))

unnamed-chunk-2-1

Error when faceting by column

Hi @zeehio, thanks for attending so many requests for managing each facet independently, that's a very useful package!

My data looks like that and faceting by row works nicely...

> data <- source("https://pastebin.com/raw/thCnhYfm")$value
> facetscales <- list(
  `col1` = scale_y_continuous(limits = c(0, 20), breaks = seq(0, 20, 5)),
  `col2` = scale_y_continuous(limits = c(0, 30), breaks = seq(0, 30, 5)),
  `col3` = scale_y_continuous(limits = c(0, 8), breaks = seq(0, 8, 2)),
  `col4` = scale_y_continuous(limits = c(0, 25), breaks = seq(0, 25, 5)))

> melt(data, id.vars = "group") %>% 
  ggplot(aes(group, value)) + geom_boxplot() +
  facetscales::facet_grid_sc(rows = vars(variable), scales = list(y = facetscales))

image

... but if I want to facet by column, it brings me this error:

> melt(data, id.vars = "group") %>% 
  ggplot(aes(group, value)) + geom_boxplot() +
  facetscales::facet_grid_sc(cols = vars(variable), scales = list(y = facetscales))

Error in .subset2(x, i, exact = exact) : 
  attempt to select less than one element in get1index
In addition: Warning message:
In melt(data, id.vars = "group") :
  The melt generic in data.table has been passed a data.frame and will attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(data). In the next version, this warning will become an error.

Maybe I'm missing something. Could you help me, please? Thanks in advance.

help with geom_smooth and facet_grid_sc

Dear Rxperts!

geom_smooth does not seem to work well when .combined with facet_grid_sc (... ,scales=list(x=scale_x, y=scale_y), ...)

 
 tst <- data.frame(u = rep(1:10,each=10),a=rep(seq(10,100,len=10),10),p=runif(100)+10,q=runif(100)*10) %>% gather(.,,,3:4) %>% mutate(yvar="a")

ylbs <- c(a=bquote(monster~'@'~jupiter),b=bquote(monster~'@'~stars))
xlbs <- c(p=bquote(monster~'@'~moon),q=bquote(monster~'@'~mars))


scales_y1 = list(a = scale_y_continuous()); scales_x1=list(p="scale_x_log10()+annotation_logticks()",q="scale_x_log10()+annotation_logticks()")


ggplot(tst, aes(y=a,x=value))+geom_point() + facet_grid_sc(yvar~key,scales=list(y=scales_y1,x=list(eval(parse(text=scales_x1)))),labeller=labeller(key=as_labeller(xlbs,default=label_parsed),yvar=as_labeller(ylbs,default=label_parsed)),switch="both")+geom_smooth(method="loess",data=tst,col='red')+labs(x="",y="")+theme(strip.placement = "outside")

Any suggestions/ideas on to make geom_smooth and axis formats work with facet_grid_sc()...?
I do realize that there are other ways of doing it.. The reason is I am trying to generate a multipanel plot with customized x- & y- axis scales (one or more layers of formatting for one or more panels) and titles (may contain multi-line expressions)

Many thanks...

facet_grid_sc() throws error with POSIXct on x-axis.

Using facet_grid_sc() together with a plot that uses POSIXct on x-axis throws an error: Error: Invalid input: time_trans works with objects of class POSIXct only

MWE:

library(lubridate)
library(dplyr)
library(ggplot2)
library(facetscales)

dat <- tribble(~dttm,                          ~value, ~group,
               ymd_hms("2020-04-05 11:00:00"), 10,     "A",
               ymd_hms("2020-04-05 12:00:00"), 15,     "A",
               ymd_hms("2020-04-05 11:00:00"), 2,      "B",
               ymd_hms("2020-04-05 12:00:00"), 6,      "B",
               ymd_hms("2020-04-05 11:00:00"), 100,    "C",
               ymd_hms("2020-04-05 12:00:00"), 150,    "C")

ggplot(dat, aes(x=dttm,y=value)) +
  geom_line() + geom_point() +
  facet_grid(rows = vars(group)) +
  theme_bw()

ggplot(dat, aes(x=dttm,y=value)) +
  geom_line() +
  facet_grid_sc(rows = vars(group)) +
  theme_bw()

Incompatible with expression()

When I try to adjust the scales for facets that are named by expression() (and not character strings), I get the error Error in x$clone() : attempt to apply non-function

Facet A, Facet B and others.

I have the problem that I have many facets and I just want to adjust the scale of y-axis for the first facet. Instead of specifying the parameter for each facet, I wonder if it possible to do

scales_y <- list(
A = scale_y_continuous(limits = c(0.2, 0.8)),
**others** = scale_y_continuous(limits = c(0, 40))
)

Error in x$clone() : attempt to apply non-function

Hi !

I am running into trouble trying to apply your package to my data. The idea here is I want to create mirror histograms. Data looks like this:

  Coverage_side Coverage Type
1     coverage1     7152 VP03
2     coverage1     7152 VP03
3     coverage1     4252 VP03
4     coverage1     3515 VP03
5     coverage1     3515 VP03
6     coverage1     4252 VP03


........

I'm trying to run these commands:

scales_x <- list(
  Reverse = scale_x_continuous(trans="reverse"),
  Normal = scale_x_continuous()
)

ggplot(coverageplots, aes(x=Coverage, fill=Type))+
  geom_histogram(bins=40)+
  facet_grid_sc(cols = vars(Coverage_side), scales = list(x = scales_x))+
  theme_classic()

And get the following error:

Error in x$clone() : attempt to apply non-function

I am not really sure what is happening here, any suggestions ?

Thanks a lot !
Bruno

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.