-
Notifications
You must be signed in to change notification settings - Fork 84
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
annotation_logticks with marginal plots #141
Comments
Copying scales is difficult. You'll probably see better results if you're not using |
I might be misunderstanding something but replacing library(tidyverse)
library(cowplot)
#>
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#> default ggplot2 theme anymore. To recover the previous
#> behavior, execute:
#> theme_set(theme_cowplot())
#> ********************************************************
x = 10 ^ rnorm(100)
y = 10 ^ rnorm(100)
x[x > 1] = 1
y[y > 1] = 1
x[sample(seq_along(x), 40)] = NA
y[sample(seq_along(y), 40)] = NA
dat = data.frame(x = x, y = y)
dat1 = filter(dat, !is.na(x), !is.na(y))
dat2 = filter(dat, is.na(x), !is.na(y))
dat3 = filter(dat, !is.na(x), is.na(y))
# base plot
p_base = ggplot(dat1, aes(x = x, y = y)) +
geom_point() +
scale_x_log10(limits = c(1e-3, 1)) +
scale_y_log10(limits = c(1e-3, 1)) +
annotation_logticks(sides = 'bl') +
theme_cowplot()
# marginal plots
p_y = ggplot(dat2, aes(x = 1, y = y)) +
geom_jitter(width = 0.05, color = 'blue') +
scale_y_log10(limits = c(1e-3, 1)) +
scale_x_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'l') +
theme_cowplot()
p_x = ggplot(dat3, aes(x = x, y = 1)) +
geom_jitter(width = 0.05, color = 'red') +
scale_x_log10(limits = c(1e-3, 1)) +
scale_y_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'b') +
theme_cowplot()
# combine
p = p_base %>%
insert_yaxis_grob(p_y, position = "left") %>%
insert_xaxis_grob(p_x, position = "bottom")
#> Warning: Removed 5 rows containing missing values (geom_point).
#> Warning: Removed 15 rows containing missing values (geom_point).
ggdraw(p) Created on 2019-07-15 by the reprex package (v0.2.1) |
I get it now. Fixed in master. library(tidyverse)
library(cowplot)
#>
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#> default ggplot2 theme anymore. To recover the previous
#> behavior, execute:
#> theme_set(theme_cowplot())
#> ********************************************************
x = 10 ^ rnorm(100)
y = 10 ^ rnorm(100)
x[x > 1] = 1
y[y > 1] = 1
x[sample(seq_along(x), 40)] = NA
y[sample(seq_along(y), 40)] = NA
dat = data.frame(x = x, y = y)
dat1 = filter(dat, !is.na(x), !is.na(y))
dat2 = filter(dat, is.na(x), !is.na(y))
dat3 = filter(dat, !is.na(x), is.na(y))
# base plot
p_base = ggplot(dat1, aes(x = x, y = y)) +
geom_point() +
scale_x_log10(limits = c(1e-3, 1)) +
scale_y_log10(limits = c(1e-3, 1)) +
annotation_logticks(sides = 'bl') +
theme_cowplot()
# marginal plots
p_y = ggplot(dat2, aes(x = 1, y = y)) +
geom_jitter(width = 0.05, color = 'blue') +
scale_y_log10(limits = c(1e-3, 1)) +
scale_x_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'l') +
theme_cowplot()
p_x = ggplot(dat3, aes(x = x, y = 1)) +
geom_jitter(width = 0.05, color = 'red') +
scale_x_log10(limits = c(1e-3, 1)) +
scale_y_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'b') +
theme_cowplot()
# combine
p = p_base %>%
insert_yaxis_grob(p_y, position = "left") %>%
insert_xaxis_grob(p_x, position = "bottom")
#> Warning: Removed 5 rows containing missing values (geom_point).
#> Warning: Removed 19 rows containing missing values (geom_point).
ggdraw(p) Created on 2019-07-15 by the reprex package (v0.3.0) |
Thanks! |
This issue seems to be recurring with cowplot 1.0.0 and ggplot2 3.3.0: library(tidyverse)
library(cowplot)
#>
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#> default ggplot2 theme anymore. To recover the previous
#> behavior, execute:
#> theme_set(theme_cowplot())
#> ********************************************************
x = 10 ^ rnorm(100)
y = 10 ^ rnorm(100)
x[x > 1] = 1
y[y > 1] = 1
x[sample(seq_along(x), 40)] = NA
y[sample(seq_along(y), 40)] = NA
dat = data.frame(x = x, y = y)
dat1 = filter(dat, !is.na(x), !is.na(y))
dat2 = filter(dat, is.na(x), !is.na(y))
dat3 = filter(dat, !is.na(x), is.na(y))
# base plot
p_base = ggplot(dat1, aes(x = x, y = y)) +
geom_point() +
scale_x_log10() +
scale_y_log10() +
annotation_logticks(sides = 'bl') +
theme_cowplot()
# marginal plots
p_y = axis_canvas(p_base, axis = "y") +
geom_jitter(data = dat2, aes(x = 1, y = y), width = 0.05,
color = 'blue') +
scale_y_log10() +
scale_x_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'l') +
theme_cowplot()
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
p_x = axis_canvas(p_base, axis = "x") +
geom_jitter(data = dat3, aes(x = x, y = 1), width = 0.05,
color = 'red') +
scale_x_log10() +
scale_y_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'b') +
theme_cowplot()
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.
# combine
p = p_base %>%
insert_yaxis_grob(p_y, position = "left") %>%
insert_xaxis_grob(p_x, position = "bottom")
#> Warning: Removed 18 rows containing missing values (geom_point).
ggdraw(p) Created on 2020-05-19 by the reprex package (v0.3.0) sessionInfo():
|
Seems to be a different problem than before because the logticks are also misaligned now. Actually, I now think that the patchwork library is a better approach for this type of plot. Have you tried using patchwork? |
Oops, the log-ticks misalignment was just because I forgot to set the axis limits in the base plot: library(tidyverse)
library(cowplot)
#>
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#> default ggplot2 theme anymore. To recover the previous
#> behavior, execute:
#> theme_set(theme_cowplot())
#> ********************************************************
x = 10 ^ rnorm(100)
y = 10 ^ rnorm(100)
x[x > 1] = 1
y[y > 1] = 1
x[sample(seq_along(x), 40)] = NA
y[sample(seq_along(y), 40)] = NA
dat = data.frame(x = x, y = y)
dat1 = filter(dat, !is.na(x), !is.na(y))
dat2 = filter(dat, is.na(x), !is.na(y))
dat3 = filter(dat, !is.na(x), is.na(y))
# base plot
p_base = ggplot(dat1, aes(x = x, y = y)) +
geom_point() +
scale_x_log10(limits = c(1e-3, 1)) +
scale_y_log10(limits = c(1e-3, 1)) +
annotation_logticks(sides = 'bl') +
theme_cowplot()
# marginal plots
p_y = ggplot(dat2, aes(x = 1, y = y)) +
geom_jitter(width = 0.05, color = 'blue') +
scale_y_log10(limits = c(1e-3, 1)) +
scale_x_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'l') +
theme_cowplot()
p_x = ggplot(dat3, aes(x = x, y = 1)) +
geom_jitter(width = 0.05, color = 'red') +
scale_x_log10(limits = c(1e-3, 1)) +
scale_y_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'b') +
theme_cowplot()
# combine
p = p_base %>%
insert_yaxis_grob(p_y, position = "left") %>%
insert_xaxis_grob(p_x, position = "bottom")
#> Warning: Removed 4 rows containing missing values (geom_point).
#> Warning: Removed 17 rows containing missing values (geom_point).
ggdraw(p) Created on 2020-05-21 by the reprex package (v0.3.0) Yes, I can just update the code to use patchwork instead. I would still be interested to understand why exactly this is breaking, though, because I wonder if it’s related to some other issues I’ve been having with ggplot2 since the version 3 release (as far as I can tell, the |
So I can't reproduce this, actually. Which version of R are you using? I'm still at 3.6. If you're at 4.0, maybe something has changed in how clipping works in grid? You could try library(tidyverse)
library(cowplot)
#>
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#> default ggplot2 theme anymore. To recover the previous
#> behavior, execute:
#> theme_set(theme_cowplot())
#> ********************************************************
x = 10 ^ rnorm(100)
y = 10 ^ rnorm(100)
x[x > 1] = 1
y[y > 1] = 1
x[sample(seq_along(x), 40)] = NA
y[sample(seq_along(y), 40)] = NA
dat = data.frame(x = x, y = y)
dat1 = filter(dat, !is.na(x), !is.na(y))
dat2 = filter(dat, is.na(x), !is.na(y))
dat3 = filter(dat, !is.na(x), is.na(y))
# base plot
p_base = ggplot(dat1, aes(x = x, y = y)) +
geom_point() +
scale_x_log10(limits = c(1e-3, 1)) +
scale_y_log10(limits = c(1e-3, 1)) +
annotation_logticks(sides = 'bl') +
theme_cowplot()
# marginal plots
p_y = ggplot(dat2, aes(x = 1, y = y)) +
geom_jitter(width = 0.05, color = 'blue') +
scale_y_log10(limits = c(1e-3, 1)) +
scale_x_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'l') +
theme_cowplot()
p_x = ggplot(dat3, aes(x = x, y = 1)) +
geom_jitter(width = 0.05, color = 'red') +
scale_x_log10(limits = c(1e-3, 1)) +
scale_y_continuous(limits = c(0.9, 1.1)) +
annotation_logticks(sides = 'b') +
theme_cowplot()
# combine
p = p_base %>%
insert_yaxis_grob(p_y, position = "left") %>%
insert_xaxis_grob(p_x, position = "bottom")
#> Warning: Removed 8 rows containing missing values (geom_point).
#> Warning: Removed 19 rows containing missing values (geom_point).
ggdraw(p) Created on 2020-05-21 by the reprex package (v0.3.0) |
When inserting grobs into the left and bottom positions of the “main” plot panel, the axes are automatically adjusted to align with the “main” panel. However, the same behaviour doesn’t occur for
annotation_logticks
, which spill over into the inserted grobs.I would like to achieve an effect where the
annotation_logticks
has the boundaries of the ones in the inner panel in the minimal example shown below, but is aligned with the axes. Is this possible?Created on 2019-07-15 by the reprex package (v0.2.1)
The text was updated successfully, but these errors were encountered: