2017-05-31 69 views
0

我有一個有4個組(條1 + 2,條3 + 4等)的情節,但在數據集中沒有任何跡象表明。如何手動添加空間,添加共享組標籤(x軸)並重新着色4組?手動分組Barplot索引

enter image description here

數據(熔化):

enter image description here

CURENT Ggplot代碼:

xdr<-melt(result) 

ggplot(
    aes(x = variable, y = value), data = xdr) + 
    stat_summary(fun.y = "mean", geom = "bar") + 
    coord_cartesian(ylim=c(0.6,0.85)) + 
    stat_summary(fun.y = mean, geom = "bar") + 
    stat_summary(fun.data = mean_se, geom = "errorbar") 
+1

「有一個在數據集中沒有的這個指示」:你能添加一個?你知道這些組是什麼。一旦將數據記錄在數據中,則可以更容易地在一個陰謀中表示該分組。 – Marius

回答

1

我想你會想用mutate()添加的基團中的任何方式適合你的數據,然後facet_wrap()使你所談論的那種子圖g左右。

library(tidyverse) 

df <- tribble(
    ~variable, ~value, 
    "baseline1", 0.730, 
    "baseline2", 0.521, 
    "baseline3", 0.762, 
    "baseline4", 0.655, 
    "baseline5", 0.604, 
    "baseline6", 0.710, 
    "baseline7", 0.528, 
    "baseline8", 0.172 
) 

df %>% 
    mutate(group = (row_number() + 1) %/% 2, 
     group = paste("Group", group)) %>% 
    ggplot(aes(variable, value, fill = group)) + 
    geom_col(show.legend = FALSE) + 
    facet_wrap(~group, nrow = 1, scales = "free_x")