在 R 中自定义 GGridges 图

问题描述 投票:0回答:1

我想创建一个带有一些自定义的 ggridges 风格的情节。

我有一个包含三个变量的数据集:

  • 类似 Likert 的响应变量(0:6 响应),称为“RankVar”
  • 称为“Node”的节点变量 (1:9)(这从自组织映射中捕获节点分配)。
  • 和一个称为“Clus”的簇变量 (A:C)

在这方面,响应变量的特征是节点ID和集群ID。例如,集群 A 中的 RankVar 响应的特征是 9 个节点中的节点 ID = 1-4-5。

就定制的ggridges图而言,我想:

  • 根据簇标签堆叠山脊。因此,集群 A 应该有节点 1-4-5,并且 x 轴上有响应变量。

  • 我还想在每个簇之间有一个空白区域。因此,在具有节点 1-4-5 的集群 A 之后,我希望在集群 B 开始节点 2-3-7 的位置之前有一个空格符(大约 binwidth)。

  • 我还希望每个簇都用自己的颜色进行索引。因此,节点 1-4-5 在图上的颜色与节点 2-3-7 不同。

我已附上我的视觉效果(未经自定义)和生成它的脚本。我们将不胜感激所有帮助。

提前谢谢您

##################################
#MY RIDGES SO FAR 
###################################

library(tidyverse)
library(ggplot2)
library(ggridges)
library(hrbrthemes)

#######################
#Creating the Ranked Response Variable 
#########################

RankVar = rep(0:5, times = 108) 
RankVar <- sample(RankVar)
RankVar <- as.factor(RankVar)

###############################################
# CREATING NODE ID TO BE ASSIGNED TO EACH OF THE 9 NODES
################################################

Node = rep(1:9, times = 72)
Node <- sample(Node)
Node <- as.factor(Node)

###############################################
#Creating Cluster ID
###############################################

Clus <- rep(LETTERS[1:3], times = 216)
Clus <- noquote(Clus)
Clus

###################
#DATA FRAME
####################

DAT <- data.frame(RankVar, Node)

##############################
#CLUSTER ASSIGNMENTS
##############################

DAT %>% 
  mutate(CLUS = case_when(Node == 1 | Node == 4 | Node == 5 ~ 'A', 
                         Node == 2 | Node == 3 | Node == 7 ~ 'B', 
                         Node == 6 | Node == 8 | Node == 9 ~ 'C'))

#######################################
#VISUALISATION WITHOUT REQUIRED CUSTOMISATION
########################################

ggplot(DAT, aes(x = RankVar, y = Node, fill = Node, group = Node)) +
  geom_density_ridges(alpha = .7, stat = "binline", bins = 30, scale = 0.9) +
  theme_ipsum(axis_title_size = 10) + 
  theme(plot.title = element_text(hjust = 0.5, size = 12, face = "bold"),
        axis.text = element_text(size = 11), legend.position = "none") +
  labs(title = '') +
  scale_x_discrete() +
  scale_fill_manual(values = c("#08519C","#08519C", "#08519C", 
                               "#08519C", "#08519C","#08519C", 
                               "#08519C", "#08519C","#08519C" ))

r ggplot2 visualization
1个回答
0
投票

这是您要找的吗?如果是这样,您可以通过在

CLUS
上进行刻面并在美学中使用
fill=CLUS
来完成大部分所需的操作。面板之间的间距由
panel.spacing
中的
theme()
参数控制。

library(tidyverse)
library(ggplot2)
library(ggridges)
library(hrbrthemes)
#######################
#Creating the Ranked Response Variable 
#########################

RankVar = rep(0:5, times = 108) 
RankVar <- sample(RankVar)
RankVar <- as.factor(RankVar)

###############################################
# CREATING NODE ID TO BE ASSIGNED TO EACH OF THE 9 NODES
################################################

Node = rep(1:9, times = 72)
Node <- sample(Node)
Node <- as.factor(Node)

###############################################
#Creating Cluster ID
###############################################

Clus <- rep(LETTERS[1:3], times = 216)
Clus <- noquote(Clus)


###################
#DATA FRAME
####################

DAT <- data.frame(RankVar, Node)

##############################
#CLUSTER ASSIGNMENTS
##############################

DAT <- DAT %>% 
  mutate(CLUS = case_when(Node == 1 | Node == 4 | Node == 5 ~ 'A', 
                          Node == 2 | Node == 3 | Node == 7 ~ 'B', 
                          Node == 6 | Node == 8 | Node == 9 ~ 'C'))

#######################################
#VISUALISATION WITHOUT REQUIRED CUSTOMISATION
########################################

ggplot(DAT, aes(x = RankVar, y = Node, fill = CLUS, group = Node)) +
  geom_density_ridges(alpha = .7, stat = "binline", bins = 30, scale = 0.9) +
  theme_ipsum(axis_title_size = 10) + 
  theme(plot.title = element_text(hjust = 0.5, size = 12, face = "bold"),
        axis.text = element_text(size = 11), 
        legend.position = "none", 
        panel.spacing = unit(1, "lines")) +
  labs(title = '') +
  scale_x_discrete() +
  facet_grid(CLUS~., scales="free_y")

创建于 2024-04-16,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.