0
votes

enter image description here

I'm trying to evaluate the above data in a boxplot similar to this: https://www.r-graph-gallery.com/89-box-and-scatter-plot-with-ggplot2.html

Text

I want the x axis to reflect my "Year" variable and each boxplot to evaluate the 8 methods as a distribution. Eventually I'd like to pinpoint the "Selected" variable in relation to that distribution but currently I just want this thing to render!

I figure out how to code my y variable and I get various errors no matter what I try. I think the PY needs to be as.factor but I've tried some code that way and I just get other errors.

anyway here is my code (Send Help):

# Libraries

library(tidyverse)
library(hrbrthemes)
library(viridis)
library(ggplot2)
library(readxl) # For reading in Excel files
library(lubridate) # For handling dates
library(dplyr) # for mutate and pipe functions

# Path to current and prior data folders
DataPath_Current <- "C:/R Projects/Box Plot Test"

Ult_sum <- read_excel(path = paste0(DataPath_Current, "/estimate.XLSX"), 
                                           sheet = "Sheet1",
                                           range = "A2:J12",
                                           guess_max = 100)

# just want to see what my table looks like
Ult_sum
# create a dataset - the below is code I commented out

# data <- data.frame(
# name=c(Ult_sum[,1]),
#     value=c(Ult_sum[1:11,2:8])
#)

value <- Ult_sum[2,]

  # Plot
Ult_sum %>%
  ggplot( aes(x= Year, y= value, fill=Year)) +
  geom_boxplot() +
  scale_fill_viridis(discrete = TRUE, alpha=0.6) +
  geom_jitter(color="black", size=0.4, alpha=0.9) +
  theme_ipsum() +
  theme(
    legend.position="none",
    plot.title = element_text(size=11)
  ) +
  ggtitle("A boxplot with jitter") +
  xlab("")
1

1 Answers

3
votes

I do not see how your code matches the screenshot of your dataset. However, just a general hint: ggplot likes data in long format. I suggest you reshape your data using tidyr::reshape_long oder data.table::melt. This way you get 3 columns: year, method, value, of which the first two should be a factor. The resulting dataset can then be neatly used in aes() as aes(x=year, y=value, fill=method).

Edit: Added an example. Does this do what you want?

library(data.table)
library(magrittr) 
library(ggplot2)
DT <- data.table(year = factor(rep(2010:2014, 10)),
                 method1 = rnorm(50),
                 method2 = rnorm(50),
                 method3 = rnorm(50))

DT_long <- DT %>% melt(id.vars = "year")

ggplot(DT_long, aes(x = year, y = value, fill = variable)) +
geom_boxplot()

enter image description here