Rmarkdown knit gives pdf with wrong plot. ggplot2 alone renders the right one

526 views Asked by At

The code I am attaching here renders the wrong plot when knitted ("Tk.wrong.png", first plot here). However, if I run it as a standard R code, the rendered plot is the right one ("Tk.right.png", second plot). I find this puzzling. Any hints? Thanks a lot. Best, David R PS: The Tk data.frame can be downloaded here. https://mycore.core-cloud.net/index.php/s/OKF9dp1tWa0OR1g

Tk.wrong.pngTk.right.png

This is the Tk data frame enter image description here

---

title: "Tukey plot help" author: "DR" date: "16/10/2020" output: pdf_document

knitr::opts_chunk$set(echo = TRUE)
setwd("~/Documents/IPBS/JN/AB")
library(tidyverse)
input.data <- read.table("input.data.txt", header = TRUE, sep = "\t")
Tk <- rownames_to_column(as.data.frame(TukeyHSD(aov(Score_Avg ~ Manip * Traitement, data = input.data), "Traitement")$Traitement),var = "comp") 
Tk$signif <- sapply(1:nrow(Tk), function (x) between(0,Tk$lwr[x],Tk$upr[x]))
Tk$comp <- as.factor(Tk$comp)
levels(Tk$comp) <- c("Genta-NT", "3T-NT", "G3T-NT", "3T-Genta", "G3T-Genta", "G3T-3T")
ggplot(Tk, aes(x = diff, y = comp, colour = signif)) +
  geom_point() +
  geom_errorbar(aes(xmin = lwr, xmax = upr), width = .1) +
  geom_vline(xintercept = 0, colour = "darkgreen") +
  scale_colour_manual(values=c('darkred','darkblue')) +
  theme_minimal() +
  theme(axis.title.x = element_text(size = 20),
        axis.text.x = element_text(size = 15),
        legend.position = "none")
ggsave("Tk.wrong.png")
0

There are 0 answers