混合折线图和R图基线中的点
我在R中构建了一个包含两个系列的图表,但我想在图表底部添加一个彩色条: 要绘制的数据如下所示混合折线图和R图基线中的点,r,charts,ggplot2,draw,point,R,Charts,Ggplot2,Draw,Point,我在R中构建了一个包含两个系列的图表,但我想在图表底部添加一个彩色条: 要绘制的数据如下所示 2013-01-01 12:35:00 0 2013-01-01 12:45:00 1 2013-01-01 13:00:00 1 .... 2013-01-01 13:00:00 2 其中0为绿色,1为橙色,2为红色。Datetime与原始图表中的数据X对齐 这是图表的代码(不带彩色条): 底部条形图的数据为: datetime
2013-01-01 12:35:00 0
2013-01-01 12:45:00 1
2013-01-01 13:00:00 1
....
2013-01-01 13:00:00 2
其中0为绿色,1为橙色,2为红色。Datetime与原始图表中的数据X对齐
这是图表的代码(不带彩色条):
底部条形图的数据为:
datetime DPV
2013-01-01 12:35:00 0
2013-01-01 12:45:00 1
2013-01-01 13:00:00 1
....
2013-01-01 13:00:00 2
更好!!我已经更改了数据,现在我有:
datetime,temp_int,hum_int,dpv
"2014-02-15 00:00:00",67.2,13.6,"red"
"2014-02-15 00:15:00",63.4,13.8,"yellow"
"2014-02-15 00:30:00",61.2,14.2,"green"
"2014-02-15 00:45:00",60.4,14.5,"green"
....
没有实际数据很难回答,但这里有一些想法 制作了一些样本数据,包括用于颜色栏的
x
值、temp
行值和id
值
set.seed(1)
df<-data.frame(x=1:100,temp=runif(100,10,50),id=sample(1:3,100,replace=TRUE))
另一种可能是将geom_bar()
与stat=“identity”
一起使用,并设置所需条形的y
值高度。使用参数width=
可以更改条的宽度,以确保条之间没有空格
ggplot(df,aes(x))+geom_line(aes(y=temp))+
geom_bar(aes(y=4,fill=factor(id)),stat="identity",width=1)
更新-使用OP数据的解决方案
所提供的有关数据
df<-read.table(text="datetime,temp_int,hum_int,dpv
2014-02-15 00:00:00,67.2,13.6,red
2014-02-15 00:15:00,63.4,13.8,yellow
2014-02-15 00:30:00,61.2,14.2,green
2014-02-15 00:45:00,60.4,14.5,green",header=T,sep=",")
你好!我已经编辑了问题,包括行和底部栏的数据。您好,我再次更改了数据,现在有了最后一个数据集。@ManuParra用您的数据更新了我的答案。
ggplot(df,aes(x))+geom_line(aes(y=temp))+
geom_tile(aes(y=0,fill=factor(id)))
ggplot(df,aes(x))+geom_line(aes(y=temp))+
geom_bar(aes(y=4,fill=factor(id)),stat="identity",width=1)
df<-read.table(text="datetime,temp_int,hum_int,dpv
2014-02-15 00:00:00,67.2,13.6,red
2014-02-15 00:15:00,63.4,13.8,yellow
2014-02-15 00:30:00,61.2,14.2,green
2014-02-15 00:45:00,60.4,14.5,green",header=T,sep=",")
df$datetime <- as.POSIXct(df$datetime)
library(reshape2)
df.melt<-melt(df,id.vars=c("datetime","dpv"))
ggplot(df.melt, aes(x = datetime, y = value)) +
geom_line(aes(colour = variable),size=1.9) +
geom_bar(aes(y=4,fill=dpv),stat="identity",width=900)+
scale_fill_identity()