我正在尝试在以下几个类别上生成两个变量(匹配,不匹配)的点图。以下代码粗略地做了我想要的(使用虚拟数据):
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)
它产生以下内容:
列标签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")
答案 0 :(得分:1)
rjturn,我意识到你正试图用格子做这件事,但我想我会分享ggplot2版本。我发现ggplot2确实更美观,更容易理解,因为有层的基础逻辑。
qplot(x=matched+unmatched, y=semester,data=dp0)+facet_grid(covariate~treatment)
您可能还想查看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))