# Grouping x-axis in scatter plot

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.

• 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. Apr 11, 2022 at 16:40
• 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. Apr 12, 2022 at 16:57
• @MaximilianPress it's not just you. I'm also having trouble understanding the question. Apr 12, 2022 at 17:14
• Possibly draw a cartoon of what you would like to accomplish? Apr 12, 2022 at 17:52

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.