1

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")

enter image description here

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.

2
  • What's the benefit of both color and size showing the same dimension, that's confusing.
    – jay.sf
    Commented Jul 10 at 16:46
  • I think it is not confusing at all. Please see my later edit.
    – Alex.V
    Commented Jul 11 at 11:16

2 Answers 2

2

You need a continuous color scale with integer, meaning, discrete breaks. It is important to note that in this case

seq(-6, 6, 1)
and
-6:6

are not the same, the latter is continuous and the former is a discrete vector (it has class "integer").
See this SO post.

library(ggplot2)

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, colour = sl, size = sl)) +
  scale_colour_gradientn(
    colors = c("blue", "blue", "red", "red"),
    values = c(0, 0.25, 0.75, 1),
    breaks = -6:6, 
    guide = guide_legend()
  ) +
  scale_size_continuous(breaks = -6:6) +
  labs(x = "", y = "") +
  theme_bw() +
  theme(legend.position = "right")

Created on 2024-07-10 with reprex v2.1.0


Edit

To have the legend sizes increase with their absolute values use override.aes in guide_legend. I had to shorten the vector legend_size to length 10.

legend_size <- c(3,2,1,0,1,2,3,4,5,6)

ggplot() + 
  geom_point(data = tt, aes(x = x, y = y, colour = sl, size = sl)) +
  scale_colour_gradientn(
    name = "sl",
    colors = c("blue", "blue", "red", "red"),
    values = c(0, 0.25, 0.75, 1),
    breaks = -6:6, 
    guide = guide_legend(override.aes = list(size = legend_size))
  ) +
  scale_size_continuous(
    name = "sl", 
    breaks = -6:6) +
  labs(x = "", y = "") +
  theme_bw() +
  theme(legend.position = "right")

Created on 2024-07-10 with reprex v2.1.0

3
  • hi, thanks. However, the legend is not showing correctly. I mean the value -3 circle size should be bigger than value -1 and the value 3 circle size should be greater than 0. My initial code shows the legend somehow correctly but the legend is not linked with the points in the plot.
    – Alex.V
    Commented Jul 10 at 12:26
  • @Alex.V Done, see if this solves the problem. Commented Jul 10 at 14:54
  • please see my later edit. I figured out how to make it. Thanks.
    – Alex.V
    Commented Jul 11 at 11:15
0

Edit: Use Rui Barradas' suggestion instead...


You can change the legend option to "none"

ggplot() +
  guides(
    colour = "none",
    ...
  )

enter image description here

1
  • Ok, but the legend is not linked with the size and color point in the plot. This is my problem too. The legend is missing the colour. The size value of the legend is fine.
    – Alex.V
    Commented Jul 10 at 12:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.