右侧刻面标签

问题描述 投票:0回答:1

所以,我尝试使用facet_wrap 创建一个绘图。 但是,我希望分面标签位于图的右侧,而不是位于图的顶部。

这是我的代码:

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)

这会生成: What I get

但我想要这样的东西: What I want

提前致谢!

r ggplot2 facet-wrap facet-grid
1个回答
0
投票

您可以通过设置

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
  )

© www.soinside.com 2019 - 2024. All rights reserved.