2016-07-26 134 views
1

我有一個包含五列和五行的數據框。數據幀是這樣的:具有多個y軸的分組箱

df <- data.frame(
    day=c("m","t","w","t","f"), 
    V1=c(5,10,20,15,20), 
    V2=c(0.1,0.2,0.6,0.5,0.8), 
    V3=c(120,100,110,120,100), 
    V4=c(1,10,6,8,8) 
) 

我想要做一些情節,所以我用了ggplot,特別是geom_bar

ggplot(df, aes(x = day, y = V1, group = 1)) + ylim(0,20)+ geom_bar(stat = "identity") 
ggplot(df, aes(x = day, y = V2, group = 1)) + ylim(0,1)+ geom_bar(stat = "identity") 
ggplot(df, aes(x = day, y = V3, group = 1)) + ylim(50,200)+ geom_bar(stat = "identity") 
ggplot(df, aes(x = day, y = V4, group = 1)) + ylim(0,15)+ geom_bar(stat = "identity") 

我的問題是,怎樣才能做一個分組ggplotgeom_bar與多個y軸?我想在X軸的一天,每天我想繪製四個垃圾桶V1,V2,V3,V4,但具有不同的範圍和顏色。那可能嗎?

編輯

我想y軸看起來像這樣:

multiple axis

+1

在'ggplot2'文檔例如如何爲繪製準備數據請看下圖: http://stackoverflow.com/documentation/r/1334/ggplot2#t=201607261933589547859。 – Alex

+0

'ggplot2'不支持多個y軸。如果你想這樣的事情會非常複雜(http://stackoverflow.com/questions/3099219/plot-with-2-y-axes-one-y-axis-on-the-left-and-another- y軸 - 上的右)。 – Alex

+0

閱讀上面鏈接問題的最後一個答案。這個答案來自ggplot2軟件包的作者。 – Alex

回答

1
require(reshape) 
data.m <- melt(df, id.vars='day') 
ggplot(data.m, aes(day, value)) + 
    geom_bar(aes(fill = variable), position = "dodge", stat="identity") + 
    facet_grid(variable ~ .) 

enter image description here

您還可以更改Y軸的限制,如果你喜歡(在這裏是一個example)。

或者你可能意味着分組如下:

require(reshape) 
data.m <- melt(df, id.vars='day') 
ggplot(data.m, aes(day, value)) + 
    geom_bar(aes(fill = variable), position = "dodge", stat="identity") 

enter image description here

對於後者的例子,如果你想2 Y軸,那麼你只用左Y軸創建情節兩次(一次和一旦有合適的Y軸),然後使用此功能:

double_axis_graph <- function(graf1,graf2){ 
    graf1 <- graf1 
    graf2 <- graf2 
    gtable1 <- ggplot_gtable(ggplot_build(graf1)) 
    gtable2 <- ggplot_gtable(ggplot_build(graf2)) 
    par <- c(subset(gtable1[['layout']], name=='panel', select=t:r)) 
    graf <- gtable_add_grob(gtable1, gtable2[['grobs']][[which(gtable2[['layout']][['name']]=='panel')]], 

          par['t'],par['l'],par['b'],par['r']) 
    ia <- which(gtable2[['layout']][['name']]=='axis-l') 
    ga <- gtable2[['grobs']][[ia]] 
    ax <- ga[['children']][[2]] 
    ax[['widths']] <- rev(ax[['widths']]) 
    ax[['grobs']] <- rev(ax[['grobs']]) 
    ax[['grobs']][[1]][['x']] <- ax[['grobs']][[1]][['x']] - unit(1,'npc') + unit(0.15,'cm') 
    graf <- gtable_add_cols(graf, gtable2[['widths']][gtable2[['layout']][ia, ][['l']]], length(graf[['widths']])-1) 
    graf <- gtable_add_grob(graf, ax, par['t'], length(graf[['widths']])-1, par['b']) 

    return(graf) 
} 

我相信也有,做同樣的事情,一個包或方便的功能。

+0

謝謝你的回答,特別是對於這個功能。 –

1

首先我重塑在問題下面的鏈接的文檔中描述。 一般ggplot不支持多個y軸。我認爲這是一個哲學的事情。但是也許刻面會適合你。

df <- read.table(text = "day V1 V2 V3 V4 
          m 5 0.1 120 1 
          t 10 0.2 100 10 
          w 2 0.6 110 6 
          t 15 0.5 120 8 
          f 20 0.8 100 8", header = TRUE) 

library(reshape2) 
df <- melt(df, id.vars = 'day') 

ggplot(df, aes(x = variable, y = value, fill = variable)) + geom_bar(stat = "identity") + facet_grid(.~day) 

enter image description here

1

如果我理解正確,您想在您的地塊中包含facets。您必須使用reshape2以正確的格式獲取數據。下面是與您的數據爲例:

df <- data.frame(
    day=c("m","t","w","t","f"), 
    V1=c(5,10,20,15,20), 
    V2=c(0.1,0.2,0.6,0.5,0.8), 
    V3=c(120,100,110,120,100), 
    V4=c(1,10,6,8,8) 
) 

library(reshape2) 

df <- melt(df, "day") 

然後用情節和包括facet_grid說法:

ggplot(df, aes(x=day, y=value)) + geom_bar(stat="identity", aes(fill=variable)) + 
    facet_grid(variable ~ .) 

enter image description here

+0

不要忘記顏色。 –

+0

@ Hack-R的答案更接近我的意思。我添加了一個想要繪製多軸的圖像。雖然我不知道如何與酒吧做到這一點。 –

+0

編輯爲包含顏色。是的,我現在看到您的示例圖像@ g.f.l – Warner