繁体   English   中英

R 绘图类型“b”,带有文本而不是点 - 带有 ggplot2 的斜率图

[英]R plot type "b" with text instead of points - Slope graph with ggplot2

ggplot2 中有没有办法获得绘图类型“b”? 见示例:

x <- c(1:5)
y <- x 
plot(x,y,type="b")

理想情况下,我想用它们的值替换点,以获得类似于这个著名示例的内容:

替代文字

编辑:这里有一些示例数据(我想用绘图类型“b”在一个方面绘制每个“猫”):

df <- data.frame(x=rep(1:5,9),y=c(0.02,0.04,0.07,0.09,0.11,0.13,0.16,0.18,0.2,0.22,0.24,0.27,0.29,0.31,0.33,0.36,0.38,0.4,0.42,0.44,0.47,0.49,0.51,0.53,0.56,0.58,0.6,0.62,0.64,0.67,0.69,0.71,0.73,0.76,0.78,0.8,0.82,0.84,0.87,0.89,0.91,0.93,0.96,0.98,1),cat=rep(paste("a",1:9,sep=""),each=5))

通过绘制没有任何内容的图来设置轴。

plot(x, y, type = "n")

然后使用text来制作数据点。

text(x, y, labels = y)

您可以使用lines添加线段。

lines(x, y, col = "grey80")

编辑:完全没有在问题中提到 ggplot 。 试试这个。

dfr <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(dfr, aes(x, y)) + 
  geom_text(aes(x, y, label = y)) + 
  geom_line(col = "grey80")
p

另一个编辑:鉴于您的新数据集和请求,这就是您所需要的。

ggplot(df, aes(x, y)) + geom_point() + geom_line() + facet_wrap(~cat)

另一个编辑:我们开始处理一个真正的问题。 就像“如何使线条不完全达到点”一样。

简短的回答是,这不是在 ggplot2 中执行此操作的标准方法。 正确的方法是使用geom_segment并在数据点之间进行插值。 然而,这是相当多的努力,所以我建议一个更简单的软糖:在你的点周围画大的白色圆圈。 这样做的缺点是它使网格线看起来很傻,所以你必须摆脱它们。

ggplot(df, aes(x, y)) + 
   facet_wrap(~cat) +
   geom_line() + 
   geom_point(size = 5, colour = "white") + 
   geom_point() + 
   opts(panel.background = theme_blank())

gridExtra 中有一个实验性的 grob 可以在网格图形中实现这一点,

 library(gridExtra)
 grid.newpage() ; grid.barbed(pch=5)

在此处输入图片说明

用户 teunbrand 创建了一个使这成为可能的 geom 我只是稍微修改了它,以便通过设置size = 0来绘制“无点”

library(ggplot2)
library(grid) # You usually won't need this, but reprex requires it

## create geom as per below

df <- data.frame(x = rep(1:5, 9), y = c(0.02, 0.04, 0.07, 0.09, 0.11, 0.13, 0.16, 0.18, 0.2, 0.22, 0.24, 0.27, 0.29, 0.31, 0.33, 0.36, 0.38, 0.4, 0.42, 0.44, 0.47, 0.49, 0.51, 0.53, 0.56, 0.58, 0.6, 0.62, 0.64, 0.67, 0.69, 0.71, 0.73, 0.76, 0.78, 0.8, 0.82, 0.84, 0.87, 0.89, 0.91, 0.93, 0.96, 0.98, 1), cat = rep(paste("a", 1:9, sep = ""), each = 5))

ggplot(df, aes(x, y)) +
  geom_text(aes(label = cat)) +
  geom_trail(size = 0)

reprex 包(v0.3.0) 于 2020 年 5 月 15 日创建

geom_trail

GeomTrail <- ggplot2::ggproto(
  "GeomTrail", ggplot2::GeomPoint,
  draw_panel = function(data, panel_params, coord, na.rm = FALSE) {

    # Default geom point behaviour
    if (is.character(data$shape)) {
      data$shape <- translate_shape_string(data$shape)
    }
    coords <- coord$transform(data, panel_params)

    if (unique(coords$size == 0)) {
      my_points <- NULL
    } else {
      my_points <- pointsGrob(
        coords$x,
        coords$y,
        pch = coords$shape,
        gp = gpar(
          col = alpha(coords$colour, coords$alpha),
          fill = alpha(coords$fill, coords$alpha),
          fontsize = coords$size * .pt + coords$stroke * .stroke / 2,
          lwd = coords$stroke * .stroke / 2
        )
      )
    }
    # New behaviour
    ## Convert x and y to units
    x <- unit(coords$x, "npc")
    y <- unit(coords$y, "npc")

    ## Make custom grob class
    my_path <- grob(
      x = x,
      y = y,
      mult = coords$gap * .pt,
      name = "trail",
      gp = grid::gpar(
        col = alpha(coords$colour, coords$alpha),
        fill = alpha(coords$colour, coords$alpha),
        lwd = coords$linesize * .pt,
        lty = coords$linetype,
        lineend = "butt",
        linejoin = "round", linemitre = 10
      ),
      vp = NULL,
      ### Now this is the important bit:
      cl = "trail"
    )

    ## Combine grobs
    ggplot2:::ggname(
      "geom_trail",
      grid::grobTree(my_path, my_points)
    )
  },
  # Adding some defaults for lines and gap
  default_aes = aes(
    shape = 19, colour = "black", size = 1.5, fill = NA, alpha = NA, stroke = 0.5,
    linesize = 0.5, linetype = 1, gap = .9,
  )
)

makeContent.trail <- function(x) {
  # Make hook for drawing
  # Convert npcs to absolute units
  x_new <- convertX(x$x, "mm", TRUE)
  y_new <- convertY(x$y, "mm", TRUE)

  # Do trigonometry stuff
  hyp <- sqrt(diff(x_new)^2 + diff(y_new)^2)
  sin_plot <- diff(y_new) / hyp
  cos_plot <- diff(x_new) / hyp

  diff_x0_seg <- head(x$mult, -1) * cos_plot
  diff_x1_seg <- (hyp - head(x$mult, -1)) * cos_plot
  diff_y0_seg <- head(x$mult, -1) * sin_plot
  diff_y1_seg <- (hyp - head(x$mult, -1)) * sin_plot

  x0 <- head(x_new, -1) + diff_x0_seg
  x1 <- head(x_new, -1) + diff_x1_seg
  y0 <- head(y_new, -1) + diff_y0_seg
  y1 <- head(y_new, -1) + diff_y1_seg
  keep <- unclass(x0) < unclass(x1)

  # Remove old xy coordinates
  x$x <- NULL
  x$y <- NULL

  # Supply new xy coordinates
  x$x0 <- unit(x0, "mm")[keep]
  x$x1 <- unit(x1, "mm")[keep]
  x$y0 <- unit(y0, "mm")[keep]
  x$y1 <- unit(y1, "mm")[keep]

  # Set to segments class
  class(x)[1] <- "segments"
  x
}

geom_trail <-
  function(mapping = NULL, data = NULL, stat = "identity",
           position = "identity", na.rm = FALSE,
           show.legend = NA, inherit.aes = TRUE, ...) {
    layer(
      data = data, mapping = mapping, stat = stat,
      geom = GeomTrail, position = position, show.legend = show.legend,
      inherit.aes = inherit.aes,
      params = list(na.rm = na.rm, ...)
    )
  }

另一种制作斜率图的方法是使用CGPfunctions包。

library(CGPfunctions)
newggslopegraph(newcancer, Year, Survival, Type)

您也有很多选择。 你可以在这里找到一个很好的教程:

https://www.r-bloggers.com/2018/06/creating-slopegraphs-with-r/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM