在ggplot中绘制多条线 [英] plot multiple lines in ggplot
本文介绍了在ggplot中绘制多条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用ggplot绘制不同日期的每小时数据,这是我的数据集:
I need to plot hourly data for different days using ggplot, and here is my dataset:
数据包括每小时的观测值,我想将每天的观测值绘制到单独的一行中.
The data consists of hourly observations, and I want to plot each day's observation into one separate line.
这是我的代码
xbj1 = bj[c(1:24),c(1,6)]
xbj2 = bj [c(24:47),c(1,6)]
xbj3 = bj [c(48:71),c(1,6)]
ggplot()+
geom_line(data = xbj1,aes(x = Date, y= Value), colour="blue") +
geom_line(data = xbj2,aes(x = Date, y= Value), colour = "grey") +
geom_line(data = xbj3,aes(x = Date, y= Value), colour = "green") +
xlab('Hour') +
ylab('PM2.5')
请对此提供建议.
推荐答案
我会先制作一些假数据(我不会尝试抄录您的数据)
I'll make some fake data (I won't try to transcribe yours) first:
set.seed(2)
x <- data.frame(
Date = rep(Sys.Date() + 0:1, each = 24),
# Year, Month, Day ... are not used here
Hour = rep(0:23, times = 2),
Value = sample(1e2, size = 48, replace = TRUE)
)
这是直接的 ggplot2
图:
library(ggplot2)
ggplot(x) +
geom_line(aes(Hour, Value, color = as.factor(Date))) +
scale_color_discrete(name = "Date")
ggplot(x) +
geom_line(aes(Hour, Value)) +
facet_grid(Date ~ .)
我强烈建议您找到有关 ggplot2
的良好教程,例如 http://www.cookbook-r.com/Graphs/.其他的也存在,很多都很好.
I highly recommend you find good tutorials for ggplot2
, such as http://www.cookbook-r.com/Graphs/. Others exist, many quite good.
这篇关于在ggplot中绘制多条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文