0

I'm struggling with the following issue. I visualized a big social network and would like to customize the color palette of the edges captured in geom_segmentfor better visibility. For instance, I want to replace my blue scale by a red scale. How can I achieve that in the easiest way possible?

I have the following code which is working fine:

ggplot(nodes_geo) + country_shapes +
  geom_segment(aes(x = x, y = y, xend = xend, yend = yend, alpha = weight, color = weight     # draw edges as arcs
                  ),
               data = edges_for_plot, curvature = 0.33,
               ) +
  scale_size_continuous(guide = FALSE, range = c(0.25, 2)) + # scale for edge widths
  geom_point(aes(x = longitude, y = latitude, size = weight),           # draw nodes
             shape = 21, fill = 'white',
             color = 'black', stroke = 0.5) +
  scale_size_continuous(guide = FALSE, range = c(0.01, 2)) + # scale for node size
  mapcoords + maptheme     

Many thanks in advance!

MixedModeler
  • 125
  • 7
  • 1
    Have a look at `?scale_color_gradient`. – stefan Jun 13 '21 at 10:17
  • I added `scale_color_gradient` @stefan. I receive the following error: `Scale for 'size' is already present. Adding another scale for 'size', which will replace the existing scale.`"This is because we used the “size” aesthetic and its scale twice, once for the node size and once for the line width of the curves". That is what they said on the website I used the code from: [link] https://www.r-bloggers.com/2018/05/three-ways-of-visualizing-a-graph-on-a-map/ – MixedModeler Jun 13 '21 at 14:33
  • That's not an error. Just a warning which tells you that could have only one scale_size_xxx. That's also what they explain in the blog you added as link: "With ggplot2, we’re left of with deciding which geom’s size we want to scale." – stefan Jun 13 '21 at 15:08
  • @stefan Thanks for following up on my link. So why are the edges of my network still blue when I add `scale_color_gradient`. I'm not so sure what I'm doing wrong here. Is it even possible to change the`geom_segment` color to e.g. red here? – MixedModeler Jun 13 '21 at 20:18
  • 1
    Have you set the colors to red? Something like `scale_color_gradient(low = "red", high = "red4")` should give you segments with a red color gradient. – stefan Jun 13 '21 at 21:05

1 Answers1

0

Thanks to stefan, I used the following code which changed the color from blue to red using scale_color_gradient.

 ggplot(nodes_geo) + country_shapes +
  geom_curve(aes(x = x, y = y, xend = xend, yend = yend, alpha = weight, color = weight     # draw edges as arcs),
  data = edges_for_plot, curvature = 0.33,) +
  scale_color_gradient(low = "red4", high = "red")+ # <-- Customize color
  scale_size_continuous(guide = FALSE, range = c(0.25, 2)) + # scale for edge widths
  geom_point(aes(x = longitude, y = latitude, size = weight),           # draw nodes
             shape = 21, fill = 'white',
             color = 'black', stroke = 0.5) +
  scale_size_continuous(guide = FALSE, range = c(0.01, 2)) + # scale for node size
  mapcoords + maptheme  
MixedModeler
  • 125
  • 7