2016-12-15 66 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