0

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

我找到了这个链接,这更符合我的要求。但我无法让该示例适用于我的案例(使用 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 包(v0.3.0)于 2020 年 3 月 13 日创建

4

1 回答 1

0

我在 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 包(v0.3.0)于 2020-03-14 创建

于 2020-03-14T12:32:05.960 回答