Model Output-Help with Plotting

Hello RStudio Community-

I am in a bit of a bind-- I have to fix this figure ASAP, and I am still on the R learning curve.

I am running a Bayesian mixing model. (simmr) The model runs great, and outputs are good.

One of the outputs is a simple box plot. It looks like it was generated from ggplot, but I cannot figure out how to change the title or the y axis limits (I want 0.0 to 1.0).

So the questions are: how do I manage the outputs from the model, and more importantly: how do I use ggplot to modify this plot?

Thank you.

The boxplot:

The reprexed code:

### Setup the model

well_simmr <- simmr_load(mixtures = as.matrix(targets[, 1:2]),
                        source_names = sources$Sources,
                        source_means = as.matrix(sources[,2:3]),
                        source_sds = as.matrix(sources[,4:5]),
                        #correction_means = as.matrix(tefs[,2:3]),
                        #correction_sds = as.matrix(tefs[,4:5]),
                        #concentration_means = as.matrix(concep[,2:3]),
                        group = targets$well)
#> Error in simmr_load(mixtures = as.matrix(targets[, 1:2]), source_names = sources$Sources, : could not find function "simmr_load"



### Run through simmr:

well_simmr_out <- simmr_mcmc(well_simmr)
#> Error in simmr_mcmc(well_simmr): could not find function "simmr_mcmc"

summary(well_simmr_out, type = 'diagonostics', group = 7)
#> Error in summary(well_simmr_out, type = "diagonostics", group = 7): object 'well_simmr_out' not found

### Group comparison plots

compare_groups(well_simmr_out, groups = 1:7, source_name = 'NH4 Fertilizer', plot = TRUE)
#> Error in compare_groups(well_simmr_out, groups = 1:7, source_name = "NH4 Fertilizer", : could not find function "compare_groups"

Created on 2021-09-08 by the reprex package (v2.0.1)

I couldn't run your reprex.. (it explicitly shows error in what you shared)
I couldnt even install simmr as it requires some .exe utility which I dont want to place on the computer im using.
But I looked at the source code for compare groups, and it uses a similar structure to returning a ggplot object as this:

function_that_makes_ggplots <- function(){
  Ordering <- 1
out_all <- "a"
p <- ggplot(iris,
               aes(x=Sepal.Length,y=Petal.Width,colour=Species)) + geom_point()
print(p)
    invisible(list(Ordering = Ordering, out_all = out_all, plot = p))
}

ret1 <- function_that_makes_ggplots()

so like I've done , capture the result of the compare_groups function to a name, like ret1.
ret1 will have 3 elements in a list. the element plot is your plot and you can manipulate it further with ggplot commands.

ret1$plot + scale_x_reverse() + ggtitle("my title")

Thank you for your rapid response.

I did not want anyone to have to install and run simmr, since, as you say it requires an exe utility.

I think your last reprex "ret1$plot" was the answer! (I did not know you could do this!)

So, all I needed to do was: extract (not sure if that is the correct term) with: "Plot1$plot"

Plot1$plot + scale_y_continuous(name = "Proportion", limits = c(0, 1.015)) +xlab("Well") + ggtitle("NH4/Urea")

Thanks again.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.