Reputation: 13
need help: My original df has 50 vars: 49 POSIXct class and 1 categorical outcome var. I want to plot (ggplot) the 49 vars to explore its time distribution as a scatterplot. I want to put the plots into a grid of plots. The trouble is getting the corresponding plot to var name correct. My solution below seems longwinded.
```{r}
library(grid)
library(ggplot2)
library(gridExtra)
str(a[c(1:4,50)])
```
'data.frame': 48 obs. of 4 variables:
$ admd : POSIXct, format: "2017-09-23 12:00:00" "2017-09-23 10:31:00" ...
$ feverd: POSIXct, format: "2017-09-23 12:00:00" "2017-09-23 12:00:00" ...
$ defd : POSIXct, format: "2017-09-23 16:00:00" "2017-09-23 12:13:00" ...
$ ns1d : POSIXct, format: NA "2017-09-23 10:13:00" ...
$ outcome: Factor w/ 2 levels "Death","Discharge": 2 1 2 2 2 2 2 2 2 2 ...
I want to plot these: intended results/plots: each plot has its corresponding variable name
What I did isn't polished at all:
#plot_fun2
```{r}
plot_fun<- function(df){
ggplot(df, aes_string(x=df[[1]], y=rnorm(1:48))) +
geom_point(aes(col=a$outcome), alpha=0.5, cex=3)+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
xlab("Time") + ylab("RandomVisualValue")+
scale_colour_manual(name="Outcome",values=c("#FF420E","#89DA59")) +
ggtitle(names(df)[[1]])
}
```
```{r}
sample_list<-list(
data.frame(a[1]), data.frame(a[2]), data.frame(a[3]), data.frame(a[4])) #CAN WE SIMPLIFY THIS? my original data frame has 49 vars!
timedistb<-lapply(sample_list, plot_fun)
do.call(grid.arrange, c(timedistb, ncol=2, nrow=2))
```
Snippet of data: https://github.com/dcicantab5/recover-study/blob/master/c.csv
Upvotes: 0
Views: 251
Reputation: 7568
Don't really understand what the random number y-axis is about, but you may want to try some combination of gather
and facet_wrap
:
gather
: gathers the date time variables in one column datevar
that stores column names and one column dateval
that stores the respective dates (from each row). facet_wrap
: creates facets for each column name in datevar
, where each facets only contains data from the respective date time variables (previous column names of date time variables)Example code:
library(dplyr)
library(tidyr)
library(ggplot2)
df <- read.csv("https://raw.githubusercontent.com/dcicantab5/recover-study/master/c.csv")
df <- df %>%
gather(datevar, dateval, -X, -outcome) %>%
mutate(rnum = rnorm(1))
ggplot(df, aes(x=dateval, y=rnum)) +
geom_point(aes(col=outcome), alpha=0.5, cex=3)+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
xlab("Time") + ylab("RandomVisualValue")+
scale_colour_manual(name="Outcome",values=c("#FF420E","#89DA59")) +
facet_wrap(~datevar)
Upvotes: 1