LATER EDIT: I finally figured out how to do it. It works with the following code:
legend_size <- c(6,5,4,3,2,1,0,1,2,3,4,5,6)
tt <- data.frame(x = c(27.9656, 27.1704, 26.2128 ,23.5634 ,25.3528 ,22.8595 ,21.3536 ,26.9125 ,23.1131 ,23.4916 ,22.4164, 23.3312, 23.3102 ,24.6147 ,26.0782 ,21.1364 ,25.5958,
27.6444, 23.9442, 26.7103, 24.5139, 23.9352, 24.2715, 22.5902, 26.6455, 22.0062, 27.9197, 25.5262, 25.2965, 26.8517, 22.9460, 27.3383, 25.2462, 23.0404,
25.7334, 25.0366 ,24.3573, 22.2263 ,25.9499, 28.0437, 21.5417, 23.5713, 28.6455, 28.3420, 26.9256, 23.8670, 22.5015 ,24.6697, 26.5735 ,23.8989 ,22.8988),
y = c(44.0883, 46.1047, 44.5001, 46.0639, 43.9779, 44.9968, 46.1335, 46.5319 ,44.4762 ,47.6608, 44.8810 ,44.0293, 46.5355 ,45.6039, 44.5104 ,45.3827 ,46.0808 ,46.2330 ,43.7897 ,45.5489 ,47.1491,
46.1784, 45.6530 ,46.9936, 47.7356, 44.9183, 45.2066, 45.6958, 46.6490, 45.1327, 43.9849, 44.2057, 47.0979, 46.3638, 45.1437, 45.2748, 44.1001, 45.4171, 46.9775, 44.3456, 46.5185, 46.7778,
44.2138, 44.7343, 47.3583 ,44.3101, 45.3005 ,45.1788, 48.1949, 47.1280, 45.8649),
sl = c(0.0, -1.0, -0.58, 0.0, 0.00, -0.90, 0.00, 0.00, 0.00, -0.45, -1.42, -0.45, -0.71, 2.80, -0.22,
0.0, 0.0, -0.62, 0.00, -2.81, -0.28, 0.00, -0.66, 0.43, -0.32, -0.43, -0.33, 0.00, 0.00, 0.00,
-0.50, 0.00, 6.18, -1.25, -1.00, -0.44, -0.52, -0.12, -0.46, 0.00, 0.00, 0.00, 0.0, 0.00, 0.00,
-3.4, 0.2, -0.4, -0.43, 0,04)
)
ggplot() +
geom_point(data = tt, aes(x = x, y = y, fill = sl, size =sl),
pch = 21, colour = "black") +
scale_fill_gradient2(
low = "blue", mid = "white", high ="red",midpoint = 0,
breaks = seq(-6,6,1), limits = c(-7,7),
guide = guide_legend()
) +
scale_size_area(breaks = seq(-6, 6, 1),limits = c(-7, 7),
guide = guide_legend(override.aes = list(size =legend_size)),max_size = 6)+
labs(x = "", y = "") +
guides(
fill = guide_legend(title = "days", title.position = "top", title.hjust = 0.5, order = 1),
size = guide_legend(title = "days", title.position = "top", title.hjust = 0.5, order = 1),
) +
theme_bw() +
theme(legend.position = "right")
I have the following code for some random values:
library(ggplot2)
legend_size <- c(6,5,4,3,2,1,0,1,2,3,4,5,6)
tt <- data.frame(x = c(27.9656, 27.1704, 26.2128 ,23.5634 ,25.3528 ,22.8595 ,21.3536 ,26.9125 ,23.1131 ,23.4916 ,22.4164, 23.3312, 23.3102 ,24.6147 ,26.0782 ,21.1364 ,25.5958,
27.6444, 23.9442, 26.7103, 24.5139, 23.9352, 24.2715, 22.5902, 26.6455, 22.0062, 27.9197, 25.5262, 25.2965, 26.8517, 22.9460, 27.3383, 25.2462, 23.0404,
25.7334, 25.0366 ,24.3573, 22.2263 ,25.9499, 28.0437, 21.5417, 23.5713, 28.6455, 28.3420, 26.9256, 23.8670, 22.5015 ,24.6697, 26.5735 ,23.8989 ,22.8988),
y = c(44.0883, 46.1047, 44.5001, 46.0639, 43.9779, 44.9968, 46.1335, 46.5319 ,44.4762 ,47.6608, 44.8810 ,44.0293, 46.5355 ,45.6039, 44.5104 ,45.3827 ,46.0808 ,46.2330 ,43.7897 ,45.5489 ,47.1491,
46.1784, 45.6530 ,46.9936, 47.7356, 44.9183, 45.2066, 45.6958, 46.6490, 45.1327, 43.9849, 44.2057, 47.0979, 46.3638, 45.1437, 45.2748, 44.1001, 45.4171, 46.9775, 44.3456, 46.5185, 46.7778,
44.2138, 44.7343, 47.3583 ,44.3101, 45.3005 ,45.1788, 48.1949, 47.1280, 45.8649),
sl = c(0.0, -1.0, -0.58, 0.0, 0.00, -0.90, 0.00, 0.00, 0.00, -0.45, -1.42, -0.45, -0.71, 2.80, -0.22,
0.0, 0.0, -0.62, 0.00, -2.81, -0.28, 0.00, -0.66, 0.43, -0.32, -0.43, -0.33, 0.00, 0.00, 0.00,
-0.50, 0.00, 6.18, -1.25, -1.00, -0.44, -0.52, -0.12, -0.46, 0.00, 0.00, 0.00, 0.0, 0.00, 0.00,
-3.4, 0.2, -0.4, -0.43, 0,04)
)
gg <- ggplot()+
geom_point(data = tt, aes(x=x, y = y, colour = sl, size = sl))+
scale_colour_continuous(low = "blue", high = "red", breaks = seq(-6,6,1)
) +
scale_size_continuous(breaks = seq(-6,6,1), limits = c(-7,7), range = c(1,7)
) +
guides(
colour = guide_legend(),
size= guide_legend(override.aes = list(size = legend_size))
)+
labs(x = "", y= "")+
theme_bw()+
theme(legend.position = "right")
The problem is for the ggplot() because the legend doesn't show only one legend, but two legends, which is not what I want. I would like only one legend showing the values from -6 to 6 (from -6 to 0 and from 0 to 6). My code works, but it shows the colours separately.I think that in guide() is something which I have missed or something.