r 比例 alpha 不连续 [英] r scale alpha not continuous

查看:48
本文介绍了r 比例 alpha 不连续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取以下数据

df <- data.frame(ID = c(1, 1, 2, 2, 3, 3),cond = c(2, 2, 2, 2, 1, 1),时间 = c(1,2,1,2,1,2),状态 = c(0, 0, 0, 1, 1, 0),眼睛 = c(2, -3, -2, 1, 0, -2),组合 = c(1"、1"、2"、2"、3"、3"))df$cond <- factor(df$cond,levels = c(1", 2"))

ggplot(df, aes(x = 时间, y = 眼睛)) +ggforce::geom_link2(aes(group=ID, color = cond, alpha = State), size = 5, n = 500, lineend = "round") +scale_color_manual(values=c("#CC6666", "#9999CC")) +scale_alpha_continuous(range = c(0.01, 0.9)) +scale_shape_manual(values=c(13,15)) +主题(legend.title=element_blank(),axis.title.x=element_text(size = 12),axis.title.y=element_text(size = 12),plot.title = element_text(hjust = 0.5, size = 15),axis.text.x= element_text(color = "black"),axis.text.y= element_text(color = "black"),axis.line = element_line(colour = "black"),plot.background = element_rect(fill = "white"),panel.background = element_rect(fill = "white"))

这会产生以下图片:

我试图通过 alpha 的连续变化使第 2 行和第 3 行在 x 轴上出现/消失.尽管线条是在两个值 (0, 1) 之间绘制的,但 alpha 似乎并没有在线条上均匀变化(从 0.01 alpha 值开始,0.9 alpha 在值 1 开始).相反,线条几乎立即增长到完整的 0.9 alpha - 为什么会这样,我该如何解决?

解决方案

这是一种改编自 @aosmith 对您的相关问题的评论中的链接答案的方法:

淡入淡出的变化更平滑一些.调整 ^2 幂以移动 alpha 的中点.

<代码>....alpha_OP = case_when(firstFull &!endFull ~ 1 - 帧^2!firstFull &endFull ~ 帧^2,TRUE ~ sin(frame*50)*0.5 # 这只是为了好玩)%>% pmax(0)) %>%...

take the following data

df <- data.frame(ID = c(1, 1, 2, 2, 3, 3),
                 cond = c(2, 2, 2, 2, 1, 1),
                 Time = c(1,2,1,2,1,2),
                 State  = c(0, 0, 0, 1, 1, 0),
                 eyes = c(2, -3, -2, 1, 0, -2),
                 combination = c("1", "1","2", "2", "3", "3"))
df$cond <- factor(df$cond,levels = c("1", "2"))

ggplot(df, aes(x = Time, y = eyes)) +
  ggforce::geom_link2(aes(group=ID, color = cond, alpha = State), size = 5, n = 500, lineend = "round") +
  scale_color_manual(values=c("#CC6666", "#9999CC")) +
  scale_alpha_continuous(range = c(0.01, 0.9)) +
  scale_shape_manual(values=c(13,15)) +
  
    theme(legend.title=element_blank(),
        axis.title.x=element_text(size = 12),
        axis.title.y=element_text(size = 12),
        plot.title = element_text(hjust = 0.5, size = 15),
        axis.text.x= element_text(color = "black"),
        axis.text.y= element_text(color = "black"),
        axis.line = element_line(colour = "black"),
        plot.background = element_rect(fill = "white"),
        panel.background = element_rect(fill = "white"))

This produces the following picture:

I am trying to make line 2 and 3 appear / disappear across the x-axis by a continuous change in alpha. Although the lines are drawn between two values (0, 1), the alpha does not seem to change evenly across the line (start as 0.01 alpha at value 0, and 0.9 alpha at value 1) . Instead, the lines almost immediately grows to the full 0.9 alpha - why is this and how do I fix it?

解决方案

Here's an approach adapted from the linked answer in @aosmith's comment to your related question: https://stackoverflow.com/a/17794763/2461552.

I divide the segments into 100 pieces and use formulas to define two fade-in gradients, each going to zero 25% of the way in from the State=0 end. (in the case of the first line, State 0 at both ends, I arbitrarily use a sine pattern.

seg = 100
df %>%
  group_by(ID) %>%
  summarize(minTime = first(Time), maxTime = last(Time),
            mineyes = first(eyes), maxeyes = last(eyes),
            firstFull = first(State), endFull = last(State)) %>%
  uncount(seg, .id = "frame") %>%
  mutate(frame = (frame - 1)/seg) %>%
  mutate(Time = maxTime * frame + minTime * (1-frame),
         eyes  = maxeyes * frame  + mineyes * (1-frame),
         alpha_OP = case_when(
           firstFull & !endFull ~ 1 - 1.33*frame,
           !firstFull & endFull ~ 1.33*frame - 0.33,
           TRUE ~ sin(frame*50)*0.5  # this is just for fun
         ) %>% pmax(0)) %>%
  
  ggplot(aes(Time, eyes, alpha = alpha_OP, group = ID)) +
  ggforce::geom_link2() +
  scale_alpha(range = c(0,1))
  

EDIT: Variation in fade that's a little smoother. Tweak the ^2 power to shift the midpoint of the alpha.

....
alpha_OP = case_when(
               firstFull & !endFull ~ 1 - frame^2
               !firstFull & endFull ~ frame^2,
               TRUE ~ sin(frame*50)*0.5  # this is just for fun
             ) %>% pmax(0)) %>%
...

这篇关于r 比例 alpha 不连续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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