R语言 是否将小平面标签Y轴标签添加到非小平面图?

0kjbasz6  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(126)

我想将出现在右侧y轴上的面标签添加到不是面的ggplot。有没有一个简单的方法来做到这一点?Plotv2显示了我想要添加的y轴标签,但实际上我并不想将数据拆分到两个方面。是否有设置的主题参数,可以用来添加第二个y轴标题容易,或者我必须去使用自定义grob?

  1. library(ggplot2)
  2. library(grid)
  3. df <- data.frame(
  4. Position=(c(1:5000)),
  5. Signal=(c((rep(c(5),times=2000)), (rep(c(100),times=1000)), (rep(c(5),times=2000))))
  6. )
  7. Plotv1 <- ggplot() +
  8. geom_line(data = df, aes(x=Position, y=Signal, col = "#000000")) +
  9. coord_cartesian(clip="off") +
  10. guides(fill = "none") +
  11. guides(color=guide_legend(title="MyLegend")) +
  12. theme_bw() +
  13. theme(axis.text.x = element_blank()) +
  14. theme(
  15. axis.title.x = element_text(margin=margin(t=30)),
  16. legend.title = element_text(colour = "#000000", size=12),
  17. legend.text = element_text(colour = "#000000", size=12)
  18. ) +
  19. annotation_custom(
  20. grid::segmentsGrob(
  21. y0 = unit(-25, "pt"),
  22. y1 = unit(-25, "pt"),
  23. arrow = arrow(angle=45, type="closed", length=unit(.15, 'cm')),
  24. gp = grid::gpar(lwd=3, col="#000000", fill="#000000")
  25. ),
  26. xmin = 2000,
  27. xmax = 3000
  28. )
  29. ggsave(paste("~/Desktop/Plotv1.png", sep = ""), Plotv1, width = 8, height = 1.7)
  30. Plotv2 <- Plotv1 +
  31. facet_grid(Signal~.)
  32. ggsave(paste("~/Desktop/Plotv2.png", sep = ""), Plotv2, width = 8, height = 1.7)

s71maibg

s71maibg1#

您可以考虑添加一个冗余的facet标签,或者使用辅助轴在右侧添加其他标题:

  1. ggplot(mtcars, aes(wt, mpg)) +
  2. geom_point() +
  3. facet_grid("Second Y-Axis Title"~.) +
  4. scale_y_continuous(sec.axis = sec_axis(trans = ~., breaks = NULL,
  5. name = "Third Y-Axis Title"))

相关问题