R Markdown not showing my graph

Hello everyone!
I'm using R Markdown for a class assignment. I plot my graphs they show up fine. However, when I knit it (to html) only the image icons show up. Does anybody ever had this problem or know a solution?

Hello @FGiacomelli ,
I hope that your issue is solved by now, but if not ...

  • I assume that you use the RStudio environment and that your .Rmd file is contained in a project. Is this the case?
  • Try to knit the default Rmd file (*) that contains a plot. Does that show correctly?
    (*) File | New File | R Markdown ... | HTML save the file and knit
  • Show us the YAML that you use at the beginning of your .Rmd file
  • Which knitr options do you use in your code (in e.g. the setup chunk) ?
    You don't do anything with folder locations?
  • In the HTML file you can see the path of the generated graphics. What does it show?
    Probably easier to interpretate is the path in the intermediate .md file. The graphics file should be in the indicated folder. The .md file can be generated by changing the YAML in the following way (for the default .Rmd file that I mentioned above) :
---
title: "Untitled"
author: "My name"
date: "2023-03-09"
output:
  html_document:
    keep_md: yes
---

Good luck!

HI @FGiacomelli, try to knirt in pdf or word for see if you get the same results.
Update rmarkdown library. Is estrange this error, but try to make the plot with ggplot:

library(ggplot2)

ggplot(Drosophila, aes(x=ocular.units, y=sugar.treatment))+
  geom_boxplot()+
  labs(title = 'Put_your_title',
       x='sugar',
       y='ocular units')

Hi HanOostdijk
Yes, I am using R Studio and my .Rmd is contained in a project. I tried both what you and @M_AcostaCH suggested and still have the same problem.

This is my entire script.

title: 'Homework #2'
author: "Fabio Giacomelli"
date: "2023-03-06"
output:
html_document: default
word_document: default
editor_options:
chunk_output_type: console

knitr::opts_chunk$set(echo = TRUE)

PROBLEM 1.

Define, compare and contrast fixed vs random factors.Provide specific examples of each and why would you must take their differences into account when designing an experiment. (No need to fallow format for this question.)

Fixed factors are the ones which the researcher has control over its levels i.e., size of an organism in a controlled lab environment in which every factor (temperature, humidity, salinity, food, and water availability, etc..) can be manipulated. Random factor would be if the same type of data would come from a wild population of the same organism, where the researcher can measure some of the same factors (and other factors i.e., precipitation) however, he/she has no control over them. When designing an experiment, the researcher should take this into account since depending on the type of factors the experiment has, a different type of analysis is necessary.

PROBLEM 2.

**a.**Determine if adding different sugars to the growth media in Drosophila has an effect in the development of ocular units. b. Provide the mean treatment effect that each factor level has, i.e.,individual ai’s. What can you conclude from that information?

Hypothesis

Ho:µ_{Treatment} = µ_{Control}
No differences will be observed in the number of Drosophila ocular units between sugar treatments
Ha:µ_{Treatmet}µ_{Control}
Differences will be observed in the number of Drosophila ocular units between sugar treatments

Justification

An ANOVA test is the recommended test when more than two variables are being tested. However, to use an ANOVA correctly the data needs to meet the test assumptions (normal distribution, equal variances, and independence).I first ran a Shapiro-Wilk test to assess data distribution (W = 0.96928, p-value = 0.2164). S-W test states the if p= >0.05, the data is normally distributed. A Bartlett Test was ran to test data variances. Bartlett states that if p= >0.05 then the data has equal variances. My initial test showed that the data did not have equal variances(K-squared = 9.0711, df = 4, p-value = 0.05935) so I ran the test again, this time with logarithmic transformation to make the data meet ANOVA assumptions. Finally I re-ran the ANOVA to assess if there were differences between treatments.

results

After making sure that the data met ANOVA assumptions, the test was re-run and differences between treatments were detected.To asses where were the differences a Tukey-Kramer test was conducted and differences were detected between control and all treatments control - 1%glucose.1%fructose (p= 0.0000000), control - 2%.fructose (p= 0.0000000), control - 2%.glucose (p= 0.0000000), and control - 2%.sucrose (p= 0.0000136) and also between 2% sucrose treatment and the other treatments 2%.sucrose - 1%glucose.1%fructose (p= 0.0000012), 2%.sucrose - 2%.fructose (P= 0.0000023), and 2%.sucrose - 2%.glucose (p= 0.0001239). The mean treatment effect showed a reduction in ocular units of 15.40656% in the 2% Glucose treatment, 16.97575% in the 2% Fructose treatment, 17.26106% in the 1% Glucose + 1% Fructose, and 8.559201% in the 2% Sucrose treatment.

Discussion

After data analysis I can conclude that adding sugars to the growth media for Drosophila, creates a significant reduction in the development of ocular units. However there was no statistically significant differences between three of the treatments 1% glucose 1% fructose, 2% fructose and 2% glucose.

library(pwr)
library(hot.deck)
library(tinytex)
library(ggplot2)
library(rmarkdown)

Drosophila <- read.csv("~/R/Homework 2/ANOVA problem2data.csv")

summary(Drosophila)
str(Drosophila)
head(Drosophila)
is.discrete(Drosophila$ocular.units, cutoff = 10)


boxplot(Drosophila$ocular.units~Drosophila$sugar.treatment, xlab = "sugar", ylab = "ocular units")
#Here I ran a box plot to visualize the data.

Drosophila.AOV <-aov(Drosophila$ocular.units~Drosophila$sugar.treatment)
Drosophila.AOV$residuals
shapiro.test(Drosophila.AOV$residuals)
#I ran a analysis of variance to obtain the residual and then I used them on a Shapiro-Wilk test to see if the data is normally distributed. 

bartlett.test(Drosophila$ocular.units~Drosophila$sugar.treatment)

bartlett.test(log10(Drosophila$ocular.units)~Drosophila$sugar.treatment)

#After failing to reject the null hypothesis with the Bartlett Test I transformed the data to meet the ANOVA assumptions.


Drosophila.AOV <-aov(log10(Drosophila$ocular.units)~Drosophila$sugar.treatment)

#To visualize the data I run summary
summary(Drosophila.AOV)

#to find where the differences are I run a Tukey-kramer test
TukeyHSD(Drosophila.AOV)

#To better visualize the differences I plot the data.
with(par(mai=c(1,2.5,1,1)), {plot(TukeyHSD(Drosophila.AOV),las=1,cex.axis=0.6)})

#Calculate the mean for each treatment
Control_µ <- mean(Drosophila[1:10,2])
Gluc2_µ<- mean(Drosophila[11:20,2])
Fruc2_µ<- mean(Drosophila[21:30,2])
Gluc1Fruc1_µ<- mean(Drosophila[31:40,2])
Sucr2_µ<- mean(Drosophila[41:50,2])

#Mean Treatment effect for each treatment
(Gluc2_µ-Control_µ)/Control_µ*100
(Fruc2_µ-Control_µ)/Control_µ*100
(Gluc1Fruc1_µ-Control_µ)/Control_µ*100
(Sucr2_µ-Control_µ)/Control_µ*100


PROBLEM 3.

The Australian brown tree snake was introduced in Guam and has cause the extinction of two native birds. This snake seems to reproduce more often and less seasonally than in its native range in Australia. The data provided represents the grams of fat in the sexual segment of the kidney, which is a good indication of sperm production in males. All of the data was collected in the non-breeding. Determine if there are any differences among the populations.

Hypothesis

Ho:µ Kidney fat = among populations
There will be no differences in the average kidney fat among populations.
Ha:µ Kidney fat ≠ among populations
There will be differences in the average kidney fat among populations.

Justification

After testing the data for normality and homogeneity with a Shapiro_Wilks (W = 0.95147, p-value = 0.01458) and a Bartlett THV (K-squared = 36.628, df = 2, p-value = 1.113e-08), it failed both tests. Even after running a logarithmic transformation (K-squared = 17.097, df = 2, p-value = 0.0001938), the dataset still failed the test for homogeneity with an extremely low p-value which indicates the the data is in fact non-homogenious. Since the data did not meet ANOVA assumptions a Kruskal_Wallis rank sum test was conducted which states that if p= <0.05 than statisticaly significant differences were detected.To find where the differences were located, a Pairwise-Wilcox test was conducted.

results

After ascertaining that the data was non-parametric a Kruskal_wallis test was conducted wich indicated that statistically significant differences were present (χ^2^ = 17.239, df = 2, p-value = 0.0001806).Finally a Pairwise_Wilcox test with a Benjamini-Hochberg p adjustment was conducted to assess where the differences were located. Benjamini-Hochberg p adjustment is use to avoid Type-I error since it can control small p-values. A statistically significant difference was detected only between Lilos and ArmP populations(p=5.5e-05) while no differences were detected between ArmP and TerriaUp (p= o.062) and Lilos and TerriaUp (p= 0.62).

Discussion

I cannot ascertain if there are differences in kidney fat content between the populations of the Australian brown tree snake from Australia (native range) and Guam (introduced range),since the dataset did not show which one is which. However, the statistical analysis indicates that there is a statistically significant difference between Lilos and ArmP populations.


Snake <- read.csv("~/R/Homework 2/ANOVA Problem 3 data.csv")

summary(Snake)
str(Snake)
head(Snake)
is.discrete(Snake$Fat.g, cutoff = 10)

boxplot(Snake$Fat.g~Snake$Population, xlab = "population", ylab = "Kidney Fat")

snake.AOV <- aov(Snake$Fat.g~Snake$Population)
snake.AOV$residuals
shapiro.test(snake.AOV$residuals)

bartlett.test(Snake$Fat.g~Snake$Populati)

bartlett.test(log10(Snake$Fat.g)~Snake$Populati)

#after testing the data for normality and homogeneity, it failed  both tests.

#Since the dataset does not meet ANOVA assumptions, a non-parametric a Kruskal_Wallis test was conducted.

kruskal.test(Snake$Fat.g~Snake$Population)


#To find where the differences are located a pairwise test was conducted

pairwise.wilcox.test(Snake$Fat.g,Snake$Population, p.adjust.method = "BH")






PROBLEM 4.

Wood density, and to a great extent hardness, is mostly determine by the number of pores per square mm in the wood. Determine which of the three species has the greatest hardness. Which one the least?

Hypothesis

Ho:µ Wood Hardness = Among Species
There will be no difference in the average wood density among species.
Ha:µ Wood Hardness ≠ Among Species
There will be difference in the average wood density among species.


WoodDens <- read.csv("~/R/Homework 2/ANOVA problem 4 data.csv")

summary(WoodDens)
str(WoodDens)
head(WoodDens)
is.discrete(WoodDens$Cells, cutoff = 10)

hist(WoodDens$Cells)

boxplot(WoodDens$Cells~WoodDens$SPP, xlab = "Species", ylab = "Pores")

WoodDens.AOV <-aov(WoodDens$Cells~WoodDens$SP)
WoodDens.AOV$residuals
hist(WoodDens.AOV$residuals)
shapiro.test(WoodDens.AOV$residuals)
bartlett.test(log10(WoodDens$Cells+1)~WoodDens$SPP)

kruskal.test(WoodDens$Cells~WoodDens$SPP)

pairwise.wilcox.test(WoodDens$Cells,WoodDens$SPP, p.adjust.method = "BH")



I solved it!

I divide my chunks into smaller chunks and it's knitting fine!

Thanks for your help!

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