R中带有ggplot2库的堆叠条形图

R中带有ggplot2库的堆叠条形图,r,ggplot2,graph,R,Ggplot2,Graph,我有一个如上所述的数据,我想画一个堆叠的条形图 我试着这样做: 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

我有一个如上所述的数据,我想画一个堆叠的条形图

我试着这样做:

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 
##图形

gr以下是一个快速解决方案:

##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以下是一个快速解决方案:

##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您能否提供一个可复制的示例以及对您的问题的更详细描述?具体来说,为什么您希望在您提供的代码示例中叠加任何内容?对于条形图,您需要指定y变量和
stat=“identity”
,或者
stat=“count”
您能否提供一个可复制的示例以及问题的更详细描述?具体来说,为什么您希望在您提供的代码示例中堆叠任何内容?对于条形图,您需要指定y变量和
stat=“identity”
,或者
stat=“count”