<p>You have two different types of values you want to display on the x-axis: <code>Cluster</code> (a categorical variable) and <code>Count</code> (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.</p>
<p>I'm guessing based on your data that you're trying to create something that looks like this figure from <a href="https://pubmed.ncbi.nlm.nih.gov/23945592/" rel="nofollow noreferrer">Alexandrov <em>et al.</em>, Nature, 2013</a>:</p>
<p><a href="https://i.sstatic.net/HcYWE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/HcYWE.png" alt="Mutation distribution plot from Alexandrov et al., Nature, 2013" /></a></p>
<p>You can accomplish this by customizing the facet and the theme.
Here's an example.</p>
<pre><code>gg &lt;- (
  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, &quot;lines&quot;),
    # this removes the border around facets
    panel.border = element_blank()
  )
)
</code></pre>
<p>There are more parts of the theme you can adjust to your liking, like the facet header (e.g. <code>strip.text</code>).
See <a href="https://ggplot2.tidyverse.org/reference/theme.html" rel="nofollow noreferrer">ggplot2's reference documentation</a> for details.</p>

CC BY-SA 4.0
2022-06-17T17:22:22.730Z
2022-06-17T17:22:22.730Z
James Hawley
<p>I am trying to plot one scatter plot for each group, where each group has continous values in x-axis.
<a href="https://drive.google.com/file/d/12wCp_rMuwzjJPmtPAYP3JmuUgwjVe-nY/view?usp=sharing" rel="nofollow noreferrer">Reference Data</a></p>
<p>Gives continuous x-axis but all groups are combined.</p>
<pre><code>ggplot(data_for_plotting, 
aes(x = log10(Count), y = log10(Nonsilent.Mutation.Rate), color=factor(Clusters))) + 
geom_point(alpha = 1, size = 1) + theme_bw()
</code></pre>
<p><a href="https://i.sstatic.net/ffQse.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ffQse.png" alt="enter image description here" /></a></p>
<p>Gives grouped x-axis but the variation in x-axis is lost.</p>
<pre><code>ggplot(data_for_plotting, 
aes(x = factor(Clusters), y = log10(Nonsilent.Mutation.Rate), color=factor(Clusters))) + 
geom_point(alpha = 1, size = 1) + theme_bw()
</code></pre>
<p><a href="https://i.sstatic.net/TmOJL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TmOJL.png" alt="enter image description here" /></a></p>
<p>Does achieve the grouping but the gives multiple plots.</p>
<pre><code>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()
</code></pre>
<p><a href="https://i.sstatic.net/nWr6C.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nWr6C.png" alt="enter image description here" /></a></p>
<p>I want x-axis to be grouped based on Cluster where each cluster has continous x-axis values.</p>

CC BY-SA 4.0
2022-04-11T16:40:41.030Z
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.
Ram RS
CC BY-SA 4.0
2022-04-12T16:57:45.177Z
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.
Maximilian Press
CC BY-SA 4.0
2022-04-12T17:14:22.583Z
@MaximilianPress it's not just you. I'm also having trouble understanding the question.
acvill
CC BY-SA 4.0
2022-04-12T17:52:05.740Z
Possibly draw a cartoon of what you would like to accomplish?
Maximilian Press
<r><visualization><ggplot2>
Grouping x-axis in scatter plot
# Grouping x-axis in scatter plot

(Asked by: username_0 on 2022-04-11)

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()


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()


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()


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

**Tags:** r, visualization, ggplot2
> **Comments:**
> **username_1**: 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.  
> **username_2**: 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.  
> **username_3**: @MaximilianPress it's not just you. I'm also having trouble understanding the question.  
> **username_2**: Possibly draw a cartoon of what you would like to accomplish?  

---

## 1 answers

### Answer by username_4 on 2022-06-17. Score: 1 | Accepted answer

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:

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.


---

