Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Facet labels on the right side

So, I’m trying to create a plot using facet_wrap.
However, I want the facet labels to be on the right side of the plot, not on top of it.

Here is my code:

library(ggplot2)

# Create the scatter plot with facets for Species
plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +  # Scatter plot with larger points
  facet_wrap(~ Species, ncol = 1) +  # Facet by Species in rows (to position labels on the right)
  labs(title = "Sepal Length vs Sepal Width by Species",
       x = "Sepal Length",
       y = "Sepal Width") +
  theme_minimal() +
  theme(
    legend.position = "none",
    strip.text.y = element_text(size = 12, angle = 0),  # Facet labels on the right side
    strip.placement = "outside",                         # Place facet labels outside the plot area
    strip.background = element_blank(),                  # Remove background for clarity
    panel.spacing = unit(1, "lines"),                    # Adjust spacing between panels
    axis.title.y = element_blank(),                      # Remove y-axis title for clarity
    axis.text.y = element_blank()                        # Remove y-axis text for clarity
  )

print(plot)

This generates:
What I get

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But I want something like this:
What I want

Thanks in advance!

>Solution :

You can reposition facet strips by setting strip.position = 'right'

library(ggplot2)


ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +  # Scatter plot with larger points
  facet_wrap(~ Species, ncol = 1,
             strip.position = 'right') +  # Facet by Species in rows (to position labels on the right)
  labs(title = "Sepal Length vs Sepal Width by Species",
       x = "Sepal Length",
       y = "Sepal Width") +
  theme_minimal() +
  theme(
    legend.position = "none",
    strip.text.y = element_text(size = 12, angle = 0),  # Facet labels on the right side
    strip.placement = "outside",                         # Place facet labels outside the plot area
    strip.background = element_blank(),                  # Remove background for clarity
    panel.spacing = unit(1, "lines"),                    # Adjust spacing between panels
    axis.title.y = element_blank(),                      # Remove y-axis title for clarity
    axis.text.y = element_blank()                        # Remove y-axis text for clarity
  )

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading