-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR_legend.R
87 lines (75 loc) · 3.53 KB
/
R_legend.R
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
<<<<<<< HEAD
library(ggplot2)
library(grid)
# Define categories and their corresponding unique colors
#categories <- c("Suprarenal abdominal aorta", "Infrarenal aorta", "Inferior vena cava", "Infrarenal vena cava" ,"Cortex","Medulla", "Pelvis", "Liver" ,"Spleen")
categories <- c("Suprarenal abdominal aorta", "Inferior vena cava")
#categories <- c("Cortex", "Medulla", "Pelvis")
colors <- c("Suprarenal abdominal aorta" = "red",
"Infrarenal aorta" = "pink",
"Inferior vena cava" = "blue",
"Infrarenal vena cava" = "lightblue",
"Cortex" = "purple",
"Medulla" = "orange",
"Pelvis" = "lightgreen",
"Liver" = "gold",
"Spleen"= "hotpink")
# Create a dummy dataframe
data <- data.frame(Category = factor(categories, levels = categories))
# Create the plot with proper color mappings
gg <- ggplot(data, aes(x = 1, y = Category, fill = Category)) +
geom_tile() + # using geom_tile to create colored boxes
scale_fill_manual(values = colors) +
theme_void() +
theme(legend.position = "bottom",
legend.text = element_text(size = 12, face = "bold"), # Bold legend text
legend.box.spacing = unit(2, "lines"), # Increase spacing between legend items
legend.spacing.x = unit(1, "cm"), # Increase spacing between legend columns
legend.spacing.y = unit(1, "cm")) + # Increase spacing between legend rows
guides(fill = guide_legend(title = "",
nrow = 10, ncol = 1, # Specify the layout of the legend items
byrow = TRUE)) # Fill by rows first
# Extract the legend
g <- ggplotGrob(gg)
legend <- gtable::gtable_filter(g, "guide-box")
# Draw the legend
grid.newpage()
grid.draw(legend)
=======
library(ggplot2)
library(grid)
# Define categories and their corresponding unique colors
#categories <- c("Suprarenal abdominal aorta", "Infrarenal aorta", "Inferior vena cava", "Infrarenal vena cava" ,"Cortex","Medulla", "Pelvis", "Liver" ,"Spleen")
categories <- c("Suprarenal abdominal aorta", "Inferior vena cava")
#categories <- c("Cortex", "Medulla", "Pelvis")
colors <- c("Suprarenal abdominal aorta" = "red",
"Infrarenal aorta" = "pink",
"Inferior vena cava" = "blue",
"Infrarenal vena cava" = "lightblue",
"Cortex" = "purple",
"Medulla" = "orange",
"Pelvis" = "lightgreen",
"Liver" = "gold",
"Spleen"= "hotpink")
# Create a dummy dataframe
data <- data.frame(Category = factor(categories, levels = categories))
# Create the plot with proper color mappings
gg <- ggplot(data, aes(x = 1, y = Category, fill = Category)) +
geom_tile() + # using geom_tile to create colored boxes
scale_fill_manual(values = colors) +
theme_void() +
theme(legend.position = "bottom",
legend.text = element_text(size = 12, face = "bold"), # Bold legend text
legend.box.spacing = unit(2, "lines"), # Increase spacing between legend items
legend.spacing.x = unit(1, "cm"), # Increase spacing between legend columns
legend.spacing.y = unit(1, "cm")) + # Increase spacing between legend rows
guides(fill = guide_legend(title = "",
nrow = 10, ncol = 1, # Specify the layout of the legend items
byrow = TRUE)) # Fill by rows first
# Extract the legend
g <- ggplotGrob(gg)
legend <- gtable::gtable_filter(g, "guide-box")
# Draw the legend
grid.newpage()
grid.draw(legend)
>>>>>>> 77a3982bf0902d3f28a8584daad61788e60cc341