2

在 ggplot2 中,我想控制条形图或横线图中的变量和因子内的多个位置闪避。例如:

data = data.frame(Var=c("a","a","a","a","b","b","b","b"),
                Val=c(0.5,0.4,0.1,0.0,-0.1,-0.3,-0.0,0.1),
                g1=c(1,2,3,4,5,6,7,8),
                g2=c(1,2,3,4,1,2,3,4),
                g3=c(1,2,1,2,1,2,1,2))

q = ggplot(data,aes(Var,Val,ymin = Val-0.15,
                    ymax=Val+0.15,
                    group=g1,
                    colour=factor(g2),
                    fill=factor(g2),
                    linetype=factor(g3),))
q + geom_crossbar(width=0.5,fatten=2,size=0.5,position=position_dodge(width=0.51)) + 
    coord_flip() +  
    theme_bw() + 
    scale_fill_manual(values=c("#00000090","#00000090","#00000050","#00000050")) + 
    scale_colour_manual(values=c("#000000","#00000070","#000000","#00000070"))

在上面的示例中,我希望能够将较暗的条与较亮的条稍微分开,同时将每个条设置在一起。我可以实施多个位置闪避来实现这一点吗?任何帮助,将不胜感激。

4

2 回答 2

3

我建议使用facet_grid来模拟您想要的嵌套闪避。为此,我创建了一个新因子g4,将浅灰色和深灰色条分组。

library(ggplot2)

dat = data.frame(Var=c("a","a","a","a","b","b","b","b"),
                Val=c(0.5,0.4,0.1,0.0,-0.1,-0.3,-0.0,0.1),
                g1=c(1,2,3,4,5,6,7,8),
                g2=c(1,2,3,4,1,2,3,4),
                g3=c(1,2,1,2,1,2,1,2))

dat$g4 = c(1, 1, 2, 2, 1, 1, 2, 2) # New grouping factor

fill_values   = c("#00000090","#00000090","#00000050","#00000050")
colour_values = c("#000000","#00000070","#000000","#00000070")

#---------------------------------------------------------------------------
fig1 = ggplot(dat, aes(x=Var, y=Val, ymin=Val - 0.15, ymax=Val + 0.15, 
              colour=factor(g2), fill=factor(g2), linetype=factor(g3))) +
    geom_crossbar(width=0.5, fatten=2, size=0.5,
                  position=position_dodge(width=0.51)) +
    coord_flip() + 
    theme_bw() +
    scale_fill_manual(values=fill_values) +
    scale_colour_manual(values=colour_values) +
    opts(title="Figure 1: Original Version")

png("fig1.png", height=600, width=600)
print(fig1)
dev.off()

#---------------------------------------------------------------------------
fig2 = ggplot(dat, aes(x=factor(g4), y=Val, ymin=Val - 0.15,ymax=Val + 0.15, 
              colour=factor(g2), fill=factor(g2), linetype=factor(g3))) +
    geom_crossbar(width=0.7, fatten=2, size=0.5,
                  position=position_dodge()) +
    coord_flip() + 
    theme_bw() +
    scale_fill_manual(values=fill_values) +
    scale_colour_manual(values=colour_values) +
    facet_grid(Var ~ .) +
    opts(title="Figure 2: Proposed Solution")

png("fig2.png", height=600, width=600)
print(fig2)
dev.off()

图1 图 2

于 2012-06-21T00:35:50.840 回答
3

更新的代码和情节

这是一种在一个面板中将浅灰色与深灰色分开的方法。它涉及对数据进行子集化,以便在对 的两次调用中geom_crossbar,浅灰色的横线位于第一次调用中,然后深灰色的横线位于第二次调用中。我在数据框中添加了另一个分组变量,以允许对数据进行子集化。

library(ggplot2)
data = data.frame(Var=c("a","a","a","a","b","b","b","b"),
                Val=c(0.5,0.4,0.1,0.0,-0.1,-0.3,-0.0,0.1),
                g1=c(1,2,3,4,5,6,7,8),
                g2=c(1,2,3,4,1,2,3,4),
                g3=c(1,2,1,2,1,2,1,2))
data$g4 = c(1,1,2,2,1,1,2,2)

q = ggplot(data, aes(Var,Val,ymin = Val-0.15,
    ymax=Val+0.15, group=g1, colour=factor(g2),
    fill=factor(g2), linetype=factor(g3)))

# Position the light grey crossbars
q = q + geom_crossbar(data = subset(data, data$g4 == 1), 
        aes(as.numeric(Var) - .12, Val, ymin = Val-0.15, ymax = Val + 0.15), 
        width = 0.2, fatten = 1.25, size = 0.65, position = position_dodge(width = 0.2))

library(grid)
# position the dark grey crossbars, and tidy up
q + geom_crossbar(data = subset(data, data$g4 == 2), 
        aes(as.numeric(Var) + .12, Val, ymin = Val-0.15, ymax = Val + 0.15), 
        width = 0.2, fatten = 1.25, size = 0.65, position = position_dodge(width = 0.2)) + 
    scale_x_continuous(breaks = c(1, 2), labels = c("a", "b"), expand = c(.2, 0)) +
    scale_fill_manual(values = c("#00000057", "#00000057", "#00000020", "#00000020")) + 
    scale_colour_manual(values = c("#000000", "#00000070", "#000000", "#00000070")) +
    coord_flip() +  theme_bw() +
   theme(legend.key.size = unit(1.5, "cm"))

在此处输入图像描述

于 2012-06-21T21:26:59.440 回答