problem with Rmd script and showing plots in chunk output and in final report html

Hello everyone!

I am developing an R Markdown (Rmd) script in which I need to create ggplot2 plots using the facet_wrap and geom_jitter parameters. After creating each plot in a different chunk, the resulting plot is being displayed in the Viewer panel of RStudio. It is not showing in the chunk output, even though I have added the parameters "fig.show='hold'" and "echo=TRUE" to the chunk configuration. (I have also added them to the YAML configuration in the Rmd).

As another peculiar detail, when I try to run ggplotly on this plot, the final configuration changes and distorts the entire graph, making it hard to interpret.

I have also attempted to remove the plots from memory using dev.off() and graphics.off(), but that did not change anything.

As a last option, I reviewed the general chunk options in the Rmd, and "Chunk Output Inline" was selected, along with "Preview Images and Equations" and "Show Previews Inline".

I would appreciate any help.

Thanks!

Best,
E.

Welcome to the forum

I think we need to see your code and some sample data. See
FAQ Asking Questions

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here between
```

```

What operating system are you using?

Hi!

First of all, I apologize for the late response and the next problem: I cannot provide any sample data.

About my operating system: I am working on Ubuntu Linux 22.04, and the graphs that were giving me problems had the following structure:


p9 <- ggplot(datos_long, aes(x = varInte, y = Measure, fill=varint)) +
  geom_boxplot(outlier.shape  = NA) +
  geom_jitter(aes(color = varint), height = 0, width = .2) +
  labs(x = "varInte", y = "Measure\n") +
  facet_wrap(~ varint, scales = "free") +
  theme_minimal() +
  theme(legend.position = "right",
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1, size = 7),
        plot.background = element_rect(size = 2, color = "white"),
        plot.margin = unit(c(1, 1, 1, 1), "cm"),
        strip.text.x = element_text(size = 6),  
        legend.title = element_text(size = 8),
        legend.text = element_text(size = 6))  +
        labs(fill = "vartInte") + 
  guides(color = FALSE, fill = guide_legend(override.aes = list(size = 1)))  


p9

I have renamed the variable's names. Also, if this could work in any way, I transformed the dataset into a long one to represent the data. The data I wanted to represent is related to the abundance (Measure) of bacteria genera (varint) against a variable of interest from the metadata (varInte).

I finally made it work using the command ggsave() followed by the command knitr::include_graphics().

However, I still cannot understand why if I don't use these combined commands, it doesn't work.

Thank you so much for your response!

Best,
E.

Sorry for the late reply. I came down with something like the flu.

I have no idea of why you need those commands. Where do you place them? In any case I am getting the same blank output without them.

If I run the code with some mocked up data under normal R I get what looks to be very weird results. See below.

ElsamdeA1

It just occured to me.
In ```{r } is include set to TRUE ?

That is, you may want something like this

insert code

Hi!

I have only echo=FALSE and some parameters related to the fig size (width, height,...). I have also tried with the parameters: with and fig.align = "center".

I will check the options you gave me and update you with news.

Best,
Elsa

Blast it, I was in a hurry and did not realize I messed up my code

```{r include = TRUE}
insert code
```

1 Like

This topic was automatically closed 45 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.