# how to set the distance each value in the X axis become same?

Dear all,
I would like to set the length size of X-axis become the same in each chromosome.
For example:
the distance from 0 to 5 in 2L chromosome and 2R chromosome become same.
does anyone know what is the package or script in R studio to do that?

I'm assuming these two plots are separate plots rather than faceted.

To do this, I'd use `scale_x_continuous()` and set the limits. Here is a reproducible example:

``````library(tidyverse)
library(patchwork) # let's me combine plots

# make data
dat = crossing(cat = c("a", "b"),
val = 1:10) |>
filter(if_else(cat == "b" & val > 5, F, T))

# issue - not the same
ggplot(filter(dat, cat == "a"), aes(x = val)) /
ggplot(filter(dat, cat == "b"), aes(x = val))
`````` ``````# fix - use scale_x_continuous
(ggplot(filter(dat, cat == "a"), aes(x = val)) + scale_x_continuous(limits = c(0, 10))) /
(ggplot(filter(dat, cat == "b"), aes(x = val)) + scale_x_continuous(limits = c(0, 10)))
`````` ``````# automate?
max = max(dat\$val)
(ggplot(filter(dat, cat == "a"), aes(x = val)) + scale_x_continuous(limits = c(0, max))) /
(ggplot(filter(dat, cat == "b"), aes(x = val)) + scale_x_continuous(limits = c(0, max)))
`````` Created on 2022-01-19 by the reprex package (v2.0.1)

No, These graphs are not combined.
I would like to make the distance from 0 to 5, 5 to 10 etc become same for each graph.
I think what you have explained above is to set the limit or scale of x axis value.

This is a solution that involves setting the aspect ratio of the plots, would that be appropriate? It does depend, to an extent, on what you want to do with these plots. Instead of setting the aspect ratio, you could `ggsave()` the plots with widths proportionate to the max values of the plots. I defer to others if there is an easier way of achieving this, however.

``````library(tidyverse)
library(patchwork) # let's me combine plots

# make data
dat = crossing(cat = c("a", "b", "c"),
val = 1:10) |>
filter(if_else(cat == "b" & val > 5, F, T),
if_else(cat == "c" & val > 3, F, T))

# could it be faceted?
ggplot(dat, aes(x = val)) +
geom_density() +
facet_grid( ~ cat, scales = "free_x", space = "free_x") +
scale_x_continuous(breaks = c(0, 2.5, 5, 7.5, 10), limits = c(0,NA))
`````` ``````# separate plots - fix aspect ratio?
ratios = dat |>
group_by(cat) |>
summarise(val = max(val)) |>
mutate(ratio = val / max(val))

myplot = function(dat, c){

dat = filter(dat, cat == c)

r = filter(ratios, cat == c) |> pull(ratio)

ggplot(dat, aes(x = val)) +
geom_density() +
theme(aspect.ratio = 1/r)

}

myplot(dat, "a")
`````` ``````myplot(dat, "b")
`````` ``````myplot(dat, "c")
`````` Created on 2022-01-19 by the reprex package (v2.0.1)

1 Like

yes, I think this is the solution.
Thank you so much sir. Have a nice day!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.