ggplot2绘图R小白相关

R可视化:拼图代码暂存

2024-03-10  本文已影响0人  生信学习者2
library(ggplot2)
library(patchwork)

# Setup common theme elements for all the plots
theme_set(
  theme_bw() +
    theme(
      plot.subtitle = element_text(face = "bold")
    )
)

#------------------------------------------------------------------------------*
# Plot 1
#------------------------------------------------------------------------------*
ggplot(mtcars) +
  geom_point( aes(disp, mpg, color = factor(cyl)), show.legend = FALSE ) +
  labs(
    title = paste(
      "Figure 1: Relations between fuel displacement,",
      "horsepower and fuel efficiency."
    ),
    subtitle = "A", caption = "Some inconspicuous text."
  ) +
  theme(plot.caption = element_text(hjust = 0)) +
  { # <--- nested plots
    #--------------------------------------------------------------------------*
    # Plot 2
    #--------------------------------------------------------------------------*
    ggplot(mtcars) +
      geom_point(aes(disp, hp, color = factor(cyl))) +
      # Individual plot annotations
      labs(subtitle = "B") +
      # Spacing
      patchwork::plot_spacer() +
      { # <--- nested plots
        #----------------------------------------------------------------------*
        # Plot 3
        #----------------------------------------------------------------------*
        ggplot(mtcars) +
          geom_point( aes(hp, mpg, color = factor(cyl)), show.legend = FALSE ) +
          # Individual plot annotations
          labs(subtitle = "C")
      } +
      patchwork::plot_layout(ncol = 1)
  } +
  patchwork::plot_layout(ncol = 2, widths = c(2.2, 1))
上一篇 下一篇

猜你喜欢

热点阅读