基于斜率改变ggplot中的线颜色 [英] Changing line color in ggplot based on slope

查看:130
本文介绍了基于斜率改变ggplot中的线颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码绘制点并在它们之间画线。

  ggplot(data = subset(df,vowel ==O& gender ==f),aes( x = time,y = val,color = formant))+ 
geom_point()+
geom_line(aes(group = interaction(formant,number)))





有没有一种方法可以根据这些线的负斜率与正斜率的颜色/线型进行组合?

编辑:
这里是我的数据:

  number <-c(1,2,3, (F2,F2,F2,F2,F2,F2,F2,F2 ,F3,F3,F3,F3,F3,F3)
时间<-c(50,50,50,99,99,99,50, (400,500,600,450,550,650,300,400,500,250,350,450)
(c)中的至少一个(例如50,50,90,99,99) code>

我想显示在值 val 超过时间 formant 和<$分组C $ C>号。所以当我实现答案时,它告诉我我有一个不兼容的大小,我认为这与它按数字分组的事实有关。

解决方案

你还没有提供样本数据,所以这是一个风格化的例子。总体思路是创建一个变量,测试斜率是否大于零,然后将其映射到颜色审美。在这种情况下,我使用 dplyr 链接运算符(%>%)来添加在 ggplot 的调用中飞行。 (我去计算斜率的麻烦,但你也可以测试是否值[t == 2]>值[t == 1] 。 )

  library(dplyr)

#假数据
set.seed(205)
dat = data.frame(t = rep(1:2,each = 10),
pairs = rep(1:10,2),
value = rnorm(20),
group = rep(c(A,B),10))

dat $ value [dat $ group ==A] = dat $ value [dat $ group ==A] + 6

ggplot(dat%>%group_by(pairs)%>%
mutate(slope =(value [t == 2] - value [ ())
aes(t,value,group = pairs,linetype = group,color = slope> 0))+
geom_point() b $ b geom_line()



更新:根据您的评论,这听起来像您只需要映射 nu mber 改为审美或使用面。以下是使用您的示例数据的分面版本:

$ $ $ $ $ c $ df data.frame(number,formant,time,val)

#shift val a
set.seed(1095)
df $ val = df $ val + rnorm(nrow(df),0,10)
$ b $ (斜率=(val [time == 99] --val [time == 50])/(99-50))b ggplot(df%>%group_by(formant,number)%>%
mutate ,
aes(x = time,y = val,linetype = formant,color = slope> 0))+
geom_point()+
geom_line(aes(group = interaction(formant,数字)))+
facet_grid(。〜number)



这是另一个将数字映射到点标记大小的选项。这看起来不太好,只是为了说明如何将变量映射到图中的不同美学(颜色,形状,大小等)。

  ggplot(df%>%group_by(formant,number)%>%
mutate(slope =(val [time == 99] - val [time = = 50])/(99-50)),
aes(x =时间,y = val,线型= formant,color =斜率> 0))+
geom_point(aes(size = number ))+
geom_line(aes(group = interaction(formant,number)))


I have the following code which plots points and draw a line between them.

ggplot (data = subset(df, vowel == "O" & gender == "f"), aes (x = time, y = val, color = formant)) +
      geom_point()+
      geom_line(aes(group=interaction(formant, number)))

It produces this:

Is there a way to group these by color/line type for negative slopes vs. positive slopes of these lines?

edit: Here is my data:

number <- c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
formant <- c("F2", "F2", "F2", "F2", "F2", "F2", "F3", "F3", "F3", "F3", "F3", "F3")
time <- c(50, 50, 50, 99, 99, 99, 50, 50, 50, 99, 99, 99)
val <- c(400, 500, 600, 450, 550, 650, 300, 400, 500, 250, 350, 450)

I want to show movement of in the value of val over time grouped by formant and number. So when I implement the answer, it tells me I have an incompatible size, which I think has something to do with the fact that it's grouped by number.

解决方案

You haven't provided sample data, so here's a stylized example. The general idea is that you create a variable that tests whether the slope is greater than zero and then map that to a colour aesthetic. In this case, I use the dplyr chaining operator (%>%) in order to add the slope on the fly within the call to ggplot. (I went to the trouble of calculating the slope, but you could just as well test whether value[t==2] > value[t==1] instead.)

library(dplyr)

# Fake data
set.seed(205)
dat = data.frame(t=rep(1:2, each=10), 
                 pairs=rep(1:10,2), 
                 value=rnorm(20), 
                 group=rep(c("A","B"), 10))

dat$value[dat$group=="A"] = dat$value[dat$group=="A"] + 6

ggplot(dat %>% group_by(pairs) %>%
         mutate(slope = (value[t==2] - value[t==1])/(2-1)),
       aes(t, value, group=pairs, linetype=group, colour=slope > 0)) +
  geom_point() +
  geom_line()

UPDATE: Based on your comment, it sounds like you just need to map number to an aesthetic or use faceting. Here's a facetted version using your sample data:

df = data.frame(number, formant, time, val)

# Shift val a bit
set.seed(1095)
df$val = df$val + rnorm(nrow(df), 0, 10)

ggplot (df %>% group_by(formant, number) %>%
          mutate(slope=(val[time==99] - val[time==50])/(99-50)), 
        aes (x = time, y = val, linetype = formant, colour=slope > 0)) +
  geom_point()+
  geom_line(aes(group=interaction(formant, number))) +
  facet_grid(. ~ number)

Here's another option that maps number to the size of the point markers. This doesn't look very good, but is just for illustration to show how to map variables to different "aesthetics" (colour, shape, size, etc.) in the graph.

ggplot (df %>% group_by(formant, number) %>% 
          mutate(slope=(val[time==99] - val[time==50])/(99-50)), 
        aes (x = time, y = val, linetype = formant, colour=slope > 0)) +
  geom_point(aes(size=number))+
  geom_line(aes(group=interaction(formant, number)))

这篇关于基于斜率改变ggplot中的线颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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