Code runs but ggplot is not getting plotted

Good day everyone,

Maybe it's a basic question but I can' resolve my issue.

I'm trying to plot a density graph using ggplot2. The code is running without errors but nothing is displayed in Rstudio.
I am using MixSIAR before to determine the contribution of food resources in the diet of a specific consumer in different sites.
Here is my entire code:

library(MixSIAR)
library(tidyr)
library(ggplot2)
install.packages("RColorBrewer")
library(RColorBrewer)


consumers <- read.csv("Tamarins_hairs_modif1.csv",header=TRUE, sep=",")
sources <- read.csv("Sources_Mean_SMA_PB.csv", header=TRUE, sep=",")
TEFs <- read.csv("TEFs.csv",header=TRUE, sep=",")

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

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

TEF <- load_discr_data(filename="TEFs.csv", mix)

model_filename <- "MixSIAR_model.txt"
resid_err <- TRUE
process_err <- TRUE
write_JAGS_model(model_filename, resid_err, process_err, mix, source)

jags2 <- run_model(run="test", mix, source, TEF, model_filename,
                   alpha.prior = 1, resid_err, process_err)

post.acol.data <- c(jags2$BUGSoutput$sims.list$p.fac1[,1,1], jags2$BUGSoutput$sims.list$p.fac1[,1,2], 
                    jags2$BUGSoutput$sims.list$p.fac1[,1,3])
post.acol.names <- c(rep("Aranea", length(jags2$BUGSoutput$sims.list$p.fac1[,1,1])),
                     rep("Fruits", length(jags2$BUGSoutput$sims.list$p.fac1[,1,2])),
                     rep("Insects", length(jags2$BUGSoutput$sims.list$p.fac1[,1,3])))
post.acol <- data.frame(post.acol.data, post.acol.names)



p<- ggplot(post.acol, aes(x = post.acol.data, y=..scaled.., colour = post.acol.names, fill = post.acol.names)) + 
   geom_density(alpha=0.5) + 
  theme_bw() +
  scale_x_continuous(name="Source contribution to diet", limits= c(0,1)) +
  scale_y_continuous(name=NULL, breaks=NULL) +
  scale_color_manual(values=c("#00A08A","#FF0000","#F98400")) +
  scale_fill_manual(values=c("#00A08A","#FF0000","#F98400")) +
  ggtitle(expression(italic("Leontopithecus chrysopygus"))) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.background = element_rect(fill=NULL, size=NULL, linetype=NULL)) +
  guides(fill=guide_legend(title="Source"), colour=guide_legend(title="Source"))
print(p)

Thanks in advance for helping me :slight_smile:

1 Like

Hi! Welcome to the RStudio Community!

I might be missing it in the code somewhere but where is the scaled object coming from?

if this is a pure plotting issue, you should replace the creation code you shared (that is useless to us because we dont have your csv files ...) with the result of your post.acol creation that your plotting relies on.
i.e. give us the result of

dput(post.acol)

and then we can look at how you use ggplot

1 Like

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.