简体   繁体   English

如何在R中的ggplot2中闪避点

[英]How to dodge points in ggplot2 in R

df = data.frame(subj=c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10), block=factor(rep(c(1,2),10)), acc=c(0.75,0.83,0.58,0.75,0.58,0.83,0.92,0.83,0.83,0.67,0.75,0.5,0.67,0.83,0.92,0.58,0.75,0.5,0.67,0.67))
ggplot(df,aes(block,acc,group=subj)) + geom_point(position=position_dodge(width=0.3)) + ylim(0,1) + labs(x='Block',y='Accuracy')

How do I get points to dodge each other uniformly in the horizontal direction? 如何获得点,使其在水平方向上均匀地相互避开? (I grouped by subj in order to get it to dodge at all, which might not be the correct thing to do...) (我按subj分组以便完全躲避,这可能不是正确的选择...)

I think this might be what you were looking for, although no doubt you have solved it by now. 我认为这可能是您想要的,尽管现在您已经解决了。 Hopefully it will help someone else with the same issue. 希望它可以帮助其他人遇到同样的问题。

A simple way is to use geom_dotplot like this: 一种简单的方法是像这样使用geom_dotplot

ggplot(df,aes(x=block,y=acc)) + 
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 0.03)  + ylim(0,1) + labs(x='Block',y='Accuracy')

This looks like this: 看起来像这样:

geom_dotplot示例

Note that x (block in this case) has to be a factor for this to work. 请注意,x(在这种情况下为block)必须是使其起作用的一个因素。

If they don't have to be perfectly aligned horizontally, here's one quick way of doing it, using geom_jitter . 如果不必将它们完美地水平对齐,这是使用geom_jitter的一种快速方法。 You don't need to group by subj. 您无需按主题分组。

Method 1 [Simpler]: Using geom_jitter() 方法1 [简单]:使用geom_jitter()

ggplot(df,aes(x=block,y=acc)) + geom_jitter(position=position_jitter(0.05)) + ylim(0,1) + labs(x='Block',y='Accuracy')

Play with the jitter width for greater degree of jittering. 播放抖动宽度,以获得更大程度的抖动。

which produces: 产生:

在此处输入图片说明

Method 2: Deterministically calculating the jitter value for each row 方法2:确定性地计算每一行的抖动值

We first use aggregate to count the number of duplicated entries. 我们首先使用aggregate来计算重复条目的数量。 Then in a new data frame, for each duplicated value, move it horizontally to the left by an epsilon distance. 然后在新的数据框中,对于每个重复的值,将其水平向左移动ε距离。

df$subj <- NULL #drop this so that aggregate works.

#a new data frame that shows duplicated values
agg.df <- aggregate(list(numdup=seq_len(nrow(df))), df, length)
agg.df$block <- as.numeric(agg.df$block) #block is not a factor
#      block  acc numdup
#1     2      0.50      2
#2     1      0.58      2
#3     2      0.58      1
#4     1      0.67      2
#...    
epsilon <- 0.02 #jitter distance

new.df <- NULL #create an expanded dataframe, with block value jittered deterministically
r <- 0
for (i in 1:nrow(agg.df)) {
  for (j in 1:agg.df$numdup[i]) {
    r <- r+1 #row counter in the expanded df
    new.df$block[r] <- agg.df$block[i]
    new.df$acc[r] <- agg.df$acc[i]
    new.df$jit.value[r] <- agg.df$block[i] - (j-1)*epsilon    
  }
}
new.df <- as.data.frame(new.df)
ggplot(new.df,aes(x=jit.value,y=acc)) + geom_point(size=2) + ylim(0,1)  + labs(x='Block',y='Accuracy') + xlim(0,3)

which produces: 产生:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM