道奇在ggplot中嵌套数据点

时间:2015-09-14 12:23:21

标签: r ggplot2

我正在绘制12个数据点,这些数据点嵌套在称为typetreatment的两个分组级别中。群组type包含选项manyfew,而群组treatment包含选项lowhigh。因此,四种组合中的每一种都会发生三次。

该图的代码如下(需要包ggplot2):

p20 <-
ggplot(data = test2, lty.est = 1) +
geom_point(data = subset(test2, treatment == 'low'), aes(class, value, shape = type,
                                                           group = type, colour=treatment),
         stat = 'identity',position = position_dodge(width=0.5)) +
geom_point(data = subset(test2, treatment == 'high'), aes(class, value, shape = type, 
                                                          group = type, colour=treatment),
         stat = 'identity', position = position_dodge(width=0.9)) +
scale_colour_manual(values=c("blue","red")) +
scale_shape_manual(values=c(16,17)) +
geom_errorbar(data = subset(test2, treatment == 'low'), aes(class, ymin=value-se, width = 0.2,
                                                              ymax=value+se, group = type, 
                                                              colour = treatment), stat = 'identity', 
            position=position_dodge(width=0.5)) +
 geom_errorbar(data = subset(test2, treatment == 'high'), aes(class,     ymin=value-se, width = 0.2,
                                                             ymax=value+se, group = type, 
                                                             colour = treatment), stat = 'identity', 
            position=position_dodge(width=0.9)) +
xlab("Class") +
ylab("Value") +
scale_y_continuous(expand=c(0.0,0.0),
                 limits=c(8.25, 10.25),
                 breaks=c(8.5,9,9.5,10),
                 labels=c("8.5","9","9.5","10")) +
scale_x_discrete(limits=c("one", "five", "ten"),
               labels=c("One", "Five", "Ten")) +
theme_bw() +
#  theme(legend.position=c(0.8,0.4)) +
theme(legend.title=element_blank()) +
theme(axis.title.x = element_text(vjust=0.1,face="bold", size=16),
    axis.text.x = element_text(vjust=0.1, size=14, angle=0)) +
theme(axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18),
    axis.text.y = element_text(size=14)) +
theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) +
theme(panel.border = element_rect(size=2, colour = "black", fill=NA, linetype=1)) +
theme(plot.margin = unit(c(0.3,0.4,0.28,0.0),"lines")) 

enter image description here

我想在当前数字中改变的是点的相对位置。例如,每个“类”的图的排列应该是(从左到右):很少高,很少低,很多高和很低。

有关如何相应调整代码的任何建议都将非常感激。

请查看以下数据:

> dput(test2)
structure(list(type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("few", "many"), class = "factor"), 
class = structure(c(2L, 2L, 1L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 
3L, 3L), .Label = c("five", "one", "ten"), class = "factor"), 
treatment = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L), .Label = c("high", "low"), class = "factor"), 
value = c(8.64, 8.78, 9.64, 9.87, 9.93, 9.99, 8.79, 8.93, 
9.69, 9.91, 9.98, 9.98), se = c(0.14, 0.13, 0.09, 0.05, 0.03, 
0.01, 0.13, 0.11, 0.08, 0.05, 0.02, 0.02)), .Names = c("type", 
"class", "treatment", "value", "se"), class = "data.frame", row.names =  c(NA, 
-12L))

2 个答案:

答案 0 :(得分:2)

您可以使用interaction功能实现此目的。用:

ggplot(data = test2) +
  geom_point(aes(class, value, shape = type, group = interaction(treatment,type), colour=treatment),
             stat = 'identity', position = position_dodge(width=0.5), size = 2.5) +
  geom_errorbar(aes(class, ymin=value-se, ymax=value+se, group = interaction(treatment,type), colour = treatment),
                stat = 'identity', position=position_dodge(width=0.5), width = 0.2) +
  labs(x="Class", y="Value") +
  scale_colour_manual(values=c("blue","red")) +
  scale_shape_manual(values=c(16,17)) +
  scale_x_discrete(limits=c("one", "five", "ten"), labels=c("One", "Five", "Ten")) +
  scale_y_continuous(expand=c(0.0,0.0), limits=c(8.25, 10.25), breaks=c(8.5,9,9.5,10), labels=c("8.5","9","9.5","10")) +
  theme_bw() +
  theme(legend.title=element_blank(),
        axis.title.x = element_text(vjust=0.1,face="bold", size=16),
        axis.text.x = element_text(vjust=0.1, size=14, angle=0),
        axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18),
        axis.text.y = element_text(size=14),
        panel.grid.minor=element_blank(), 
        panel.grid.major=element_blank(),
        panel.border = element_rect(size=2, colour = "black", fill=NA, linetype=1),
        plot.margin = unit(c(0.3,0.4,0.28,0.0),"lines"))

你得到:

enter image description here

另一种选择是预先创建交互变量:

test2$treattype <- factor(interaction(test2$treatment,test2$type),
                          labels = c("few high","few low","many high","many low"))

使用:

ggplot(data = test2) +
  geom_point(aes(class, value, shape = treattype, colour = treattype),
             stat = 'identity', position = position_dodge(width=0.5), size = 3) +
  geom_errorbar(aes(class, ymin=value-se, ymax=value+se, colour = treattype),
                stat = 'identity', position=position_dodge(width=0.5), width = 0.2) +
  labs(x="Class", y="Value") +
  scale_x_discrete(limits=c("one", "five", "ten"), labels=c("One", "Five", "Ten")) +
  scale_y_continuous(expand=c(0.0,0.0), limits=c(8.25, 10.25), breaks=c(8.5,9,9.5,10), labels=c("8.5","9","9.5","10")) +
  scale_colour_manual(values=c("blue","red","blue","red")) +
  scale_shape_manual(values=c(16,17,16,17)) +
  theme_bw() +
  theme(legend.title=element_blank(),
        axis.title.x = element_text(vjust=0.1,face="bold", size=16),
        axis.text.x = element_text(vjust=0.1, size=14, angle=0),
        axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18),
        axis.text.y = element_text(size=14),
        panel.grid.minor=element_blank(), 
        panel.grid.major=element_blank(),
        panel.border = element_rect(size=2, colour = "black", fill=NA, linetype=1),
        plot.margin = unit(c(0.3,0.4,0.28,0.0),"lines"))

你得到一个你只有一个传奇的情节:

enter image description here

以下选项是@jlhoward提供的解决方案的扩展,并且还处理了类“ten”的错误栏几乎不可读的事实,并且集成了第二个选项中的交互变量:

test2$class <- with(test2, factor(class, levels=unique(class)))

ggplot(test2, aes(x=type, y=value))+
  geom_point(aes(shape=treattype, color=treattype), position=position_dodge(width=0.5), size=3)+
  geom_errorbar(aes(ymin=value-se, ymax=value+se, shape=treattype, color=treattype),
                width=0.2, position=position_dodge(width=0.5))+
  scale_colour_manual(values=c("blue","red","darkgreen","brown")) +
  scale_shape_manual(values=c(16,17,16,17)) +
  facet_wrap(~class, scales="free_y")+
  theme_bw() +
  theme(legend.key = element_rect(colour=NA)) +
  guides(colour = guide_legend(title = "treatment x type",
                               override.aes = list(colour = c("blue","red","darkgreen","brown"),
                                                   shape = c(16,17,16,17))),
         shape = FALSE)

这导致以下情节:

enter image description here

答案 1 :(得分:1)

不完全是你问的问题,我知道,但我会强烈建议你考虑这方面的方面。它更容易混淆(你避免使用这两个传说)。

library(ggplot2)
test2$class <- with(test2,factor(class, levels=unique(class)))
ggplot(test2, aes(x=type, y=value, color=treatment))+
  geom_point(position=position_dodge(width=0.5))+
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=0.1, 
                position=position_dodge(width=0.5))+
  facet_wrap(~class)+
  theme_bw()