2
votes

I'm really struggling with the size and color parameters of geom_path in ggplot2. Let me share my data and code (both short) with you first, then show the plot I'm getting, then explain what plot I am trying to obtain. I'm really confused with this output right now:

# the data - x and y coordinates to plot
x_loc = c(39.29376, 39.44371, 39.59578, 39.7439, 39.88808, 40.18122, 
40.92207, 41.91831, 42.09564, 42.27909, 81.77751, 81.79779, 81.81031, 
81.81723, 81.81997, 81.81846)

y_loc = c(21.02953, 20.91538, 20.80633, 20.69479, 20.58158, 20.37095, 
19.87498, 19.38372, 19.31743, 19.26005, 35.55103, 35.64354, 35.7384, 
35.82535, 35.9067, 35.98656)

# creating the factor with which to base size and color off of
end = length(x_loc)
distances = sqrt(((x_loc[2:end] - x_loc[1:(end-1)]) ^ 2) + ((y_loc[2:end] - y_loc[1:(end-1)]) ^ 2))
my_colors = c('black', ifelse(distances > 0.5, 'red', 'black'))

# and now for my plot
ggplot() + 
geom_point(aes(x = x_loc, y = y_loc)) +
geom_path(aes(x = x_loc, y = y_loc, col = as.factor(my_colors), size = as.factor(my_colors)), 
        alpha = 1) + 
scale_color_manual(values = c("black", "red")) +
scale_size_manual(values = c(1.5, 0.45))

Here is the output plot I'm getting, incase you haven't run my code:

enter image description here

Here's what I'm getting, but it's not what I want. My objective here is to plot the coordinate points with lines connecting the points, so I use separate layers for geom_point() and geom_path(). However, for very long lines (long distances between consecutive coordinates), measured in the distances vector, I would like the line color to be red and for the line to be thin. For the short distances, I would like the line color to be black and for the line to be thicker.

What's wrong with my plot above is that the long black line should not be there. There's an additional black line plotting that shouldn't appear either (where the other red line is).

(It appears that by splitting the coordinates into groups (groups by size and by color, both set using the my_colors vector), the geom_path is creating two separate paths for two separate groups of points, each of which has the respective size and colors correct. However, this results in the wrong plot)

Let me know if I'm not explaining this correctly. I really want to get to the bottom of this, somehow. I'll work now on manually creating a plot similar to what I would want, and will edit shortly with it!

Thanks!

EDIT: Here's what I'm hoping to get:

enter image description here

which was created by cheating somewhat (cheating in the sense that I can get away with this for 16 coordinates, but not for 100K), using the following 5 geom_path layers:

 ggplot() + geom_point(aes(x = x_loc, y = y_loc)) + 
    geom_path(aes(x = x_loc[1:6], y = y_loc[1:6]), 
              color = 'black',
              size = 1.5,
              alpha = 1) +
    geom_path(aes(x = x_loc[6:8], y = y_loc[6:8]), 
              color = 'red',
              size = 0.45,
              alpha = 1) + 
    geom_path(aes(x = x_loc[8:10], y = y_loc[8:10]), 
              color = 'black',
              size = 1.5,
              alpha = 1) + 
    geom_path(aes(x = x_loc[10:11], y = y_loc[10:11]), 
              color = 'red',
              size = 0.45,
              alpha = 1) + 
    geom_path(aes(x = x_loc[11:16], y = y_loc[11:16]), 
              color = 'black',
              size = 1.5,
              alpha = 1) 
1

1 Answers

4
votes

I think I solved this myself - for anybody working on this, has to do with groupings. I will edit this with a solution shortly!

EDIT:

ggplot() + 

  geom_point(aes(x = x_loc, y = y_loc)) +
  geom_path(aes(x = x_loc, y = y_loc, col = my_colors, size = my_colors, group = my_group), 
            alpha = 1) + 
  scale_color_manual(values = c("black", "red")) +
  scale_size_manual(values = c(1.5, 0.45))

this gets the job done!, needed to group everything into the same group before splitting up the colors and sizes