使用geom_point()避开位置,x =连续,y =因数 [英] Position dodge with geom_point(), x=continuous, y=factor

查看:94
本文介绍了使用geom_point()避开位置,x =连续,y =因数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个函数,可以一次绘制来自多个因子分析的载荷,也可以绘制变量不完全重叠(或根本不重叠)的载荷.它可以正常工作,但有时在各个分析中因子加载是相同的,这意味着这些点会相互重叠绘制.

I have made a function that can plot the loadings from many factor analyses at once, also when their variables do not overlap perfectly (or at all). It works fine, but sometimes factor loadings are identical across analyses which means that the points get plotted on top of each other.

library(pacman)
p_load(devtools, psych, stringr, plotflow)
source_url("https://raw.githubusercontent.com/Deleetdk/psych2/master/psych2.R")

loadings.plot2 = function(fa.objects, fa.names=NA) {
  fa.num = length(fa.objects) #number of fas

  #check names are correct or set automatically
  if (length(fa.names)==1 & is.na(fa.names)) {
    fa.names = str_c("fa.", 1:fa.num)
  }
  if (length(fa.names) != fa.num) {
    stop("Names vector does not match the number of factor analyses.")
  }

  #merge into df
  d = data.frame() #to merge into
  for (fa.idx in 1:fa.num) { #loop over fa objects
    loads = fa.objects[[fa.idx]]$loadings
    rnames = rownames(loads)
    loads = as.data.frame(as.vector(loads))
    rownames(loads) = rnames
    colnames(loads) = fa.names[fa.idx]

    d = merge.datasets(d, loads, 1)
  }

  #reshape to long form
  d2 = reshape(d,
               varying = 1:fa.num,
               direction="long",
               ids = rownames(d))
  d2$time = as.factor(d2$time)
  d2$id = as.factor(d2$id)
  colnames(d2)[2] = "fa"

  print(d2)

  #plot
  g = ggplot(reorder_by(id, ~ fa, d2), aes(x=fa, y=id, color=time, group=time)) +
      geom_point(position=position_dodge()) +
      xlab("Loading") + ylab("Indicator") +
      scale_color_discrete(name="Analysis",
                           labels=fa.names)

  return(g)
}

#Some example plots    
fa1 = fa(iris[-5])
fa2 = fa(iris[-c(1:50),-5])
fa3 = fa(ability)
fa4 = fa(ability[1:50,])

loadings.plot2(list(fa1,fa1,fa2))

在这里,我两次绘制同一对象只是为了显示效果.该图没有红点,因为fa.2中的绿色在最上面.相反,我希望在y轴上避开它们.但是,具有各种设置的position="dodge"似乎没有什么不同.

Here I've plotted the same object twice just to show the effect. The plot has no red points because the green ones from fa.2 are on top. Instead, I want them to be dodged on the y-axis. However, position="dodge" with various settings does not appear to make a difference.

但是,position="jitter"可以工作,但是它是随机的,因此有时它不能很好地工作,并且使情节变得混乱.

However, position="jitter" works, but it is random, so sometimes it does not work well as well as makes the plot chaotic to look at.

如何使点在y轴上闪避?

How do I make the points dodged on the y-axis?

推荐答案

显然,您只能侧身躲闪,但是有一种解决方法.诀窍是翻转x和y,执行position_dodge,然后执行coord_flip().

Apparently, you can only dodge sideways, but there is a workaround. The trick is to flip your x and y, do the position_dodge, and then do a coord_flip().

  g = ggplot(data = reorder_by(id, ~ fa, d2), aes(x=id, y=fa, color=time, group=time)) +
    geom_point(position=position_dodge(width = .5)) +
    xlab("Loading") + ylab("Indicator") +
    scale_color_discrete(name="Analysis",
                         labels=fa.names) +
    coord_flip()

这篇关于使用geom_point()避开位置,x =连续,y =因数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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