【问题标题】:Add markings/labels to Ridgeline plot [R / ggplot2]添加标记/标签到 Ridgeline 图 [R / ggplot2]
【发布时间】:2020-06-25 20:30:21
【问题描述】:

我制作了一个山脊线图,但我想为其添加自定义标记(弹出我添加的代表)。我能想到的最好的方法是添加标签。这并不理想。

我找到了this 链接,这更符合我的要求。但我无法让该示例适用于我的案例(使用 fill = 绘制因子水平)。

下面是一些我能想到的最好的输出。

谢谢。


library(tidyverse)
library(ggridges)

dfs <-
  data.frame(
    "estimate" = rnorm(300),
    "loading" = factor(rep(1:5, 60)),
    "set" = factor(rep(1:3, 100),),
    "pop" = rep(c(0.1, 0.15, 0.20, 0.05, 0.7), 60)
  )

ggplot(dfs, aes(x = estimate, y = loading, fill = set)) +
  geom_density_ridges(
    jittered_points = TRUE,
    point_shape = "|", point_size = 2, size = 0.25,
    position = position_points_jitter(height = 0), alpha = 0.6, scale = 1.2) 
#> Picking joint bandwidth of 0.395


loadsummary2 <- crossing("set" =  dfs$set, "loading" = dfs$loading)
loadsummary2$pop <- rep(c(0.1, 0.15, 0.20, 0.05, 0.7), 3)
  
ggplot(dfs, aes(x = estimate, y = loading, fill = set)) +
  geom_density_ridges(
    jittered_points = TRUE,
    point_shape = "|", point_size = 2, size = 0.25,
    position = position_points_jitter(height = 0), alpha = 0.6, scale = 1.2) + 
    geom_label(data = loadsummary2, aes(y = loading, x = pop), label = "*")
#> Picking joint bandwidth of 0.395

reprex package (v0.3.0) 于 2020-03-13 创建

【问题讨论】:

标签: r ggplot2 data-visualization ggridges ridgeline-plot


【解决方案1】:

我在 RStudio 社区页面的某个人的帮助下找到了解决方案!


library(tidyverse)
library(ggridges)

dfs <-
  data.frame(
    "estimate" = rnorm(300),
    "loading" = factor(rep(1:5, 60)),
    "set" = factor(rep(1:3, 100),),
    "pop" = rep(c(0.1, 0.15, 0.20, 0.05, 0.7), 60)
  )


loadsummary2 <- crossing("set" =  dfs$set, "loading" = dfs$loading)
loadsummary2$pop <- rep(c(0.1, 0.15, 0.20, 0.05, 0.7), 3)

p <- ggplot(dfs, aes(x = estimate, y = loading, fill = set)) +
  geom_density_ridges(
    jittered_points = TRUE,
    point_shape = "|", point_size = 2, size = 0.25,
    position = position_points_jitter(height = 0), alpha = 0.6, scale = 1.2) + 
    geom_text(data = loadsummary2, 
              aes(y = loading, x = pop, label = pop),
              position=position_nudge(y= .25), 
              colour="black", 
              size=3.5)

p + geom_segment(aes(
  x = pop,
  y = as.numeric(loading) - .05,
  xend = pop,
  yend = as.numeric(loading) + .15))
#> Picking joint bandwidth of 0.437

reprex package (v0.3.0) 于 2020 年 3 月 14 日创建

【讨论】:

    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-10-11
    • 2022-08-13
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多