【问题标题】:displaying `shape` values in `ggplot2` plot annotations在“ggplot2”绘图注释中显示“shape”值
【发布时间】:2020-01-30 21:09:54
【问题描述】:

我想在绘图注释中显示形状值(geom_point 函数的shape 参数)。

例如,在下面,我只是写'point shape',我实际上希望显示形状本身。 (这是我想做的更复杂的事情的一个最小示例。)

有没有办法做到这一点?

library(ggplot2)

ggplot(mtcars, aes(mpg , wt)) + geom_point(shape = 7) +
  labs(title = "'point shape' represents data point")

reprex package (v0.3.0.9001) 于 2020 年 1 月 30 日创建

【问题讨论】:

  • 不知道我是否理解正确。所以像 ggplot(mtcars,aes(mpg,wt)) + geom_text(aes(label=cyl)) ?你在哪里显示 cyl 的值?
  • 一种可能性是添加 annotate("point", x, y, shape = 7) 之类的内容,然后添加另一个 annotate("text", x, y, label = "represents...", hjust = 0)xy 是您希望文本/符号出现的坐标(可能在情节之外如果需要也可以)

标签: r ggplot2


【解决方案1】:

您可以将标题创建为自定义注释,这可以通过将点和文本作为图形对象(“grobs”)粘贴在一起来完成。这里有一个小函数可以做到这一点,同时允许您设置点的形状和颜色以及标题文本。

library(grid)

make_title <- function(point_type = 7, label = "represents data point", col = "black")
{
  Point <- grid::pointsGrob(x = 0.0 , y = 1.05, pch = point_type, gp = gpar(col = col))
  Text <-  grid::textGrob(x = 0.025, y = 1.055, just = "left", label)
  Title <- grid::grobTree(Point, Text)
}

现在您只需将其作为自定义注释添加到您的绘图中。您必须关闭剪裁以允许在绘图区域之外进行绘图,并添加一个带有换行符的空白标题,以便为您的新绘图标题提供足够的空间:

library(ggplot2)

ggplot(mtcars, aes(mpg , wt))     + 
  labs(title = "\n ")             +
  geom_point(shape = 7)           +
  annotation_custom(make_title()) + 
  coord_cartesian(clip = "off")

或者如果你需要改变它:

ggplot(mtcars, aes(mpg , wt)) + 
  labs(title = "\n ") +
  geom_point(shape = 3, colour = "red")           +
  annotation_custom(make_title(3, "Different plot with red point", "red")) + 
  coord_cartesian(clip = "off")

【讨论】:

    【解决方案2】:

    我认为最好的办法是使用形状编号并使用键值对列表将其与相应的“点形状”链接起来。

    这是 R 中所有 25 个基本形状的 shape_label_key,您可以使用 ggpubr::show_point_shapes() 查看它们

    shape_label_key <- list(
        key = c(0:24),
        value = c(
            "square", "circle", "triangle point up", "plus", "cross", "diamond", "triangle point down",
            "square cross", "star", "diamond plus", "circle plus", "triangles up and down", "square plus",
            "circle cross", "square and triangle down", "filled square", "filled circle", "filled triangle point-up",
            "filled diamond", "solid circle", "bullet (smaller circle)", "filled circle blue", "filled square blue",
            "filled diamond blue", "filled triangle point-up blue", "filled triangle point down blue"
        )
    )
    ggplot(mtcars, aes(mpg , wt)) + geom_point(shape = 7) +
      labs(title = paste0("'", shape_label_key$value[shape_label_key$key == 7], "' represents data point"))
    

    希望这会有所帮助!

    【讨论】:

    • 为了简单和编写更好的代码,您必须将 shape_value 作为变量传递,而不是两次传递 7!
    • 将绘图保存到变量p1,然后使用:p1$layers[[1]]$aes_params$shape 获取变量,这样您就不必输入两次。这假定geom_point() 始终是第一层。
    【解决方案3】:

    您可以将color添加到aes()

    ggplot(data=mtcars, mapping = aes(x = mpg, y = wt, color=cyl), show.legend = TRUE) + 
    geom_point(shape=7) + 
    theme(legend.position = "top")
    

    【讨论】:

    • 我不想显示图例。我想在注释inside显示点形状。
    • 抱歉,问题误解了。
    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多