簡體   English   中英

R中帶有ggplot2庫的堆疊條形圖

[英]stacked bar graph with ggplot2 library in R

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 

我有如上所述的數據,我想繪制一個堆積的條形圖。

我試圖這樣做:

##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"))

但是,我想更改條形顏色設置。

我該怎么做?

謝謝在這里輸入圖片說明

這是一個快速的解決方案:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM