Reputation: 111
This is what my dataframe looks like:
Persnr Date AmountHolidays
1 55312 X201101 2
2 55312 X201102 4.5
3 55312 X201103 5
etc.
What I want to have is a graph that shows the amount of holidays (on the y-axis) of each period (Date on the x-axis) of a specific person (persnr). Basically, it's a pivot graph in R. So far I know, it is not possible to create such a graph. Something like this is my desired result:
Is it possible in the first place to create such a model in R? If not, what is the best way for me to visualise such graph in R? Thanks in advance.
Upvotes: 3
Views: 91
Reputation: 24074
You can try with package ggplot2
:
First option
ggplot(dat, aes(x=Date, y=AmountHolidays, group=Persnr)) +
geom_line(aes(colour=Persnr)) + scale_colour_discrete()
or
Second option
ggplot(dat, aes(x=Date, y=AmountHolidays, group=Persnr)) +
geom_line() + facet_grid(~Persnr)
One of the advantages is that you don't need to have a line per Persnr or even to specify (to know) the name or number of Persnr.
example:
first option
second option
Data:
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")
Upvotes: 0
Reputation: 10421
Something like this could do the trick?
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"))
To set X coordinates, you can either use as.POSIXct(YYYY-MM-DD)
or as.numeric(as.POSIXct(YYYY-MM-DD)
as I did for the xlim's.
Upvotes: 1