【问题标题】:jitter geom_line()抖动 geom_line()
【发布时间】:2012-06-02 21:22:37
【问题描述】:

有没有办法抖动geom_line() 中的行?我知道这有点违背这个情节的目的,但如果你的情节很少,并且希望它们都显示出来,它可能会很方便。对于这个可见性问题,也许还有其他解决方案。

请参阅下面的代码,

A  <- c(1,2,3,5,1)
B  <- c(3,4,1,2,3)
id <- 1:5
df <- data.frame(id, A, B)


# install.packages(reshape2)
require(reshape2) # for melt
dfm <- melt(df, id=c("id"))

# install.packages(ggplot2)
require(ggplot2)
p1 <- ggplot(data = dfm, aes(x = variable, y = value, group = id, 
color= as.factor(id))) + geom_line() + labs(x = "id # 1 is hardly 
visible as it is covered by id # 5") + scale_colour_manual(values = 
c('red','blue', 'green', 'yellow', 'black')) 


p2 <- ggplot(subset(dfm, id != 5), aes(x = variable, y = value, 
group = id, color= as.factor(id))) + geom_line() + labs(x = "id # 
5 removed, id # 1 is visible") + scale_colour_manual(values = 
c('red','blue', 'green', 'yellow', 'black')) 

# install.packages(RODBC)
require(gridExtra)

grid.arrange(p1, p2)

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    你可以试试

    geom_line(position=position_jitter(w=0.02, h=0))
    

    看看效果好不好。

    【讨论】:

    • 我喜欢这个解决方案,明天我会在我的真实数据上试用它(当我回到实验室时)。你用我的代码试过了吗?
    • 是的,我想,但是您喜欢垂直抖动还是水平抖动取决于您的数据。此外,如果您只想抖动一行,那么抖动所有内容可能不是一个好主意。
    • 考虑position_dodge()以避免重叠 - 请参阅下面的答案
    【解决方案2】:

    如果你只是想防止两条线完全重叠,现在有一个更好的方法:position_dodge(),它“通过将重叠躲避到一边来调整位置”。这比向任何线路添加抖动要好,即使在不需要时也是如此。

    使用 position_dodge() 避免 ggplot2 行完全重叠

    代码示例:

    df<-data.frame(x=1:10,y=1:10,z=1:10);
    df.m <- melt(df, id.vars = "x");
    ggplot(df.m, aes(x=x,y=value,group=variable,colour=variable)) 
        + geom_line(position=position_dodge(width=0.2));
    

    感谢position_dodge(),我们现在可以看到图中有两条线恰好重合:

    【讨论】:

    • 这本来是我需要的,但由于某种原因,它在此期间被削弱了。现在position_dodge 只接受没有heightwidth 参数(版本2.2.1),因此它不能用于区分我的预处理意大利面条图中的水平条:( 仍然为你+1,因为它似乎最接近我需要的答案。
    • @rumtscho 感谢您的评论。对于其他阅读本文的人,我刚刚确认上面的基本示例(用于线图)仍然适用于 ggplot 2.2.1
    【解决方案3】:

    我倾向于使用不同的线条样式,例如,一条蓝色实线“透视”其顶部的一条红色虚线。 再说一次,它确实取决于你想向读者传达什么。首先要记住 data 应该是点和 theory 线,除非这会使事情变得混乱。除非 y x 值相同,否则会更容易看到这些点。 (或者您可以将现有的 jitter 函数应用于 x 值) 接下来,如果您只想显示哪些运行在“捆绑包”中,哪些是异常值,重叠并不重要,因为两个异常值几乎不可能相等。

    如果您想显示一组接近相等的运行,您可能更喜欢(也就是说,您的读者会更好地理解)将增量与平均值而不是实际值进行对比。

    【讨论】:

    • 我喜欢你的论点,但我不确定如何将它应用于我的具体案例。您能否提供一些示例代码或一些视觉示例的链接?
    • @EricFail :在绘制点时应用抖动的最简单方法是 y_jit&lt;-jitter(y_data) 和/或 x_data 相同,然后将抖动数据提供给绘图代码。如果你想“抖动”线条,我会选择 baptiste 的解决方案。
    【解决方案4】:

    我想提出一个解决方案来解决与描述不同的问题, 其中 Y 轴是一个因素,因此 position_dodge 什么都不做。

    代码:

    library(tidyverse)
    
    time_raw <- tibble(year=1900:1909,
                person_A=c(rep("Rome",2),rep("Jerusalem",8)),
                person_B=c(rep("Jerusalem",5),rep("Rome",5)))
    
    
    achivments <- tribble(~year,~who,~what,
                      1900,"person_A","born",
                      1900,"person_B","born",
                      1909,"person_A","died",
                      1909,"person_B","died",
                      1905,"person_A","super star",
                      1905,"person_B","super star")
    
    SCALE=0.5
    
    jitter_locations <- time_raw %>%
      pivot_longer(-year,names_to="who",values_to="place") %>%
      distinct(place)%>%
      filter(!is.na(place)) %>% 
      mutate(y_place=seq_along(place)) 
    
    jitter_lines <- time_raw %>%
      pivot_longer(-year,names_to="who",values_to="place") %>%
      distinct(who) %>%
      mutate(y_jitter=scale(seq_along(who))*0.015)
    
    data_for_plot <- time_raw %>% 
     pivot_longer(-year,names_to="who",values_to="place") %>% 
     filter(!is.na(place)) %>% 
     left_join(achivments) %>% 
     left_join(jitter_locations) %>% 
     left_join(jitter_lines)
    
    
     data_for_plot %>% 
       ggplot(aes(x=year,y=y_place+y_jitter,color=who,group=who))+
       geom_line(size=2)+
       geom_hline(aes(yintercept=y_place),size=50,alpha=0.1)+
       geom_point(data = . %>% filter(!is.na(what)),size=5)+
       geom_label(aes(label=what),size=3,nudge_y = -0.025)+
       theme_bw()+
       coord_cartesian(ylim = c(min(jitter_locations$y_place)-0.5*SCALE,
                           max(jitter_locations$y_place)+0.5*SCALE))+
       scale_y_continuous(breaks = 
       min(jitter_locations$y_place):max(jitter_locations$y_place),
                     labels = jitter_locations$place)+
       scale_x_continuous(breaks =  
       min(data_for_plot$year):max(data_for_plot$year))+
    
       ylab("Place")
    

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多