ggplot:一个颜色/类的多行 [英] ggplot: Multiple Lines for one Color/class

查看:125
本文介绍了ggplot:一个颜色/类的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将多行作为ggplot2中单个类的一部分。我可以绘制如下内容:




我的问题是,如果我在数据框中创建了一个新因子以将它们分组,我最终从n1的末端绘制了添加连接(右上角)到n2(左下角)的开头,它在我的图上添加了一条对角线:



这更接近我想要的,但具有额外的对角线。如果它很重要,我将使用ROCR计算这些值,然后从结果 performance 对象中提取x和y点以生成这些行。

解决方案

以下是一种方法,但我不认为您会非常喜欢:
<$ (x = 1:10,
y = rep(1:2,each = 5),
grp = factor(rep( (d1,data.frame(x = 1:2,y = c(NA, NA),grp = c('c','c')))

d2 < - data.frame(x = 1:15,
y = rep(3:5, )=
grp = rep(1:3,each = 5) y = y,group = grp,color = grp))+
geom_line(data = d2,aes(x = x,y = y,group = grp),color =blue)



请注意,您的解决方案不适用于其他类型的数据。恰好在图例中你想要合并成同一类别的三条线中的每条线都开始和结束于基本相同的位置。如果情况并非如此,那么最终会出现不需要的连接线。



上述方法可以更普遍地工作,但正如您所看到的,不会少于尴尬。如果有的话更加不方便。

这种问题已经出现很多次了,可能是我忘记了一个更简单的解决方案。但总的来说,请记住,与使用基本图形相比,通过手动构建每个图例来交换 ggplot2 ,您(通常但不总是)牺牲了一些灵活性。

I'm trying to plot multiple lines as a part of a single "class" in ggplot2. I can plot something like the following:

But my issue is that I want to show n1, n2, and n3 as a single class (grey thin lines for each with a single name in the legend).

My problem is that if I create a new factor in the data frame in order to group them, I end up drawing the addition connection from the end of n1 (upper right) to the beginning of n2 (lower left) which adds a diagonal across my graph:

That's closer to what I want, but has the additional diagonal line. If it matters, I'm computing these using ROCR then extracting the x and y points from the resultant performance object in order to produce these lines.

解决方案

Here's one way, but I don't think you'll like it very much:

d1 <- data.frame(x = 1:10,
                 y = rep(1:2,each = 5),
                 grp = factor(rep(letters[1:2],each = 5),levels = letters[1:3]))
d1 <- rbind(d1,data.frame(x = 1:2,y = c(NA,NA),grp = c('c','c')))

d2 <- data.frame(x = 1:15,
                 y = rep(3:5,each = 5),
                 grp = rep(1:3,each = 5))   

ggplot() + 
    geom_line(data = d1,aes(x = x,y = y,group = grp,colour = grp)) + 
    geom_line(data = d2,aes(x = x,y = y,group = grp),colour = "blue")

Note that your solution won't work well with other kinds of data. It just happens that each of the three lines you want to merge into the same category in the legend begin and end in basically the same spot. If that weren't the case, you'd end up with unwanted connector lines.

The above method will work more generally, but as you can see isn't less awkward. If anything it's more inconvenient.

This kind of issue has come up many times, and it could be that I'm forgetting a simpler solution. But in general, try to keep in mind that in exchange for ggplot2 saving you from having to construct every legend manually, you (frequently, but not always) are sacrificing some flexibility, compared to base graphics.

这篇关于ggplot:一个颜色/类的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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