xpose
xpose copied to clipboard
order of stratified vpc plots
First of all, thanks for a great package! When generating vpc data based on non-PsN generated simulations the order of elements in column "strat" and the columns containing the stratification variables (both in data frame "vpc_dat" and "aggr_obs") seems to be based on data class character. This although the stratification variable in problem 1 and 2 are data class factor, with appropriate levels and ordering. This can results in unexpected ordering of vpc plots when stratification variable is for example 2, 5 and 10, where 10 then will be ordered before 2 and 5. If my understanding is correct, is it possible to not do this intermediate transformation of the stratification variables during vpc data generation or is there any obvious solution/feature that I have missed?
Hi @oskarrgh,
Thanks a lot for the feedback. Would you be able to PM me a reproducible example so I can run some tests on it?
In the meantime they are some yet not so well documented features of xpose to modify the vpc (i.e. special) data.
library(xpose)
# Create the VPC data
xpdb <- vpc_data(xpdb_ex_pk, stratify = 'SEX')
# You can extract the vpc data to take a look at its structure
vpc_special <- get_special(xpdb)
# Then you can modifiy the xpdb as you wish (e.g. remove/rename strata, etc).
xpdb %>%
# Change the sim shadded areas dataset
mutate(strat = as.numeric(strat),
.source = 'special',
.where = 'vpc_dat') %>%
# Change the obs lines dataset
mutate(strat = as.numeric(strat),
.source = 'special',
.where = 'aggr_obs') %>%
# Change the obs points dataset
mutate(SEX = as.numeric(SEX),
.source = 'special',
.where = 'obs') %>%
# Draw the plot
vpc()
Thanks for the fast reply @guiastrennec, I will get back to you asap with regards to a reproducible example. Meanwhile I can confirm that the suggested features did solved my issue by enabling transformation of the variable into a factor and setting the appropriate levels.
xpdb %>%
vpc_data(stratify=c("DOSE") %>%
mutate(variable = factor(variable, levels=c("2", "5", "10"), ordered=TRUE),
.source = "special",
.where = "vpc_dat") %>%
mutate(variable = factor(variable, levels=c("2", "5", "10"), ordered=TRUE),
.source = "special",
.where = "aggr_obs") %>%
mutate(variable = factor(variable, levels=c("2", "5", "10"), ordered=TRUE),
.source = "special",
.where = "obs") %>%
vpc(facets=c("variable"))
Would be good to have the feature documented as I failed to figure out how to do this prior to my first post. Still curious if it would be possible to retain the data class set in problem 1 and 2 when generating data for problem 3. I think that would convenient not to have to modify variables more than once. Keep up the good work!
@bguiastr, how to re-order of stratified vpc plots with 2 stratifiers (e.g., DAY and DOSE)? DAY has two values (1, 15), and DOSE has three values (60, 80, 120). I used the code below; however, the 120 dose is still shown in front of the 60 and 80 dose.
vpc_data=vpc_data(xpdb,psn_folder="@dir/@run_TAD_DAY_DOSE",psn_bins=TRUE,stratify=DAY~DOSE)
vpc_special <- get_special(vpc_data)
vpc_data=vpc_data%>% mutate(strat=factor(strat,levels=c('1, 60','1, 80','1, 120','15, 60','15, 80','15, 120'),ordered=TRUE), .source = 'special', .where = 'vpc_dat')%>% mutate(strat=factor(strat,levels=c('1, 60','1, 80','1, 120','15, 60','15, 80','15, 120'),ordered=TRUE), .source = 'special', .where = 'aggr_obs')%>% mutate(strat=factor(strat,levels=c('1, 60','1, 80','1, 120','15, 60','15, 80','15, 120'),ordered=TRUE), .source = 'special', .where = 'obs')
@JoannaPeng, the approach you used to relabel the factors should work you are probably close to the answer ! Two suggestions from my end:
-
You might find it easier to create a new variable in your dataset which would be taking values based on your
DAYandDOSEvariables. This allows you to stratify on 2 or more variable without difficulty and apply the code I provided in.
-
Beware that you need to edit the variable strat in
vpc_dat(summary of simulations), andaggr_obs(summary of observations) as well as your stratification variable (which might not be calledstratas I see in your example code) inobs(raw observations).
Hope it helps!
@bguiastr, thanks for your help. I figured out how to fix this problem. For my code above, I should mutate "DOSE", not "strat", as an ordered factor. It is because the plotting uses "DOSE" (and "DAY"), not "strat".
Glad it solved your problem !