Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create.boxplot: different text above each box #137

Open
jarbet opened this issue Jul 13, 2023 · 2 comments
Open

create.boxplot: different text above each box #137

jarbet opened this issue Jul 13, 2023 · 2 comments

Comments

@jarbet
Copy link
Contributor

jarbet commented Jul 13, 2023

For create.boxplot, I am trying to add unique text above each box. The documentation for text.labels suggests this should be possible:

Labels for additional text. If the formula contains group, the length of this argument should match with the number of groups.

However, when I pass a vector c('A', 'B') to text.labels, it only plots the first element A above each box:

# modified from example in ?create.boxplot
    
library(BoutrosLab.plotting.general);
#> Loading required package: lattice
#> Loading required package: latticeExtra
#> Loading required package: cluster
#> Loading required package: hexbin
#> Loading required package: grid
#> 
#> Attaching package: 'BoutrosLab.plotting.general'
#> The following object is masked from 'package:stats':
#> 
#>     dist

set.seed(779);
groupA <- rnorm(n = 100, mean = 10, sd = 2);
groupB <- rnorm(n = 134, mean = 10.5, sd = 2);

# Create data frame for plotting
to.plot <- data.frame(
    y = rep(
        c('1', '2'),
        times = c(100, 134)
        ),
    x = c(groupA, groupB)
    );


# Plot and display difference
create.boxplot(
    formula = x ~ y,
    data = to.plot,
    add.stripplot = TRUE,
    add.text = TRUE,
    text.labels = c('A', 'B'),
    text.x = c(1, 2),
    text.y = 15.3,
    text.col = 'black',
    text.cex = 1.5,
    text.fontface = 'bold',
    ylimits = c(
        min(to.plot$x) - abs(min(to.plot$x) * 0.1),
        max(to.plot$x) + abs(max(to.plot$x) * 0.1)
        ),
    resolution = 200
    )
#> Warning in validDetails.text(x): NAs introduced by coercion

Created on 2023-07-13 by the reprex package (v2.0.1)

@jarbet
Copy link
Contributor Author

jarbet commented Jul 13, 2023

Ahh I'm now thinking text.label only supports 1 element per panel, so if one uses | group in the formula, then you can pass a vector to text.label to get 1 text element per panel.

Nevertheless, how can I get unique text above each box in a single panel plot?

EDIT: I suppose one option is to use + layer(panel.text(...));

@stefaneng
Copy link
Contributor

@jarbet

I think your edit is probably the best solution (panel.text in a layer)

You are correct about the panel and grouping. See this example:

library(BoutrosLab.plotting.general);

set.seed(779);
groupA <- rnorm(n = 100, mean = 10, sd = 2);
groupB <- rnorm(n = 134, mean = 10.5, sd = 2);

# Create data frame for plotting
to.plot <- data.frame(
    y = rep(
        c('1', '2'),
        times = c(100, 134)
        ),
    x = c(groupA, groupB)
    );

to.plot.groups <- rbind(
  data.frame(to.plot, z = 1),
  data.frame(to.plot, z = 2)
  )
to.plot.groups$z <- as.factor(to.plot.groups$z) 
# replicate(2, to.plot)


# Plot and display difference
create.boxplot(
    formula = x ~ y | z,
    data = to.plot.groups,
    add.stripplot = TRUE,
    add.text = TRUE,
    text.labels = c('A', 'B'),
    text.x = c(1, 2),
    text.y = 15.3,
    text.col = 'black',
    text.cex = 1.5,
    text.fontface = 'bold',
    ylimits = c(
        min(to.plot$x) - abs(min(to.plot$x) * 0.1),
        max(to.plot$x) + abs(max(to.plot$x) * 0.1)
        )
    );

Created on 2023-07-13 with reprex v2.0.2

Note that this behavior is not consistent across different plot types (not sure what the "correct" way should be). See how it works for scatterplot:

library(BoutrosLab.plotting.general);
#> Loading required package: lattice
#> Loading required package: latticeExtra
#> Loading required package: cluster
#> Loading required package: hexbin
#> Loading required package: grid
#> 
#> Attaching package: 'BoutrosLab.plotting.general'
#> The following object is masked from 'package:stats':
#> 
#>     dist
set.seed(13);
to.plot.scatter <- data.frame(
  x = rnorm(100),
  y = rnorm(100),
  z = c(1,2)
  );

create.scatterplot(
  y ~ x,
  data = to.plot.scatter,
  add.text = TRUE,
  text.labels = c('a', 'b'),
  text.x = c(1, 2),
  text.y = 1
  )

create.scatterplot(
  y ~ x | z,
  data = to.plot.scatter,
  add.text = TRUE,
  text.labels = c('a', 'b'),
  text.x = c(1, 2),
  text.y = 1
  );

Created on 2023-07-13 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants