以日期作为 r 中绘制值的浮动条形图
Floating bar chart with dates as the plotted values in r
我想绘制日期范围,其中特定对象是"active",如下所示:
这个例子来自Wikipedia。这是数据:
df1 <- data.frame(
name = c("Eazy-E", "DJ Yella", "Dr. Dre",
"Ice Cube", "Arabian Prince", "MC Ren"),
from = as.Date(c("01/01/1986", "01/01/1986", "01/01/1986",
"01/01/1986", "01/01/1986","02/01/1988"), "%m/%d/%Y"),
to = as.Date(c("08/01/1991", "08/01/1991", "07/01/1991",
"12/01/1989", "07/01/1988", "08/01/1991"),"%m/%d/%Y"))
df1
name from to
1 Eazy-E 1986-01-01 1991-08-01
2 DJ Yella 1986-01-01 1991-08-01
3 Dr. Dre 1986-01-01 1991-07-01
4 Ice Cube 1986-01-01 1989-12-01
5 Arabian Prince 1986-01-01 1988-07-01
6 MC Ren 1988-02-01 1991-08-01
我不知道如何:
- 绘制日期值
- 制作浮动条形图
感谢任何意见。
它本身似乎不是条形图,更像是线条或矩形的集合。因此,可以使用 lines()
、rect()
甚至 polygon()
来解决这个问题。我会展示第一个,希望你能明白要点。
您的数据:
dat <- structure(list(name = c("Eazy-E", "DJ Yella", "Dr. Dre", "Ice Cube", "Arabian Prince", "MC Ren"),
from = c("1986-01-01", "1986-01-01", "1986-01-01", "1986-01-01", "1986-01-01", "1988-02-01"),
to = c("1991-08-01", "1991-08-01", "1991-07-01", "1989-12-01", "1988-07-01", "1991-08-01")),
.Names = c("name", "from", "to"), row.names = c(NA, -6L), class = "data.frame")
dat$from <- as.POSIXct(dat$from)
dat$to <- as.POSIXct(dat$to)
dat
## name from to
## 1 Eazy-E 1986-01-01 1991-08-01
## 2 DJ Yella 1986-01-01 1991-08-01
## 3 Dr. Dre 1986-01-01 1991-07-01
## 4 Ice Cube 1986-01-01 1989-12-01
## 5 Arabian Prince 1986-01-01 1988-07-01
## 6 MC Ren 1988-02-01 1991-08-01
一个解决方案:
par(mar=c(2,8,0,0) + 0.1)
plot(0, type='n', xlim=range(c(dat$from, dat$to)), ylim=c(1, nrow(dat)),
main='', xlab='', ylab='', xaxt='n', yaxt='n')
years <- seq.POSIXt(min(dat$from), max(dat$to), by='1 year')
abline(v=years, lty=3, col='gray')
axis(1, at=years, labels=format(years, '%Y'))
axis(2, at=1:nrow(dat), labels=rev(dat$name), las=2)
lines(x=as.POSIXct(c(apply(dat[,c('from','to')], 1, c, NA))),
y=rep(nrow(dat):1, each=3),
lwd=5)
增加lwd
以获得更粗的线条。如果您想对其进行更多控制,请考虑以相同的方式使用 rect
。
这是一种使用 barchart
的方法。首先,绘制一个条形图,其中条形长度由结束日期给出,然后用白色条形图绘制,条形长度由开始日期给出。使用 axis.date
添加 x 轴标签。
par(mar=c(5, 6, 2, 2))
b <- barplot(as.numeric(df$to), horiz=TRUE, border='transparent',
xlim=range(c(df$from, df$to)), xaxt='n', yaxs='i',
space=1)
barplot(as.numeric(df$from), horiz=TRUE, space=1, add=TRUE, col='white',
border='transparent', xaxt='n', names.arg=df$name, las=1,
cex.names=0.8)
axis(2, at=b[, 1], labels=FALSE, tick=FALSE)
box(bty='l')
axis.Date(1, pretty(df$from), cex.axis=0.8)
我想绘制日期范围,其中特定对象是"active",如下所示:
这个例子来自Wikipedia。这是数据:
df1 <- data.frame(
name = c("Eazy-E", "DJ Yella", "Dr. Dre",
"Ice Cube", "Arabian Prince", "MC Ren"),
from = as.Date(c("01/01/1986", "01/01/1986", "01/01/1986",
"01/01/1986", "01/01/1986","02/01/1988"), "%m/%d/%Y"),
to = as.Date(c("08/01/1991", "08/01/1991", "07/01/1991",
"12/01/1989", "07/01/1988", "08/01/1991"),"%m/%d/%Y"))
df1
name from to
1 Eazy-E 1986-01-01 1991-08-01
2 DJ Yella 1986-01-01 1991-08-01
3 Dr. Dre 1986-01-01 1991-07-01
4 Ice Cube 1986-01-01 1989-12-01
5 Arabian Prince 1986-01-01 1988-07-01
6 MC Ren 1988-02-01 1991-08-01
我不知道如何:
- 绘制日期值
- 制作浮动条形图
感谢任何意见。
它本身似乎不是条形图,更像是线条或矩形的集合。因此,可以使用 lines()
、rect()
甚至 polygon()
来解决这个问题。我会展示第一个,希望你能明白要点。
您的数据:
dat <- structure(list(name = c("Eazy-E", "DJ Yella", "Dr. Dre", "Ice Cube", "Arabian Prince", "MC Ren"),
from = c("1986-01-01", "1986-01-01", "1986-01-01", "1986-01-01", "1986-01-01", "1988-02-01"),
to = c("1991-08-01", "1991-08-01", "1991-07-01", "1989-12-01", "1988-07-01", "1991-08-01")),
.Names = c("name", "from", "to"), row.names = c(NA, -6L), class = "data.frame")
dat$from <- as.POSIXct(dat$from)
dat$to <- as.POSIXct(dat$to)
dat
## name from to
## 1 Eazy-E 1986-01-01 1991-08-01
## 2 DJ Yella 1986-01-01 1991-08-01
## 3 Dr. Dre 1986-01-01 1991-07-01
## 4 Ice Cube 1986-01-01 1989-12-01
## 5 Arabian Prince 1986-01-01 1988-07-01
## 6 MC Ren 1988-02-01 1991-08-01
一个解决方案:
par(mar=c(2,8,0,0) + 0.1)
plot(0, type='n', xlim=range(c(dat$from, dat$to)), ylim=c(1, nrow(dat)),
main='', xlab='', ylab='', xaxt='n', yaxt='n')
years <- seq.POSIXt(min(dat$from), max(dat$to), by='1 year')
abline(v=years, lty=3, col='gray')
axis(1, at=years, labels=format(years, '%Y'))
axis(2, at=1:nrow(dat), labels=rev(dat$name), las=2)
lines(x=as.POSIXct(c(apply(dat[,c('from','to')], 1, c, NA))),
y=rep(nrow(dat):1, each=3),
lwd=5)
增加lwd
以获得更粗的线条。如果您想对其进行更多控制,请考虑以相同的方式使用 rect
。
这是一种使用 barchart
的方法。首先,绘制一个条形图,其中条形长度由结束日期给出,然后用白色条形图绘制,条形长度由开始日期给出。使用 axis.date
添加 x 轴标签。
par(mar=c(5, 6, 2, 2))
b <- barplot(as.numeric(df$to), horiz=TRUE, border='transparent',
xlim=range(c(df$from, df$to)), xaxt='n', yaxs='i',
space=1)
barplot(as.numeric(df$from), horiz=TRUE, space=1, add=TRUE, col='white',
border='transparent', xaxt='n', names.arg=df$name, las=1,
cex.names=0.8)
axis(2, at=b[, 1], labels=FALSE, tick=FALSE)
box(bty='l')
axis.Date(1, pretty(df$from), cex.axis=0.8)