4

如何区分 4 个不同的因素(不使用大小)?是否可以使用空心点和实心点来区分 ggplot2 中的变量?

test=data.frame(x=runif(12,0,1),
     y=runif(12,0,1),
     siteloc=as.factor(c('a','b','a','b','a','b','a','b','a','b','a','b')),
     modeltype=as.factor(c('q','r','s','q','r','s','q','r','s','q','r','s')),
     mth=c('Mar','Apr','May','Mar','Apr','May','Mar','Apr','May','Mar','Apr','May'),
     yr=c(2010,2011,2010,2011,2010,2011,2010,2011,2010,2011,2010,2011))

其中 x 是观察值,y 是建模结果,我想比较多个因素的不同模型版本。谢谢!

4

3 回答 3

5

我认为,很难根据 4 个因素在视觉上区分/比较 x 和 y 值。我会使用刻面,并使用interaction例如减少因子的数量。

这是一个使用示例geom_bar

在此处输入图像描述

set.seed(10)
library(reshape2)
test.m <- melt(test,measure.vars=c('x','y'))
ggplot(test.m)+
  geom_bar(aes(x=interaction(yr,mth),y=value,
                 fill=variable),stat='identity',position='dodge')+
  facet_grid(modeltype~siteloc)
于 2013-07-28T00:57:50.997 回答
3

我真的很喜欢使用interactionagstudy - 我可能会先尝试一下。但如果保持不变,那么:

刻面和 2 个轴可以容纳 4 个因素。然后有 2 个指标xy:一个选项是气泡图,两个指标都通过颜色或形状或两者进行区分(添加抖动以减少形状重叠):

testm = melt(test, id=c('siteloc', 'modeltype', 'mth', 'yr'))

# by color
ggplot(testm, aes(x=siteloc, y=modeltype, size=value, colour=variable)) +
  geom_point(shape=21, position="jitter") +
  facet_grid(mth~yr) +
  scale_size_area(max_size=40) +
  scale_shape(solid=FALSE) +
  theme_bw()

以颜色区分的指标

#by shape
testm$shape = as.factor(with(testm, ifelse(variable=='x', 21, 25)))

ggplot(testm, aes(x=siteloc, y=modeltype, size=value, shape=shape)) +
  geom_point(position="jitter") +
  facet_grid(mth~yr) +
  scale_size_area(max_size=40) +
  scale_shape(solid=FALSE) +
  theme_bw() 

在此处输入图像描述

# by shape and color
ggplot(testm, aes(x=siteloc, y=modeltype, size=value, colour=variable, shape=shape)) +
  geom_point(position="jitter") +
  facet_grid(mth~yr) +
  scale_size_area(max_size=40) +
  scale_shape(solid=FALSE) +
  theme_bw()

在此处输入图像描述

更新:

这是基于 Dominik 的第一条评论的尝试,以显示 (x,y) 是否高于或低于 1:1 线,以及x/yy/x的比率有多大- 如果 x/y>1,红色三角形是蓝色三角形否则圈子(melt在这种情况下不需要):

test$shape = as.factor(with(test, ifelse(x/y>1, 25, 21)))
test$ratio = with(test, ifelse(x/y>1, x/y, y/x))

ggplot(test, aes(x=siteloc, y=modeltype, size=ratio, colour=shape, shape=shape)) +
  geom_point() +
  facet_grid(mth~yr) +
  scale_size_area(max_size=40) +
  scale_shape(solid=FALSE) +
  theme_bw()

在此处输入图像描述

于 2013-07-28T05:33:05.693 回答
3

您可以使用空心点和实心点,但只能使用本答案中描述的某些形状

因此,这让您拥有fillcolourshapealpha作为您的美学映射。它看起来很丑,但它是:

ggplot(test, aes(x, y,
                 fill=modeltype,
                 shape=siteloc,
                 colour=mth,
                 alpha=factor(yr)
                 )) + 
geom_point(size = 4) + 
scale_shape_manual(values=21:25) +
scale_alpha_manual(values=c(0.35,1))

丑陋,但我想这是你要求的。(我没有费心去弄清楚图例发生了什么——它显然没有正确显示边界。)

丑陋的

如果你想将一个变量映射到一种自定义美学(空心和实心),你必须走得更远一点:

test$fill.type<-ifelse(test$yr==2010,'other',as.character(test$mth))
cols<-c('red','green','blue')

ggplot(test, aes(x, y,
                 shape=modeltype,
                 alpha=siteloc,
                 colour=mth,
                 fill=fill.type
)) + 
  geom_point(size = 10) + 
  scale_shape_manual(values=21:25) +
  scale_alpha_manual(values=c(1,0.5)) +
  scale_colour_manual(values=cols) +
  scale_fill_manual(values=c(cols,NA))

还是丑

仍然丑陋,但它的工作原理。如果是 2010 年和如果不是,我不知道将两者映射yr到一种颜色的更简洁的方法mth;如果有人向我展示了一种更清洁的方法,我会很高兴。现在指南(图例)完全错误,但您可以手动修复它。

于 2013-07-28T00:51:32.657 回答