3
$\begingroup$

I am trying to plot one scatter plot for each group, where each group has continous values in x-axis. Reference Data

Gives continuous x-axis but all groups are combined.

ggplot(data_for_plotting, 
aes(x = log10(Count), y = log10(Nonsilent.Mutation.Rate), color=factor(Clusters))) + 
geom_point(alpha = 1, size = 1) + theme_bw()

enter image description here

Gives grouped x-axis but the variation in x-axis is lost.

ggplot(data_for_plotting, 
aes(x = factor(Clusters), y = log10(Nonsilent.Mutation.Rate), color=factor(Clusters))) + 
geom_point(alpha = 1, size = 1) + theme_bw()

enter image description here

Does achieve the grouping but the gives multiple plots.

ggplot(data_for_plotting, 
aes(x = log10(Count),y = log10(Nonsilent.Mutation.Rate),color=factor(Clusters))) +
facet_wrap(~ factor(Clusters), shrink = TRUE) +
geom_point(alpha = 1, size = 1) + theme_bw()

enter image description here

I want x-axis to be grouped based on Cluster where each cluster has continous x-axis values.

$\endgroup$
4
  • 2
    $\begingroup$ You are asking for repeating values in the x-axis. That's the only way you'll maintain a continuous x axis while seeing separate lines for each Cluster. The facet is the right solution - they're not separate plots, they're multiple facets of the same plot. $\endgroup$
    – Ram RS
    Apr 11, 2022 at 16:40
  • 1
    $\begingroup$ I'm not sure what you're asking for. The first and the third both seem acceptable from a generic visualization point of view. "Gives multiple plots" seems to be what you want based on the prompt? Ram seems to understand you, maybe it's just me. $\endgroup$ Apr 12, 2022 at 16:57
  • 1
    $\begingroup$ @MaximilianPress it's not just you. I'm also having trouble understanding the question. $\endgroup$
    – acvill
    Apr 12, 2022 at 17:14
  • $\begingroup$ Possibly draw a cartoon of what you would like to accomplish? $\endgroup$ Apr 12, 2022 at 17:52

1 Answer 1

1
$\begingroup$

You have two different types of values you want to display on the x-axis: Cluster (a categorical variable) and Count (a continuous variable). As Ram RS suggested, the easiest way to accomplish this is by taking your first plot (which uses a continuous x-axis scale) and facet along your discrete variable. This gets you what you want, and the rest of the work is adjusting the theme to make it look how you want.

I'm guessing based on your data that you're trying to create something that looks like this figure from Alexandrov et al., Nature, 2013:

Mutation distribution plot from Alexandrov et al., Nature, 2013

You can accomplish this by customizing the facet and the theme. Here's an example.

gg <- (
  ggplot(
    data = data_for_plotting, 
    mapping = aes(
      x = log10(Count),
      y = log10(Nonsilent.Mutation.Rate),
      color = factor(Clusters)
    )
  )
  + geom_point(alpha = 1, size = 1)
  + facet_wrap(
    ~ Cluster,
    # this ensures that all facets are shown on the same row instead of wrapping
    nrow = 1
  )
  + theme_bw()
  + theme(
    # this removes the spacing between facets
    panel.spacing = unit(0, "lines"),
    # this removes the border around facets
    panel.border = element_blank()
  )
)

There are more parts of the theme you can adjust to your liking, like the facet header (e.g. strip.text). See ggplot2's reference documentation for details.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.