2016-12-15 13 views
1

私は以下のようなプロットを作成しようとしています。現在、4つのヘッダー(タイプ、値1、値2、合計値)を持つデータがあります。私が達成しようとしているのは、Type1列を出力Value1、Value2および合計値と共に同時に分類したいということです。ggplotと3つのy変数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には1つだけ入れる。 – 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.

dplyr/tidyrで
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

でこれを行うことだったでしょうがあります。

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)) 
Value2とともに Totalスタッキング

enter image description here

Totalは、他の2つの和であるため、誤解を招く恐れがあります。棒グラフを使用する必要がある場合は、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

関連する問題