2016-12-15 69 views
1

我正試圖創建一個如下所示的圖。目前,我有一個數據包含4個標題(類型,值1,值2和總值)。我試圖實現的是,我想將類型列與輸出值1,值2和總值同時分類。帶三個y變量的ggplot R

我有下面的代碼,但它似乎不工作!請任何人都能指出我正確的方向。

df <- data.frame(Type = c("a", "b", "c", "d", "e", "f","g","h","i"), 
     Value1 = c(1, 32, 63, 94, 125, 156,187,218,249), 
     Value2 = c(125, 5, 125, 76, 3, 125,3,2,100), 
Total = c(126,37,188,170,128,281,190,220,349)) 

plot <- ggplot(df,aes(x = Type,y=c("Value1","Value2","Total"),fill = EVTYPE))+geom_bar(position="dodge") 

樣地是在這裏:

http://imgur.com/a/mZgTW

+1

把數據轉換爲長格式(在SO上有很多答案),並且只在y中寫入一個數據。 – Haboryme

+1

你也想要position =「stack」 – Nate

回答

3

你的數據在堆棧條形圖的格式不對,從reshape包來改變其格式爲可用的東西用melt功能。

library(ggplot2) 
library(reshape2) 

df <- data.frame(Type = c("a", "b", "c", "d", "e", "f","g","h","i"), 
      Value1 = c(1, 32, 63, 94, 125, 156,187,218,249), 
      Value2 = c(125, 5, 125, 76, 3, 125,3,2,100), 
      Total = c(126,37,188,170,128,281,190,220,349)) 

df.m <- melt(df,id.vars = "Type") 

plot <- ggplot(df.m, aes(x = Type, y = value,fill=variable)) + 
     geom_bar(stat='identity') 

這將產生以下圖表: enter image description here

2

是需要你的數據進行重組。這是你如何手動完成的。大概有幾十種不同的是做到這一點的R.

Type = c("a", "b", "c", "d", "e", "f","g","h","i") 
Value1 = c(1, 32, 63, 94, 125, 156,187,218,249) 
Value2 = c(125, 5, 125, 76, 3, 125,3,2,100) 
Total = c(126,37,188,170,128,281,190,220,349) 
Type<-rep(Type,3) 
Values<-c(Value1,Value2,Total) 
Groups<-c(rep("Value1",9),rep("Value2",9),rep("Total",9)) 

df<-data.frame(Type,Values,Groups 

ggplot(df, aes(x=Type,y=Values, fill=Groups)) + geom_bar(stat='identity') 

enter image description here

1

隨着dplyr/tidyr:

library(dplyr) 
library(tidyr) 
df %>% gather(Variable, Value, -Type) %>% ggplot(aes(Type, Value, fill=Variable)) + 
    geom_bar(stat='identity') + scale_fill_manual(values = c('gray', 'skyblue', 'orange')) 

enter image description here

2

你可能會用一條線更好繪製在這裏:

library(ggplot2) 
library(reshape2) 

theme_set(theme_bw()) 

f.m = melt(f, id.var="Type") 
f.m$variable = factor(f.m$variable, levels=c("Total", "Value1", "Value2")) 

ggplot(f.m, aes(x=Type, y=value, group=variable, colour=variable)) + 
    geom_line(aes(size=variable)) + 
    geom_point() + 
    scale_color_manual(values=c("black", hcl(c(15,195),100,65))) + 
    scale_size_manual(values=c(1,0.5,0.5)) 

enter image description here

,層疊具有Value1Value2沿Total是一種誤導,因爲Total是其他兩者的總和。如果您需要使用條形圖,則Value1Value2(排除Total)的堆積圖也會給出總數。

library(dplyr) 

ggplot(f.m %>% filter(variable != "Total") %>% 
     mutate(variable = factor(variable, c("Value2", "Value1"))), 
     aes(x=Type, y=value, fill=variable)) + 
    geom_bar(stat="identity") 

enter image description here

雖然我更喜歡線圖,我想可能有情況下,您會希望做這樣的事情:

ggplot() + 
    geom_bar(data=f.m %>% filter(variable=="Total"), 
      aes(x=Type, y=value, fill=variable), stat="identity", width=0.8) + 
    geom_bar(data=f.m %>% filter(variable != "Total"), 
      stat="identity", position="dodge", width=0.5, colour="black", 
      aes(x=Type, y=value, fill=variable)) + 
    scale_fill_manual(values=c("grey60", hcl(c(15,195),100,65))) 

enter image description here