抖动 geom_line()

31

有没有办法在geom_line()中抖动线条?我知道这有点违背了这个图的目的,但如果您有一个只有几条线并希望它们都显示的图,这可能很方便。也许还有其他解决这个可见性问题的方法。

请参见下面的代码, jitter 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)
4个回答

39

您可以尝试

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

并查看它是否工作良好。


我喜欢这个解决方案,明天(回到实验室后)我会在我的真实数据上尝试一下。你试过用我的代码吗? - Eric Fail
是的,我做了,但是你喜欢垂直还是水平抖动取决于你的数据。而且,如果你只想要一行抖动的话,把所有东西都抖动可能不是一个好主意。 - baptiste
考虑使用“position_dodge()”来避免重叠-请参见我下面的答案。 - flexponsive

30

如果你只想防止两条线完全重叠,现在有了更好的方法: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_doge在ggplot中防止线条重叠


1
这本来是我需要的,但由于某些原因,它在此期间被削弱了。现在 position_dodge 只带有一个 width 参数而没有 height(版本2.2.1),因此无法用于区分我的预后处理意大利面图中的水平条 :( 仍然+1给你,因为它似乎是我需要的答案最接近的一个。 - rumtscho
1
@rumtscho 感谢您的评论。对于其他阅读此内容的人,我刚刚确认基本示例(用于线图)在 ggplot 2.2.1 中仍然可以正常工作。 - flexponsive

4
我倾向于使用不同的线条样式,这样,例如,一个实心蓝色线条会在其上方的虚线红色线条中“露出一点”。但这取决于您想向读者传达什么。首先要记住的是,数据应该是点,理论应该是线,除非这样会使事情变得混乱。除非y x值相同,否则看到点会更容易。(或者您可以将现有的jitter函数应用于x值)接下来,如果您只想显示哪些运行处于“捆绑”状态,哪些是异常值,则重叠并不重要,因为两个异常值很少会非常接近。
如果您想展示一堆接近相等的运行,则可能更喜欢(也就是说,您的读者会更好地理解)将增量绘制成平均值而不是实际值。

我喜欢你的论点,但我不确定如何将其应用于我的特定情况。您能否提供一些示例代码或链接到一些视觉示例? - Eric Fail
1
@EricFail:在绘制点时应用抖动的最简单方法就是 y_jit<-jitter(y_data),或者对于 x_data 也是同样的操作,然后将抖动后的数据提供给您的绘图代码。如果您想要“抖动”线条,我会采用baptiste的解决方案。 - Carl Witthoft

2
我想提出一个不同于描述的问题的解决方案,其中Y轴是一个因素,因此position_dodge没有作用。

enter image description here

代码:
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)))


achievements <- 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(achievements) %>% 
 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")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接