2017-10-02 4 views
1
DMU Name trip rate time sline distan wait gate 
Sadang  0.00 0.00 0.00 0.00 0.00 0.00 0.00 
Jamsil  0.00 0.10 0.15 0.31 0.04 0.29 0.20 
Silim  0.00 0.00 0.00 0.00 0.00 0.00 0.00 
Guro  0.00 0.00 0.00 0.00 0.00 0.00 0.00 
Suwon  0.00 0.00 0.00 0.00 0.00 0.00 0.00 
Suyu  0.06 0.26 0.21 0.23 0.12 0.34 0.30 

Ich habe Daten wie oben, und ich möchte ein gestapeltes Balkendiagramm plotten.gestapelt Balkendiagramm mit Ggplot2 Bibliothek in R

Ich habe versucht, es so zu tun:

##graph 
gr <- read.csv("graph.csv",header=TRUE, sep=",") 

library(tidyr) 
#convert into long format 
df2<-gather(gr, key=Input, value, -DMU.Name) 

library(ggplot2) 


ggplot(df2, aes(DMU.Name)) +geom_bar(aes(weight= value, fill = Input))+ 
theme(axis.text.x = element_text(angle=60, hjust=1)) + 
scale_x_discrete(limits = c("Sadang","Jamsil","Silim","Guro Digital Complex", 
"Suwon","Sindorim","Suyu","Gangnam","Seoul Nat'l Univ.", 
"Yangjae","Bucheon","Gangbyeon","Seoul","Hongik Univ.","Bupyeong", 
"Yeongdeungpo","Sinchon","Samsung","Gasan Digital Complex","Konkuk University", 
"Univ. of Education","Express Bus Terminal", "Seolleung", "Apgujeong","Gwanghwamun", 
"Hyehwa","Euljiro 1(il)-ga", "Yeonsinnae","Jonggak","Yeoksam","Chungmuro","Myeong-dong")) + 
    scale_fill_discrete(limits = c ("trip","rate","time","sline","distan","wait","gate","lines","stops","allocation")) 

Allerdings habe ich die Bar Farbsatz ändern möchten.

Wie versuche ich zu ??

Dank enter image description here

+0

Könnten Sie ein reproduzierbares Beispiel sowie eine detailliertere Beschreibung des Problems zur Verfügung stellen? Warum sollten Sie erwarten, dass in dem von Ihnen bereitgestellten Codebeispiel irgendetwas gestapelt wird? – PejoPhylo

+0

Für einen Barplot müssen Sie entweder eine y-Variable und stat = "identity" 'oder" stat = "count" ' – Mako212

Antwort

0

Hier ist eine schnelle Lösung:

df<-read.table(header = TRUE, text= 
"DMUName trip rate time sline distan wait gate 
Sadang  0.00 0.00 0.00 0.00 0.00 0.00 0.00 
Jamsil  0.00 0.10 0.15 0.31 0.04 0.29 0.20 
Silim  0.00 0.00 0.00 0.00 0.00 0.00 0.00 
Guro  0.00 0.00 0.00 0.00 0.00 0.00 0.00 
Suwon  0.00 0.00 0.00 0.00 0.00 0.00 0.00 
Suyu  0.06 0.26 0.21 0.23 0.12 0.34 0.30") 

library(tidyr) 
#convert into long format 
df2<-gather(df, key=Name, value, -DMUName) 

library(ggplot2) 
#plot 
g<-ggplot(df2, aes(DMUName)) +geom_bar(aes(weight= value, fill =Name)) 
print(g) 
+0

Vielen Dank Sir !!!!! Hab einen schönen Tag !!! –

Verwandte Themen