2017-09-15 129 views
1

这个阴谋的主要目标是使AB之间的比较三组,但我希望有onetwo,并且除了他们three,也是如此。使用下面的代码,我可以创建一个分组的barplot,这几乎是我想要的。但是由于传说非常难看,我需要在它下面的每个栏的名称。Labling酒吧分组barplot在ggplot

我该怎么办?

m.names <- c("A1","B1","one","A2","B2","two","A3","B3","three") 
m.group <- c(1,1,1,2,2,2,3,3,3) 
m.value <- c(5,10,1,20,15,2,10,20,3) 
df <- data.frame(m.names, m.group, m.value) 
df 


ggplot(df, aes(x = m.group, y = m.value)) + 
    geom_bar(aes(fill = m.names), position = "dodge", stat = "identity") + 
    scale_fill_manual(values=c("gray75", "gray75","gray75", "gray40","gray40","gray40", "blue", "red", "green")) 

enter image description here

回答

1

好像你可能希望这样的:

require(ggplot2) 

ggplot(df, aes(x = m.names, y = m.value)) + 
    geom_bar(aes(fill = m.names), stat = "identity") + 
    scale_fill_manual(values=c("gray75", "gray75","gray75", "gray40", 
          "gray40","gray40", "blue", "red", "green")) + 
    facet_grid(~m.group, scales = "free_x", space = "free_x") + 
    theme(strip.text.x = element_blank(), 
     panel.spacing = unit(0, "lines")) 

输出:

enter image description here

诀窍是在这里用m.names来代替m.groups。然后,我们可以通过m.group面向酒吧,让它们以您想要的方式呈现。

3

添加geom_text并确保它以同样的方式回避的酒吧:

# width = 0.9 should be the default for dodged bars but set 
# it explicitly to be sure 
dodger = position_dodge(width = 0.9) 
ggplot(df, aes(x = m.group, y = m.value)) + 
    geom_bar(aes(fill = m.names), position = dodger, stat = "identity") + 
    scale_fill_manual(values=c("gray75", "gray75","gray75", "gray40","gray40","gray40", "blue", "red", "green"), 
         guide = "none") + 
    geom_text(aes(x = m.group, group = m.names, label = m.names, y = 0), 
       position = dodger, 
       vjust = 1, colour = "black") 

enter image description here

+0

谢谢,这几乎是我想要的,是不是很容易摆脱组名,并将条形标签放在图下方? – havij

+1

如果这是你想要的,你可能会更好地看着Z Lin的回答(+我的评论)。 – Marius

3

刻面按组可针对这种情况以及工作:

fill.values = c("gray75", "gray75","gray75", 
       "gray40","gray40","gray40", 
       "blue", "red", "green") 
names(fill.values) = levels(df$m.names) 

> fill.values 
     A1  A2  A3  B1  B2  B3  one three  two 
"gray75" "gray75" "gray75" "gray40" "gray40" "gray40" "blue" "red" "green" 

ggplot(df, 
     aes(x = m.names, y = m.value, fill = m.names)) + 
    geom_col() + 
    scale_fill_manual(values = fill.values, guide = F) + 
    facet_wrap(~m.group, scales = "free_x") + 
    theme_bw() 

enter image description here

+1

'+ theme(strip.background = element_blank(),strip.text = element_blank())'这个答案看起来可能与OP想要的最接近。 – Marius

1

我们可以使用geom_label

dodger = position_dodge(width = 0.9) 
ggplot(df, aes(x = m.group, y = m.value)) + 
    geom_bar(aes(fill = m.names), position = dodger, stat = "identity") + 
    scale_fill_manual(values=c("gray75", "gray75","gray75", 
        "gray40","gray40","gray40", "blue", "red", "green"), 
         guide = "none") + 
    theme(axis.text.x=element_blank(), 
     axis.ticks.x=element_blank()) + 
    geom_label(aes(x = m.group, group = m.names, label = m.names, y = 0), 
       position = dodger, 
       vjust = 1, colour = "black") 

enter image description here

+0

有无论如何我可以把标签放在剧情之外?我的意思是低于酒吧,但在灰色地带之外 – havij