R- 具有特定变量的 Plot 图
R- Plot graph with certain variable
这是我的数据框的样子:
Persnr Date AmountHolidays
1 55312 X201101 2
2 55312 X201102 4.5
3 55312 X201103 5
etc.
我想要的是一个图表,显示特定人 (persnr) 每个时期(x 轴上的日期)的假期量(y 轴上)。基本上,它是 R 中的一个数据透视图。据我所知,创建这样的图是不可能的。
这是我想要的结果:
首先可以在 R 中创建这样的模型吗?如果没有,我在 R 中可视化此类图形的最佳方式是什么?
提前致谢。
这样的东西可以解决问题吗?
dat <- read.table(text="Persnr Date AmountHolidays
55312 2011-01-01 2
55312 2011-02-01 4.5
55312 2011-03-01 5
55313 2011-01-01 4
55313 2011-02-01 2.5
55313 2011-03-01 6", header=TRUE)
dat$Date <- as.POSIXct(dat$Date)
dat$Persnr <- as.factor(dat$Persnr)
# Build a primary graph
plot(AmountHolidays ~ Date, data = dat[dat$Persnr==55312,], type="l", col="red",
xlim = c(1293858000, 1299301200), ylim=c(0,8))
# Add additional lines to it
lines(AmountHolidays ~ Date, data = dat[dat$Persnr==55313,], type="l", col="blue")
# Build and place a legend
legend(x=as.POSIXct("2011-02-19"), y=2.2, legend = levels(dat$Persnr),fill = c("red", "blue"))
要设置 X 坐标,您可以使用 as.POSIXct(YYYY-MM-DD)
或 as.numeric(as.POSIXct(YYYY-MM-DD)
,就像我对 xlim 所做的那样。
你可以试试包 ggplot2
:
第一个选项
ggplot(dat, aes(x=Date, y=AmountHolidays, group=Persnr)) +
geom_line(aes(colour=Persnr)) + scale_colour_discrete()
或
第二个选项
ggplot(dat, aes(x=Date, y=AmountHolidays, group=Persnr)) +
geom_line() + facet_grid(~Persnr)
其中一个优点是您不需要为每个 Persnr 一行,甚至不需要指定(知道)Persnr 的名称或编号。
示例:
第一个选项
第二个选项
数据:
dat <- structure(list(Persnr = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("54000",
"55312"), class = "factor"), Date = structure(c(1L, 2L, 3L, 1L,
2L, 3L), .Label = c("2011-01-01", "2011-02-01", "2011-03-01"), class = "factor"),
AmountHolidays = c(5, 4.5, 2, 3, 6, 7)), .Names = c("Persnr",
"Date", "AmountHolidays"), row.names = c(3L, 5L, 6L, 1L, 2L,
4L), class = "data.frame")
这是我的数据框的样子:
Persnr Date AmountHolidays
1 55312 X201101 2
2 55312 X201102 4.5
3 55312 X201103 5
etc.
我想要的是一个图表,显示特定人 (persnr) 每个时期(x 轴上的日期)的假期量(y 轴上)。基本上,它是 R 中的一个数据透视图。据我所知,创建这样的图是不可能的。 这是我想要的结果:
首先可以在 R 中创建这样的模型吗?如果没有,我在 R 中可视化此类图形的最佳方式是什么? 提前致谢。
这样的东西可以解决问题吗?
dat <- read.table(text="Persnr Date AmountHolidays
55312 2011-01-01 2
55312 2011-02-01 4.5
55312 2011-03-01 5
55313 2011-01-01 4
55313 2011-02-01 2.5
55313 2011-03-01 6", header=TRUE)
dat$Date <- as.POSIXct(dat$Date)
dat$Persnr <- as.factor(dat$Persnr)
# Build a primary graph
plot(AmountHolidays ~ Date, data = dat[dat$Persnr==55312,], type="l", col="red",
xlim = c(1293858000, 1299301200), ylim=c(0,8))
# Add additional lines to it
lines(AmountHolidays ~ Date, data = dat[dat$Persnr==55313,], type="l", col="blue")
# Build and place a legend
legend(x=as.POSIXct("2011-02-19"), y=2.2, legend = levels(dat$Persnr),fill = c("red", "blue"))
要设置 X 坐标,您可以使用 as.POSIXct(YYYY-MM-DD)
或 as.numeric(as.POSIXct(YYYY-MM-DD)
,就像我对 xlim 所做的那样。
你可以试试包 ggplot2
:
第一个选项
ggplot(dat, aes(x=Date, y=AmountHolidays, group=Persnr)) +
geom_line(aes(colour=Persnr)) + scale_colour_discrete()
或
第二个选项
ggplot(dat, aes(x=Date, y=AmountHolidays, group=Persnr)) +
geom_line() + facet_grid(~Persnr)
其中一个优点是您不需要为每个 Persnr 一行,甚至不需要指定(知道)Persnr 的名称或编号。
示例:
第一个选项
数据:
dat <- structure(list(Persnr = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("54000",
"55312"), class = "factor"), Date = structure(c(1L, 2L, 3L, 1L,
2L, 3L), .Label = c("2011-01-01", "2011-02-01", "2011-03-01"), class = "factor"),
AmountHolidays = c(5, 4.5, 2, 3, 6, 7)), .Names = c("Persnr",
"Date", "AmountHolidays"), row.names = c(3L, 5L, 6L, 1L, 2L,
4L), class = "data.frame")