【问题标题】:Adding Annotations to coord_polar向 coord_polar 添加注释
【发布时间】:2020-02-10 18:17:26
【问题描述】:

我想在带有极坐标的绘图的左下角放置注释。

有没有办法通过使用不属于圆形坐标系的坐标(如图所示,这不允许我将它们放置在很远的地方)而是使用普通的 x 和 y 系统?

我不需要显示坐标轴,但我将它们留在里面以帮助显示正在发生的事情。

library(tidyverse)
iris2 <- iris %>%
  mutate(id = row_number())
ggplot(iris2) +      
  theme_minimal() +
  theme(
    legend.position = "none",
    axis.title = element_blank()
  ) +
  coord_polar() +
  geom_segment(aes(x = id, xend = id, 
                   y = 10, 
                   yend = 10 + Sepal.Length),
               size = 1, inherit.aes = FALSE) +
  annotate(geom="text", x = 90, y = 10, label="Annotation",
           color="Red") +
  annotate(geom="text", x = 90, y = 15, label="Annotation",
           color="Blue")

【问题讨论】:

标签: r ggplot2


【解决方案1】:

您可以使用cowplot 包来制作所需的情节。您可以制作两个 text_grob 作为文本标签,然后将它们添加到您已有的绘图的左下角(在以下代码中显示为 p1)。

library(cowplot)

#X axis text
x <- textGrob(label = "X text",
              gp=gpar(fontsize=12,
                      fontface = "bold"),
              hjust = 0.1)
#Y axis text
y <- textGrob(label = "Y text",
              #rotate it 90 degrees
              rot = 90,
              gp=gpar(fontsize=12,
                      fontface = "bold"))

#Get X axis text and p1 together in a 2 row array
p2<-plot_grid(p1,x, 
              nrow = 2,
              rel_widths = c(4,1),
              rel_heights = c(20,1))

#Get Y axis text and p2 together in 2 column array
p3<-plot_grid(y,p2, 
              ncol = 2,
              rel_widths = c(1,20),
              rel_heights = c(1,4))
p3

【讨论】:

    【解决方案2】:

    基于@Aexman 对尝试cowplot 的有用评论,我做了并想出了以下内容。这意味着您可以将两个图层添加在一起,第二个图层具有独立的坐标系。

    两个轴的坐标都在 0 和 1 之间,相对于左下角。也可以使用绝对定位。

    library(tidyverse)
    library(cowplot)
    
    iris2 <- iris %>%
      mutate(id = row_number())
    
    g <- ggplot(iris2) +      
      theme_minimal() +
      theme(
        legend.position = "none",
        axis.title = element_blank()
      ) +
      coord_polar() +
      geom_segment(aes(x = id, xend = id, 
                       y = 10, 
                       yend = 10 + Sepal.Length),
                   size = 1, inherit.aes = FALSE)
    
    ggdraw(g) +
      draw_label("Annotation", x = 0.05, y = 0.05, hjust = 0, vjust = 0,
                 fontfamily = "", fontface = "plain", color = "black", size = 14,
                 colour = "Red")
    

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多