在R中,使用具有不同大小和颜色参数的geom_path [英] In R, using geom_path with different size and color parameters

查看:147
本文介绍了在R中,使用具有不同大小和颜色参数的geom_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的在ggplot2中的geom_path的大小和颜色参数上苦苦挣扎.首先让我与您分享我的数据和代码(均简短),然后显示我得到的图,然后解释我要获取的图.我现在真的很困惑这个输出:

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:

这就是我得到的,但这不是我想要的.我的目标是用连接点的线来绘制坐标点,因此我对geom_point()和geom_path()使用了单独的图层.但是,对于非常长的线(连续坐标之间的长距离)(以距离矢量测量),我希望线颜色为红色,并且线为细.对于短距离,我希望线条颜色为黑色,并且线条更粗.

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

(似乎是通过将坐标分为几组(按大小和颜色分组,均使用my_colors矢量进行设置),geom_path为两组独立的点创建了两条独立的路径,每组点都有各自的大小和颜色正确.但是,这会导致错误的绘图)

(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!

谢谢!

这就是我希望得到的:

是通过使用以下5个geom_path层通过某种作弊(即我可以针对16个坐标而不是100K进行欺骗)创建的:

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) 

推荐答案

我想我自己解决了这个问题-因为从事此工作的任何人都与分组有关.我将很快用解决方案对其进行编辑!

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

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

这篇关于在R中,使用具有不同大小和颜色参数的geom_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆