簡體   English   中英

R ggridges plot - 顯示 y 軸刻度和標簽

[英]R ggridges plot - Showing y axis ticks and labels

我正在嘗試隨時間生成疊加密度圖,比較男性與女性的密度。 這是我的 output: 在此處輸入圖像描述

我正在關注來自https://cran.r-project.org/web/packages/ggridges/vignettes/gallery.html的澳大利亞運動員身高示例。 這是我的代碼:

ggplot(math_dat, aes(x = order_math, y = time, color = gender, point_color = gender, fill = gender)) +
  geom_density_ridges(
    jittered_points = TRUE, scale = .95, rel_min_height = .01,
    point_shape = "|", point_size = 3, size = 0.25,
    position = position_points_jitter(height = 0)
  ) +
  scale_y_discrete(expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0), name = "Rankings") +
  scale_fill_manual(values = c("#D55E0050", "#0072B250"), labels = c("female", "male")) +
  scale_color_manual(values = c("#D55E00", "#0072B2"), guide = "none") +
  scale_discrete_manual("point_color", values = c("#D55E00", "#0072B2"), guide = "none") +
  coord_cartesian(clip = "off") +
  guides(fill = guide_legend(
    override.aes = list(
      fill = c("#D55E00A0", "#0072B2A0"),
      color = NA, point_color = NA)
  )
  ) +
  ggtitle("Ranks over time") +
  theme_ridges(center = TRUE)

我的問題是我無法生成任何 Y 軸刻度值,並且該示例也不顯示任何值。 任何想法如何讓 Y 軸刻度線顯示? 以下是一些類似於我的示例數據:

## generating dataset
order_math<-c(1,2,1,2,3,3,1,2,3,1,2,3)
gender<-c("M","F","M","M","M","F","F","M","F","M","M","F")
time<-c(1,1,2,3,3,2,1,2,3,2,3,1)
sample<-data.frame(order_math,gender,time)

更新:在@Tomasu 的建議之后,我更新了我的代碼,但它沒有運行:

ggplot(math_dat, aes(x = order_math, y = time, color = gender, point_color = gender, fill = gender)) +
  geom_density_ridges(
    jittered_points = TRUE, scale = .95, rel_min_height = .01,
    point_shape = "|", point_size = 3, size = 0.25,
    position = position_points_jitter(height = 0)
  ) +
  scale_y_reverse(limits = c(1000, 500, 100),expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0), name = "Rankings") +
  scale_fill_manual(values = c("#D55E0050", "#0072B250"), labels = c("female", "male")) +
  scale_color_manual(values = c("#D55E00", "#0072B2"), guide = "none") +
  scale_discrete_manual("point_color", values = c("#D55E00", "#0072B2"), guide = "none") +
  coord_cartesian(clip = "off") +
  guides(fill = guide_legend(
    override.aes = list(
      fill = c("#D55E00A0", "#0072B2A0"),
      color = NA, point_color = NA)
  )
  ) +
  ggtitle("Ranks over time") +
  theme_ridges(center = TRUE)+
  theme(
    axis.ticks = element_line(size=0.5),         # turn ticks back on
    axis.ticks.length = grid::unit(5, "pt"),     # set length
    axis.ticks.y = element_line(colour = "red"), # define tick line color
    axis.text.y = element_text(vjust = .4)       # center text with tick
  )

解決此問題的一個簡單方法是使用包含軸刻度的theme_ ,因為theme_ridges()已將它們關閉。 只需刪除該主題並使用基本 ggplot2 主題即可達到預期的結果。

但是,假設我們仍然想使用theme_ridges()並重新打開滴答聲。 這可以通過在theme_ridges()之后進行theme()編輯來實現。

我正在使用提供的鏈接中的示例,因為我無法讓您的示例數據正常工作。

library(ggplot2)
library(ggplot2movies)
library(ggridges)

ggplot(movies[movies$year>1912,], aes(x = length, y = year, group = year)) +
  geom_density_ridges(scale = 10, size = 0.25, rel_min_height = 0.03) +
  theme_ridges() +
  theme(
    axis.ticks = element_line(size=0.5),         # turn ticks back on
    axis.ticks.length = grid::unit(5, "pt"),     # set length
    axis.ticks.y = element_line(colour = "red"), # define tick line color
    axis.text.y = element_text(vjust = .4)       # center text with tick
  ) +
  scale_x_continuous(limits = c(1, 200), expand = c(0, 0)) +
  scale_y_reverse(
    breaks = c(2000, 1980, 1960, 1940, 1920, 1900),
    expand = c(0, 0)
  ) +
  coord_cartesian(clip = "off")

代表 package (v1.0.0) 於 2021 年 5 月 11 日創建

我認為您的問題是您需要指定組。

相關線程: geom_density_ridges 需要以下缺失的美學:y

擴展來自用戶 tomasu 的答案+1 的代碼

library(ggridges)
library(ggplot2)
order_math<-c(1,2,1,2,3,3,1,2,3,1,2,3)
gender<-c("M","F","M","M","M","F","F","M","F","M","M","F")
time<-c(1,1,2,3,3,2,1,2,3,2,3,1)
sample<-data.frame(order_math,gender,time)

ggplot(sample, aes(x = order_math, y = time, group = time,
                   color = gender, point_color = gender, fill = gender)) +
  geom_density_ridges()  +
  theme(
    axis.ticks = element_line(size=0.5),         # turn ticks back on
    axis.ticks.length = grid::unit(5, "pt"),     # set length
    axis.ticks.y = element_line(colour = "red"), # define tick line color
    axis.text.y = element_text(vjust = .4)       # center text with tick
  )
#> Picking joint bandwidth of 0.555

代表 package (v2.0.0) 於 2021 年 5 月 12 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM