2
votes

I am using ggplot2 to plot values stored in 12 matrices. Each matrix cell can contain a number from 1 to 10, and I use ggplot2 to plot these values with:

col<-c("red", "orange", "blue", "cyan", "green", "lightgreen", "violet", "pink", "black", "brown")
tdm <- melt(node10den0.7)
ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) +
labs(x = "Cost", y = "Benefit", fill = "Value", title="10 nodes, den=0.7") +
geom_raster() +
scale_fill_manual("Legend", breaks = c("1","2","3","4","5","6","7","8","9","10"),
                  values = col, labels=c("null", "n-null", "star", "n-star", "bistar", "n-bistar", "shared", "n-shared", "complete", "n-complete")) +
theme(plot.background = element_rect(fill = "grey90"),
      legend.background = element_rect(fill = "grey90")) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))

My problem is that not all the matrices contain all the possible values, so I see there is a shift of colors for some values in some of the plots... Is there a way to keep color fixed? like if you see 2 in the matrix then plot it red?

Second question: Is it possible to make ggplot2 display all the possible colors/labels in the legend?

I add a couple of my matrices as example:

> dput(node3den0)
structure(c(2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 
8, 2, 0, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 
0, 0, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 0, 0, 
0, 2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 2, 2, 0, 0, 0, 0, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 10, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 
2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 2, 2, 2, 2, 2, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 2), .Dim = c(19L, 19L), .Dimnames = list(
c("0.05", "0.1", "0.15", "0.2", "0.25", "0.3", "0.35", "0.4", 
"0.45", "0.5", "0.55", "0.6", "0.65", "0.7", "0.75", "0.8", 
"0.85", "0.9", "0.95"), c("0.05", "0.1", "0.15", "0.2", "0.25", 
"0.3", "0.35", "0.4", "0.45", "0.5", "0.55", "0.6", "0.65", 
"0.7", "0.75", "0.8", "0.85", "0.9", "0.95")))

> dput(node10den0.7)
structure(c(3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 
8, 7, 0, 3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 2, 
0, 0, 10, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 2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.7", "0.75", "0.8", "0.85", "0.9", "0.95"), c("0.05", 
"0.1", "0.15", "0.2", "0.25", "0.3", "0.35", "0.4", "0.45", "0.5", 
"0.55", "0.6", "0.65", "0.7", "0.75", "0.8", "0.85", "0.9", "0.95"
)))

Best regards, Simone

1

1 Answers

2
votes

Use the limits argument. With your first matrix:

col<-c("red", "orange", "blue", "cyan", 
       "green", "lightgreen", "violet", "pink", "black", "brown", "yellow")
ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) +
  labs(x = "Cost", y = "Benefit", fill = "Value", title="10 nodes, den=0.7") +
  geom_raster() +
  scale_fill_manual("Legend", limits = 0:10,
                    values = col, 
                    labels=c("null", "n-null", "star", "n-star", "bistar", 
                      "n-bistar", "shared", "n-shared", "complete", "n-complete",
                      "additional")) +
  theme(plot.background = element_rect(fill = "grey90"),
        legend.background = element_rect(fill = "grey90")) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

enter image description here

Note that I added one colour value because your possible values seem to go from 0 to 10.