所以你需要重塑数据。
您想要一个堆叠条形图,因此您需要告诉 ggplot 关于变量 1S、2S ...
和测试。
#let's melt the data
#library(reshape2)
data.plot.m <-melt(data.plot, id.vars = "tests") #I stored your data in data.plot
data.plot.m$variable <-gsub("X","",data.plot.m$variable)
#as R doesn't like variable names beginning with numbers,
#it adds an 'X' automatically when
#we load the data with read.table so we remove this from melted data
#now we plot the data
ggplot(data.plot.m,aes(y = value,x = variable,fill = tests)) +
geom_bar(stat = "identity")
You will notice the order of the plots are different.
We will need to reorder your variable:
data.plot.m$variable <- factor(data.plot.m$variable, levels = unique(data.plot.m$variable))
#now plot again
ggplot(data.plot.m,aes(y = value,x = variable,fill = tests))+
geom_bar(stat = "identity")
我刚刚意识到你想要这个
ggplot(data.plot.m,aes(y=value,x=tests,fill=variable))+geom_bar(stat="identity")
并旋转 x 轴刻度标签
ggplot(data.plot.m,aes(y=value,x=tests,fill=variable))+geom_bar(stat="identity") + theme(axis.text.x = element_text(angle=90))
Note how I switched x and fill