Home > Blockchain >  How do I run a ggboxplot in ggplotly within R Markdown?
How do I run a ggboxplot in ggplotly within R Markdown?

Time:12-24

Problem:

I'm not sure what is going on, but every time I try to run this in my R script, I get no issues. When I run it in R Markdown, it produces errors despite pre-loading all of the necessary packages and data. I will list the error below. First, I will show what I have for a script.

Here is the dput for my data:

structure(list(Case_Number = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 
62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 
75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 
88L, 89L, 90L, 91L), Year = c("2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021"), Month_Number = c("9", 
"9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", 
"9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", 
"9", "9", "9", "10", "10", "10", "10", "10", "10", "10", "10", 
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", 
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", 
"11", "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", 
"11", "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", 
"11", "11", "11", "11", "11", "11", "11", "11"), Month_Name = c("September", 
"September", "September", "September", "September", "September", 
"September", "September", "September", "September", "September", 
"September", "September", "September", "September", "September", 
"September", "September", "September", "September", "September", 
"September", "September", "September", "September", "September", 
"September", "September", "September", "September", "October", 
"October", "October", "October", "October", "October", "October", 
"October", "October", "October", "October", "October", "October", 
"October", "October", "October", "October", "October", "October", 
"October", "October", "October", "October", "October", "October", 
"October", "October", "October", "October", "October", "November", 
"November", "November", "November", "November", "November", "November", 
"November", "November", "November", "November", "November", "November", 
"November", "November", "November", "November", "November", "November", 
"November", "November", "November", "November", "November", "November", 
"November", "November", "November", "November", "November"), 
    Day_Number = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
    12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
    24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
    19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
    27L, 28L, 29L, 30L), Day_Name = c("Wednesday", "Thursday", 
    "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", 
    "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", 
    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", 
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
    "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
    "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", 
    "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", 
    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", 
    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", 
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
    "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
    "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday"
    ), Time_Wake = c(500L, 715L, 600L, 600L, 700L, 600L, 700L, 
    500L, 500L, 500L, 500L, 700L, 645L, 700L, 630L, 645L, 700L, 
    600L, 700L, 550L, 700L, 730L, 725L, 800L, 600L, 640L, 600L, 
    730L, 650L, 830L, 630L, 630L, 830L, 722L, 641L, 800L, 720L, 
    700L, NA, NA, NA, 700L, 700L, 622L, 710L, 632L, 400L, 640L, 
    700L, 730L, 830L, 659L, 800L, 701L, 700L, 900L, 930L, 650L, 
    930L, NA, 300L, 830L, 800L, 705L, 647L, 800L, NA, 830L, NA, 
    830L, 838L, 650L, 849L, 500L, 830L, 800L, 321L, 700L, 400L, 
    400L, NA, 700L, 600L, 604L, 700L, 730L, 700L, 700L, 500L, 
    700L), Mins_Sleep = c(300L, 540L, 540L, 480L, 480L, 480L, 
    480L, 420L, 300L, 240L, 480L, 300L, 420L, 360L, 390L, 405L, 
    420L, 360L, 420L, 350L, 420L, 450L, 445L, 480L, 300L, 400L, 
    310L, 390L, 350L, 450L, 390L, 390L, 510L, 452L, 310L, 360L, 
    500L, 360L, NA, NA, 420L, 420L, 420L, 382L, 430L, 393L, 240L, 
    400L, 480L, 450L, 450L, 359L, 420L, 361L, 360L, 480L, 570L, 
    340L, 450L, NA, 180L, 510L, 420L, 425L, 407L, 360L, NA, 510L, 
    NA, 360L, 368L, 410L, NA, 360L, 510L, 436L, 291L, 420L, 240L, 
    300L, NA, 420L, 420L, 424L, 520L, 240L, 390L, 480L, 300L, 
    480L), Start_Work = c(1015L, 1000L, 945L, 1400L, 1500L, 915L, 
    930L, 1000L, 940L, 840L, 730L, 1700L, 945L, 1040L, 955L, 
    945L, 930L, 745L, 800L, 955L, 1030L, 1115L, 905L, 930L, 815L, 
    830L, 950L, 1108L, 1430L, 955L, 1313L, 1125L, 1636L, 1126L, 
    1027L, 1323L, 1003L, 918L, NA, NA, 950L, 913L, 1244L, 656L, 
    930L, 718L, 1744L, 759L, 928L, 912L, 857L, 930L, 907L, 920L, 
    1029L, 1027L, 1211L, 914L, 1226L, 1337L, 1900L, 1313L, 1118L, 
    800L, 700L, 1544L, 1350L, 905L, 1025L, 0L, 942L, 930L, 1234L, 
    1222L, 925L, 0L, 2018L, 945L, 500L, 447L, 0L, 818L, 604L, 
    632L, 1015L, 930L, 748L, 732L, 900L, 739L), End_Work = c(1800L, 
    1600L, 1210L, 1700L, 1515L, 1530L, 1530L, 1800L, 1650L, 1800L, 
    1410L, 2000L, 1710L, 1430L, 1800L, 1840L, 1720L, 1915L, 1240L, 
    2040L, 1730L, 1825L, 1100L, 2130L, 1705L, 915L, 1504L, 1430L, 
    1933L, 1335L, 1330L, 2130L, 1636L, 1600L, 1730L, 1800L, 1739L, 
    1413L, NA, NA, 1729L, 2200L, 2219L, 2316L, 2359L, 911L, 2014L, 
    1945L, 2113L, 2004L, 2017L, 1530L, 2114L, 2158L, 1800L, 1813L, 
    2005L, 1717L, 1512L, 1620L, 2209L, 2200L, 1231L, 2030L, 1817L, 
    1737L, 1605L, 2038L, 2357L, 2348L, 1407L, 1653L, 1720L, 1927L, 
    2359L, 1911L, 2355L, 1726L, 2247L, 2359L, 2020L, 2032L, 1818L, 
    1449L, 1407L, 1330L, 2223L, 2232L, 2040L, 2359L), Workout_Y_N = c(1, 
    1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 
    0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Time_Workout = c(730L, 
    730L, 730L, NA, NA, NA, NA, 730L, 730L, NA, NA, NA, NA, 735L, 
    735L, 735L, NA, NA, 2030L, 730L, NA, NA, NA, NA, NA, NA, 
    730L, NA, NA, NA, NA, 915L, 1000L, 815L, NA, 900L, 740L, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, 1011L, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA), Work_Environment = c("Office", 
    "Office", "Office", "Home", "Home", "Office", "Office", "Office", 
    "Office", "Office", "Home", "Home", "Office", "Office", "Office", 
    "Home", "Office", "Home", "Home", "Office", "Office", "Home", 
    "Office", "Home", "Home", "Home", "Office", "Office", "Office", 
    "Office", "Home", "Home", "Home", "Office", "Office", "Office", 
    "Office", "Office", "Home", "Home", "Office", "Both", "Home", 
    "Home", "Both", "Home", "Home", "Both", "Office", "Home", 
    "Home", "Both", "Home", "Home", "Office", "Office", "Home", 
    "Office", "Home", "Home", "Home", "Both", "Home", "Both", 
    "Both", "Home", "Home", "Both", "Both", "Home", "Home", "Office", 
    "Office", "Home", "Both", "Both", "Home", "Office", "Both", 
    "Home", "Home", "Both", "Both", "Home", "Home", "Both", "Home", 
    "Home", "Office", "Both"), Coffee_Cups = c(3L, 0L, 2L, 6L, 
    4L, 5L, 3L, 3L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 2L, 0L, 1L, 1L, 
    4L, 4L, 3L, 0L, 1L, 3L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 2L, 3L, 
    2L, 2L, 4L, 3L, 6L, 6L, 3L, 4L, 6L, 8L, 3L, 5L, 0L, 2L, 2L, 
    8L, 6L, 4L, 6L, 4L, 4L, 2L, 6L, 6L, 5L, 1L, 1L, 5L, 4L, 6L, 
    5L, 0L, 6L, 6L, 4L, 4L, 2L, 2L, 6L, 6L, 7L, 3L, 3L, 0L, 5L, 
    7L, 6L, 3L, 5L, 3L, 3L, 1L, 9L, 9L, 3L, 3L), Tea_Cups = c(2L, 
    4L, 2L, 0L, 0L, 2L, 0L, 2L, 4L, 0L, 0L, 0L, 2L, 6L, 5L, 0L, 
    2L, 0L, 2L, 4L, 0L, 0L, 0L, 2L, 1L, 0L, 4L, 4L, 4L, 2L, 1L, 
    0L, 2L, 0L, 0L, 4L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 
    1L, 0L, 2L, 0L, 0L, 2L, 0L, 3L, 0L, 2L, 0L, 2L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Mins_Work = c(435L, 350L, 145L, 135L, 15L, 60L, 60L, 390L, 
    395L, 395L, 315L, 80L, 580L, 175L, 545L, 230L, 435L, 370L, 
    255L, 515L, 330L, 65L, 115L, 550L, 420L, 45L, 266L, 196L, 
    198L, 220L, 17L, 382L, 0L, 180L, 343L, 207L, 263L, 332L, 
    0L, 0L, 259L, 417L, 282L, 685L, 517L, 111L, 64L, 466L, 499L, 
    460L, 269L, 300L, 427L, 301L, 436L, 342L, 229L, 379L, 102L, 
    146L, 94L, 345L, 73L, 204L, 512L, 113L, 135L, 458L, 493L, 
    552L, 108L, 335L, 395L, 508L, 546L, 396L, 159L, 325L, 747L, 
    650L, 377L, 461L, 669L, 186L, 220L, 410L, 708L, 409L, 515L, 
    413L)), row.names = c(NA, -90L), class = "data.frame")

Script for Markdown:

# Load libaries:    
library(tidyverse) # piping and general ggplot functions
library(ggpubr) # better looking ggplots
library(plotly) # interactive plots
library(foreign) # loading most types of data

# Load dataset:
work <- read.csv("Shawn_Productivity.csv") %>% 
  slice(1:113) 

work_clean <- work %>% 
  filter(!Month_Name == "December") %>% 
  filter(!Mins_Work=="NA")

# Full Boxplot:
gbox <- ggboxplot(work_clean,
          x = "Day_Name",
          y = "Mins_Work",
          color = "Day_Name",
          palette = "futurama")  
  labs(x="Day Name",
       y="Minutes of Work",
       title = "ANOVA Boxplot of Daily Differences in Productivity") 
  theme_bw() 
  theme(plot.title = element_text(face = "bold"),
        plot.background = element_rect(fill = "gray"),
        axis.text = element_text(color = "black"),
        plot.subtitle = element_text(face = "italic")) 
  annotate("text",
           x=5.5,
           y=700,
           label = "*No overall or PWC significance found")

ggplotly(gbox)

# Create interactive plot:
ggplotly(gbox)

Which should give me this interactive plot in R:

enter image description here

Error

Instead what I get is this error message:

Line 9: Error in file (file, "rt") : ?????????
Calls: <Anonymous> ... eval -> %>% -> slice -> read.csv -> read.table -> file
???

Which looks like this:

enter image description here

What do I do to fix this? I've tried nitpicking the code, using the exact directory name for the CSV file, etc. to see whats wrong but cant seem to find the error.

For clarification, this is what the R Markdown doc looks like:

---
title: "Using GGPLOTLY for Interactive Plots"
author: "Shawn Hemelstrand"
date: "2021/12/23"
output: html_document
---

```{r interactive, echo=TRUE}
# Load libraries:
library(tidyverse) # piping and general ggplot functions
library(ggpubr) # better looking ggplots
library(plotly) # interactive plots
library(foreign) # loading most types of data
library(rstatix) # for ANOVA functions

# Load dataset:
work <- read.csv("Shawn_Productivity.csv") %>% 
  slice(1:113) 

work_clean <- work %>% 
  filter(!Month_Name == "December") %>% 
  filter(!Mins_Work=="NA")

# Full Boxplot:
gbox <- ggboxplot(work_clean,
          x = "Day_Name",
          y = "Mins_Work",
          color = "Day_Name",
          palette = "futurama")  
  labs(x="Day Name",
       y="Minutes of Work",
       title = "ANOVA Boxplot of Daily Differences in Productivity") 
  theme_bw() 
  theme(plot.title = element_text(face = "bold"),
        plot.background = element_rect(fill = "gray"),
        axis.text = element_text(color = "black"),
        plot.subtitle = element_text(face = "italic")) 
  annotate("text",
           x=5.5,
           y=700,
           label = "*No overall or PWC significance found")

ggplotly(gbox)
```

CodePudding user response:

You need to know the current directory before executing the code. You can get it using getwd().

The working directory of your R console and the working directory of the R markdown file that you have created is different.

R markdown documents are compiled separately in separate R sessions. So your, current R console has nothing to do with the compilation of the R markdown documents.

Once, you get the current working directory with getwd(). You need to change it with the correct path where your R markdown file is saved using setwd().

EDIT The only thing I did different that your code is as follows:

setwd("D:/Programming/StackOverflow/")
work <- read.csv("Shawn_Productivity.csv") %>% 
  slice(1:113)
  • Related