MAPK MAPK - 2 months ago 8
R Question

How to add missing x-axis in multi faceted plot in R

I have this dataframe called

cpp
. I have plotted the figure below which is missing X-axis in two of the plots in the right. How can I put the x-axis for these two plots? Additionally, I would also like to increase the size of each box,so the lines would not touch the margin on the right.

ggplot(cpp, aes(x = Num_Good, y = IBS, group = key.related.sheet, color = cutoff)) +
geom_line() + facet_wrap(~cutoff) + geom_point()


enter image description here

Answer

Check this answer about the "dangling" facets: add "floating" axis labels in facet_wrap plot

Or as a quick workaround (if it is ok to add x axis to all facets), you can set the parameter scales = "free_x" of facet_wrap.

Regarding the second part of your question, try to set the second value of the expand parameter in scale_x_continuous, e.g.:

ggplot(cpp, aes(x = Num_Good, y = IBS, group = key.related.sheet, color = cutoff))+
  geom_line() + 
  facet_wrap(~cutoff, scales = "free_x") + 
  geom_point() +
  scale_x_continuous(expand = c(0, .1))