簡體   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