Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用par(新=真)覆盖两个几何图形,就像两个条形图一样_R_Ggplot2_Bar Chart - Fatal编程技术网

用par(新=真)覆盖两个几何图形,就像两个条形图一样

用par(新=真)覆盖两个几何图形,就像两个条形图一样,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我目前有一个类似的图: sourceTable<-"dateValues;total;inHospital 2014-01-01; 80; 20 2014-01-02; 90; 15 2014-01-03; 98; 16 2014-01-04; 98; 17 2014-01-05; 100; 28 2014-01-06; 110; 28 2014-01-07; 122; 30 2014-01-08; 110; 25 2014-01-09; 100; 22 2014-01-00; 90; 2

我目前有一个类似的图:

sourceTable<-"dateValues;total;inHospital
2014-01-01; 80; 20
2014-01-02; 90; 15
2014-01-03; 98; 16
2014-01-04; 98; 17
2014-01-05; 100; 28
2014-01-06; 110; 28
2014-01-07; 122; 30
2014-01-08; 110; 25
2014-01-09; 100; 22
2014-01-00; 90; 20
2014-01-11; 90; 15
2014-01-12; 80; 13
2014-01-13; 82; 10"

timetable<-read.table(textConnection(sourceTable), sep=";", head=T, dec=",")
barplot(timetable$total, border="red", axes=FALSE, ylim=c(0, max(timetable$total)))
par(new=TRUE)
barplot(timetable$inHospital, border="blue", axes=FALSE, ylim=c(0, max(timetable$total)))
如何将第二个ggplot覆盖在第一个ggplot上,即:

ggplot(timetable, aes(x=dateValues, y=inHospital))
  +geom_bar(stat="identity", fill="red", colour="blue")
试试这个:

library(ggplot2)

ggplot(data = timetable, aes(x = dateValues, y = total)) + 
  geom_bar(stat = "identity", fill = "grey", colour = "red")+
  geom_bar(data = timetable, aes(x = dateValues, y = inHospital),
           stat = "identity", fill = "grey", colour = "blue")


编辑: 备选方案-正确的方法-打印前转换数据:

library(reshape2)
library(ggplot2)

# transform the data - melt
timetable$outHospital <- timetable$total - timetable$inHospital
df <- melt(timetable, id = c("dateValues", "total"))

# plot in one go
ggplot(data = df, aes(x = dateValues, y = value, fill = variable)) + 
  geom_bar(stat = "identity")
library(重塑2)
图书馆(GG2)
#转换数据-熔化
门诊时间表
library(reshape2)
library(ggplot2)

# transform the data - melt
timetable$outHospital <- timetable$total - timetable$inHospital
df <- melt(timetable, id = c("dateValues", "total"))

# plot in one go
ggplot(data = df, aes(x = dateValues, y = value, fill = variable)) + 
  geom_bar(stat = "identity")