多个模型上面板变量的双结果点图(R 中的点阵图)

问题描述 投票:0回答:2

我正在尝试在几个类别的两个变量(匹配、不匹配)上生成点图,如下所示。以下代码大致完成了我想要的操作(使用虚拟数据):

library(lattice)

# Generate dummy data that resembles my real data
dp0 <- data.frame(expand.grid(covariate=c("var1","var2"), semester=1:5,
                              treatment=LETTERS[1:3]), matched=runif(30),
                              unmatched=runif(30))

# Plot matched and unmatched (percentages) by several semester-level covariates
# across several treatments (models):
dotplot(semester ~ matched + unmatched | treatment + covariate, data=dp0)

它产生以下内容:

Two-outcome dotplot by covariate-semester over treatment

列标签 A、B 和 C 都很好(不需要跨行复制,也不需要可怕的桃色,但可以接受),但行标签(var2、var1)的重要性不如列标签。我设想的东西看起来像这样:

       |    A    |    B    |    C    |
var1:  |                             |
.... 1 | x  o    |   o   x |  ox     |
.... 2 |   ox    | x  o    |  o    x |
.... 3 |o     x  |     o  x| x o     |
.... 4 | x     o | x o     |  o  x   |
.... 5 |   o x   |     x o |    ox   |
       |         |         |         |
var2:...

例如,我试图扰乱团体,但这没有帮助。该图是正确的,只是它的显示方式具有误导性(换句话说,这是显示问题,而不是数据问题)。为了实现这一目标,我应该寻找哪些概念?任何想法表示赞赏。

基于@JPC答案的最终图表:

ma <- c("Unmatched", "Matched")
ma <- factor(ma, levels=ma, ordered=TRUE)
ggplot(dp0, aes(y=semester)) + geom_point(aes(x=unmatched, shape=ma[1], color=ma[1])) +
    geom_point(aes(x=matched, shape=ma[2], color=ma[2])) + facet_grid(covariate~treatment) +
    xlim(-1, 1) + labs(x=NULL,y=NULL) + scale_shape_discrete(name="") +
    scale_colour_discrete(name="", guide="legend") + theme(legend.position="bottom")

Final Plot

r lattice
2个回答
1
投票

rjturn,我意识到你正在尝试用lattice来做到这一点,但我想我会分享ggplot2版本。我发现 ggplot2 确实更漂亮,更容易理解,因为有层的底层逻辑。

qplot(x=matched+unmatched, y=semester,data=dp0)+facet_grid(covariate~treatment)

enter image description here

您可能还想查看

reshape
库,它与
ggplot2
结合使用非常有用。请参阅下面的图形代码的简化。

library(reshape2)
flatdp0<-melt(data=dp0,id=c("covariate","semester","treatment"))

qplot(data=flatdp0,x=value, y=semester,color=variable, shape=variable)+
  facet_grid(covariate ~ treatment)+labs(color="",shape="",x="",y="")+
  theme(legend.position="bottom",axis.text.x=element_text(angle=90))

enter image description here


0
投票

虽然这是一个较晚的答案,但它可能会帮助那些仍在使用

lattice
的人,并且它解决了已接受答案中的
qplot()
现已弃用的事实,以 鼓励用户学习
ggplot()
,因为它使创建更容易复杂的图形
。我发现
ggplot
然后
ggplot2
的进化比我希望的要频繁。我用它来完成一些任务,但在投入时间学习
trellis
然后学习
lattice
之后,我发现
lattice
仍然相当有用。此外,最新版本的
lattice
使用比“可怕的桃色”更柔和的配色方案!

  library(latticeExtra) # need this support package
  obj <- dotplot(semester ~ matched + unmatched | treatment + covariate, dp0,
    scales = list(x = list(alternating = FALSE, tck = c(0.5, 0), rot = 90)))
  useOuterStrips(obj)

© www.soinside.com 2019 - 2024. All rights reserved.