brianstock / MixSIAR

A framework for Bayesian mixing models in R:
http://brianstock.github.io/MixSIAR/
90 stars 75 forks source link

ggplot error when making isospace plot? #127

Open KGurney opened 6 years ago

KGurney commented 6 years ago

Good afternoon;

I am looking for some insight re: a problem I am having making my isospace plot.

Using the MixSIAR script version, I have successfully loaded mixture (n = 95 rows, 7 columns, including 2 tracers), source (n = 9 rows, 7 columns), and discrimination (n = 3 rows, 5 columns).

However, when I run the following script for the plot:

plot_data(filename="isospace_plot", plot_save_pdf=TRUE, plot_save_png=FALSE, mix,source,discr)

I get a message that says:

Error: Aesthetics must be either length 1 or the same as the data (8): size, linetype

My googling tells me that there is something going awry in the ggplot function, potentially related to issues with the legend. Unfortunately, I'm not familiar enough with R to tinker with it.

Any thoughts or suggestions as to how I might get this plot made would be much appreciated.

Thanks in advance.

kbg

sianglazier commented 6 years ago

Hi,

I'm not sure if this will be helpful, but I was coming up against the same error when I was trying to look at regional and seasonal differences in my data.

The way I resolved this issue was selecting region and season as random effects and nesting region in season when loading the mixture data. When I loaded my source data I selected regional differences in sources but no seasonal differences in sources.

This resolved the problem for me and was logical as I expected regional differences in isotopic signature but not seasonal differences.

I was doing this using the MixSIAR GUI

Sorry if this isn't helpful for your data

KGurney commented 6 years ago

Thanks very much for the tip – unfortunately, this didn’t make any difference with my data.

Seems it might come down to something weird within ggplot. Will wait to see if those wiser than me have any thoughts on that!

Best.

kbg

From: sianglazier [mailto:notifications@github.com] Sent: February 8, 2018 12:36 AM To: brianstock/MixSIAR Cc: Gurney, Kirsty (EC); Author Subject: Re: [brianstock/MixSIAR] ggplot error when making isospace plot? (#127)

Hi,

I'm not sure if this will be helpful, but I was coming up against the same error when I was trying to look at regional and seasonal differences in my data.

The way I resolved this issue was selecting region and season as random effects and nesting region in season when loading the mixture data. When I loaded my source data I selected regional differences in sources but no seasonal differences in sources.

This resolved the problem for me and was logical as I expected regional differences in isotopic signature but not seasonal differences.

Sorry if this isn't helpful for your data

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/brianstock/MixSIAR/issues/127#issuecomment-364016592, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AL36M8yLy0SiLw9dNqWnWHL0hqxGnEulks5tSpXmgaJpZM4RjOau.

brianstock commented 6 years ago

How do you have 7 columns in your mix data, but only 2 tracers? I suspect there is some issue with the structure of your data files. Would help if you can describe/summarize them here.

KGurney commented 6 years ago

Hi Brian;

Thanks for the reply.

The 7 columns in the mix data are the values for the tracers (C, N) plus some identifiers / covariates.

d13C

d15N

Cage

Group

Replic

BirdID1

Sex

-16.2

6.45

2

1

1

5672

M

-16.11

6.26

2

1

1

5674

M

-16.14

6.26

2

1

1

5675

F

The source file just includes the 2 food items (Form1, Form2), plus the Group number and the tracer values (mean, SD, n).

Group

Meand13C

SDd13C

Meand15N

SDd15N

n

Form1

1

-16.91

0.23

5.53

0.19

8

Form1

2

-16.91

0.23

5.53

0.19

8

Not sure if that is helpful or not, but happy to send the fractionation file too, if you think it might prove useful.

Thanks in advance for any insights / suggestions.

kbg

From: Brian Stock [mailto:notifications@github.com] Sent: March 9, 2018 12:11 AM To: brianstock/MixSIAR Cc: Gurney, Kirsty (EC); Author Subject: Re: [brianstock/MixSIAR] ggplot error when making isospace plot? (#127)

How do you have 7 columns in your mix data, but only 2 tracers? I suspect there is some issue with the structure of your data files. Would help if you can describe/summarize them here.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/brianstock/MixSIAR/issues/127#issuecomment-371723010, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AL36M_luVDGFF-90olkIRLbnmdNYd0u-ks5tchzWgaJpZM4RjOau.

brianstock commented 6 years ago

Hmm... I can take a look if you send me an email with the files and .R script you're trying to run.

latifapelage commented 5 years ago

Hello! I'm having the same problem with the script from the alligator example.When I run "plot_data(filename=paste0("isospaceplot", mod), plot_save_pdf=TRUE, plot_save_png=FALSE, mix[[mod]], source[[mod]], discr[[mod]])" I get the same error " Aesthetics must be either length 1 or the same as the data (28): size, linetype". I would like to know if you managed to solve this problem. Thank you very much!

gregor-fausto commented 10 months ago

The code below produces the same error message for me as above and in what seems to be related issue #343. I modified the wolves example R script to be a null model, as in model 1 of the alligator example, and edited out comments.

I'm on R version 4.2.2 with ggplot2_3.4.4 and MixSIAR_3.1.12.

# Load MixSIAR package
library(MixSIAR)

# To run on your data, replace the system.file call with the path to your file
mix.filename <- system.file("extdata", "wolves_consumer.csv", package = "MixSIAR")

# Load mixture data
mix <- load_mix_data(filename=mix.filename, 
                     iso_names=c("d13C","d15N"), 
                     factors=NULL, 
                     fac_random=NULL, 
                     fac_nested=NULL, 
                     cont_effects=NULL)

# To run on your data, replace the system.file call with the path to your file
source.filename <- system.file("extdata", "wolves_sources.csv", package = "MixSIAR")

# Load source data
source <- load_source_data(filename=source.filename, source_factors=NULL, 
                            conc_dep=FALSE, data_type="means", mix)

# To run on your data, replace the system.file call with the path to your file
discr.filename <- system.file("extdata", "wolves_discrimination.csv", package = "MixSIAR")

# Load discrimination data
discr <- load_discr_data(filename=discr.filename, mix)

# Make isospace plot
plot_data(filename="isospace_plot", 
          plot_save_pdf=FALSE,
          plot_save_png=FALSE,
          mix,source,discr)

The specific error message is

Error in `ggplot2::geom_pointrange()`:
! Problem while setting up geom aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (9)
✖ Fix the following mappings: `linetype`
gregor-fausto commented 10 months ago

Following up on the above, I think the warning may be coming from the function plot_data_two_iso, called within plot_data. For the NULL models in the example above, I think the following lines are called for plotting (lines 194-212)

if (mix$n.effects == 0) {
        g <- ggplot2::ggplot(data = df, ggplot2::aes(x = x, y = y)) + 
            ggplot2::geom_point() + ggplot2::geom_pointrange(data = df_sources, 
            ggplot2::aes(ymin = ymin, ymax = ymax), size = 1, 
            linetype = source_linetype, show.legend = F) + ggplot2::geom_errorbarh(data = df_sources, 
            ggplot2::aes(xmin = xmin, xmax = xmax), size = 1, 
            height = 0, linetype = source_linetype, show.legend = F) + 
            ggplot2::geom_text(data = source.labels, ggplot2::aes(x = x, 
                y = y, label = label), show.legend = F) + ggplot2::ylab(y_label) + 
            ggplot2::xlab(x_label) + ggplot2::theme_bw()
        print(g)
    }

These use df_sources, which gets constructed like this (lines 76-82)

  df_sources <- data.frame(x=MU_plot[,1], y=MU_plot[,2],
                           ymin = MU_plot[,2] - SIG_plot[,2],
                           ymax = MU_plot[,2] + SIG_plot[,2],
                           xmin = MU_plot[,1] - SIG_plot[,1],
                           xmax = MU_plot[,1] + SIG_plot[,1],
                           linetype = source_linetype,
                           scolour = source_color)

When the data frame df_sources is it gets a variable named linetype but not source_linetype. The latter is what's used in the plotting call. I think that may be one issue?

A related issue may be that if linetype depends on the data, then it might need to be called from within aes(...) in geom_pointrange and geom_errorbarh.

brianstock commented 10 months ago

Not at a computer, but my guess is this error happens because the wolves_sources.csv file has source data by factor (region) and you've created 'sources' without. I'd try calculating source means+SD+n without region, save as a new file, and use that.

On Thu, Oct 26, 2023, 6:52 AM Gregor-Fausto Siegmund < @.***> wrote:

The code below produces the same error message for me as above and in what seems to be related issue #343 https://github.com/brianstock/MixSIAR/issues/343. I modified the wolves example R script to be a null model, as in model 1 of the alligator example https://github.com/brianstock/MixSIAR/blob/master/inst/example_scripts/mixsiar_script_alligator.R, and edited out comments.

I'm on R version 4.2.2 with ggplot2_3.4.4 and MixSIAR_3.1.12.

Load MixSIAR package

library(MixSIAR)

To run on your data, replace the system.file call with the path to your file

mix.filename <- system.file("extdata", "wolves_consumer.csv", package = "MixSIAR")

Load mixture data

mix <- load_mix_data(filename=mix.filename, iso_names=c("d13C","d15N"), factors=NULL, fac_random=NULL, fac_nested=NULL, cont_effects=NULL)

To run on your data, replace the system.file call with the path to your file

source.filename <- system.file("extdata", "wolves_sources.csv", package = "MixSIAR")

Load source data

source <- load_source_data(filename=source.filename, source_factors=NULL, conc_dep=FALSE, data_type="means", mix)

To run on your data, replace the system.file call with the path to your file

discr.filename <- system.file("extdata", "wolves_discrimination.csv", package = "MixSIAR")

Load discrimination data

discr <- load_discr_data(filename=discr.filename, mix)

Make isospace plot

plot_data(filename="isospace_plot", plot_save_pdf=FALSE, plot_save_png=FALSE, mix,source,discr)

The specific error message is

Error in ggplot2::geom_pointrange(): ! Problem while setting up geom aesthetics. ℹ Error occurred in the 2nd layer. Caused by error in check_aesthetics(): ! Aesthetics must be either length 1 or the same as the data (9) ✖ Fix the following mappings: linetype

— Reply to this email directly, view it on GitHub https://github.com/brianstock/MixSIAR/issues/127#issuecomment-1780412426, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHDA456W2PAJDWABAUG3CDYBHUCPAVCNFSM4EMM42XKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZYGA2DCMRUGI3A . You are receiving this because you commented.Message ID: @.***>