Repository navigation
Expand file tree
/
Copy pathtest_fill_legend_fix.R
More file actions
108 lines (96 loc) · 2.79 KB
/
Copy pathtest_fill_legend_fix.R
File metadata and controls
108 lines (96 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env Rscript
# Test to verify that fill legend appears when using discrete color coding in geom_dice()
library(ggplot2)
library(ggdiceplot)
## ---- SPARSE TEST DATA ----
df_dice <- data.frame(
miRNA = c(
# miR-1
"miR-1", "miR-1", # compound 1 (two organs only)
"miR-1", # compound 2 (one organ)
# miR-2
"miR-2", "miR-2", "miR-2", # complete row
# miR-3
"miR-3", # only one organ across all compounds
"miR-3"
),
Compound = c(
# miR-1
"Compound_1", "Compound_1",
"Compound_2",
# miR-2
"Compound_1", "Compound_2", "Compound_3",
# miR-3
"Compound_3",
"Compound_1"
),
Organ = c(
# miR-1
"Lung", "Brain",
"Liver",
# miR-2
"Lung", "Liver", "Brain",
# miR-3
"Liver",
"Brain"
),
log2FC = c(
1.2, -0.8,
0.3,
-1.1, 0.6, 2.0,
-0.5,
1.5
)
)
## ---- DIRECTION CATEGORIES ----
df_dice$direction <- ifelse(
df_dice$log2FC > 0.5, "Up",
ifelse(df_dice$log2FC < -0.5, "Down", "Unchanged")
)
## ---- FACTOR LEVELS ----
organ_levels_boxes <- c("Lung", "Liver", "Brain")
df_dice$Organ <- factor(df_dice$Organ, levels = organ_levels_boxes)
df_dice$direction <- factor(df_dice$direction, levels = c("Down", "Unchanged", "Up"))
df_dice$miRNA <- factor(df_dice$miRNA)
df_dice$Compound <- factor(df_dice$Compound,
levels = c("Compound_1", "Compound_2", "Compound_3"))
direction_colors <- c(
Down = "#2166ac", # blue
Unchanged = "grey80",
Up = "#b2182b" # red
)
## ---- PLOT ----
p_dice <- ggplot(df_dice, aes(x = miRNA, y = Compound)) +
geom_dice(
aes(
dots = Organ, # Position in dice = Organ
fill = direction, # Fill = Up / Down / Unchanged
width = 0.8,
height = 0.8
),
show.legend = TRUE,
ndots = length(levels(df_dice$Organ)), # = 3 organs
x_length = length(levels(df_dice$miRNA)),
y_length = length(levels(df_dice$Compound))
) +
scale_fill_manual(
values = direction_colors,
drop = FALSE,
name = "Regulation"
) +
theme_dice(x_length = length(levels(df_dice$miRNA)),
y_length = length(levels(df_dice$Compound))) +
theme(
axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.5),
axis.text.y = element_text(hjust = 1),
panel.grid = element_blank() # remove background grid
) +
labs(
title = "DicePlot: log2FC direction per miRNA, compound and organ",
x = "miRNA",
y = "Compound"
)
# Save plot
ggsave("test_fill_legend_after_fix.png", p_dice, width = 10, height = 8, dpi = 150)
print("Plot saved to test_fill_legend_after_fix.png")
print("Expected: Both 'Organ' legend (showing dice positions) AND 'Regulation' legend (showing fill colors)")