rggplot2facet-wrapfacet-grid

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

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
      )