这个问题与这extend axis to 0 with geom_density_ridges2有关.

这里,OP要求将x轴扩展到现有比例来显示0、1、2. 而@Stefan发布的正确答案是limits = c(-.5, 9.5)分, 我try 了另一种方法,将缺少的0,1,2作为新行添加到dfvalue列,并将此行添加到代码complete(value = seq(0, max(value), 1)),结果如下:

library(dplyr)
library(tidyr)
library(ggplot2)
library(ggridges)

df %>%
  complete(value = seq(0, max(value), 1)) %>%
  ggplot(aes(x = value, y = item, group = item)) +
  geom_density_ridges2(aes(fill = item), 
                       stat = "binline", 
                       binwidth = 1, 
                       scale = 0.95) +
  geom_text(
    stat = "bin",
    aes(
      y = group + 0.95*stat(count/max(count)),
      label = ifelse(stat(count) > 0, stat(count), "")
    ),
    vjust = -0.5, size = 3, color = "black", binwidth = 1
  ) +
  scale_x_continuous(
    breaks = 0:9, 
    name = "Answer (higher = better)"
  ) +
  scale_y_discrete(
    expand = expansion(add = 2)
  ) +
  scale_fill_viridis_d() +
  labs(
    title = "Example",
    y = NULL
  ) +
  theme_ridges(grid = FALSE) +
  theme(
    axis.title.x = element_text(hjust = 0.5),
    axis.title.y = element_text(hjust = 0.5),
    legend.position = "none",
    plot.title.position = "plot",
    plot.title = element_text(face="bold")
  )

enter image description here

How can I remove the NA level while keeping the values for the x axis in the plot above?

Desired output: enter image description here

数据:

df <- df <- structure(list(id = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 
7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 
16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 
24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29), item = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"), class = "factor"), 
    value = c(8, 9, 4, 5, 8, 9, 7, 9, 8, 8, 7, 6, 9, 8, 9, 9, 
    9, 9, 8, 9, 9, 9, 8, 9, 8, 8, 7, 8, 8, 8, 7, 9, 8, 9, 8, 
    9, 9, 9, 9, 9, 8, 9, 9, 9, 9, 7, 9, 9, 8, 9, 8, 9, 7, 9, 
    7, 7, 7, 8)), row.names = c(NA, -58L), class = c("tbl_df", 
"tbl", "data.frame"))

推荐答案

您可以删除NA类别,方法是将scale_Y_discrete中的limits设置为仅包括所需的"真实"类别:

library(dplyr)
library(tidyr)
library(ggplot2)
library(ggridges)

df %>%
  complete(value = 0:9) %>%
  ggplot(aes(x = value, y = item, group = item)) +
  geom_ridgeline(
    aes(fill = item),
    stat = "binline",
    binwidth = 1,
    scale = 0.95
  ) +
  geom_text(
    stat = "bin",
    aes(
      y = group + 0.95 * stat(count / max(count)),
      label = ifelse(stat(count) > 0, stat(count), "")
    ),
    vjust = -0.5, size = 3, color = "black", binwidth = 1
  ) +
  scale_x_continuous(
    breaks = 0:9,
    name = "Answer (higher = better)",
    expand = c(0, 0)
  ) +
  scale_y_discrete(
    limits = c("A", "B"),
    expand = expansion(add = 1)
  ) +
  scale_fill_viridis_d() +
  labs(
    title = "Example",
    y = NULL
  ) +
  theme_ridges(grid = FALSE) +
  theme(
    axis.title.x = element_text(hjust = 0.5),
    axis.title.y = element_text(hjust = 0.5),
    legend.position = "none",
    plot.title.position = "plot",
    plot.title = element_text(face = "bold")
  )

enter image description here

R相关问答推荐

在ComplexHeatmap中,如何更改anno_barplot()标题的Angular ?

高质量地将R格式的图表从Word中输出

基于现有类创建类的打印方法(即,打印tibles更长时间)

如何求解arg必须为NULL或deSolve包的ode函数中的字符向量错误

当月份额减go 当月份额

如果可能,将数字列转换为整数,否则保留为数字

DEN扩展包中的RECT树形图出现异常行为

跨列查找多个时间报告

是否有新方法来更改Facet_WRAP(Ggplot2)中条文本的文本 colored颜色 ?

R+reprex:在呈现R标记文件时创建可重现的示例

如何将一列中的值拆分到R中各自的列中

WRS2包中带有bwtrim的简单ANOVA抛出错误

使用geom_iles在一个切片中包含多个值

使用ifElse语句在ggploy中设置aes y值

如何将图例文本添加到图例符号中

如何为包创建自定义roxygen2标签?

R dplyr::带有名称注入(LHS of:=)的函数,稍后在:=的RHS上引用

如何捕获这个shiny 的、可扩展的react 性用户输入矩阵作为另一个react 性对象,以便进一步操作?

创建由三个单独的shapefile组成的单个 map

Ggplot2水平线和垂直线的图例图标不匹配