2

New to R and appreciate advice! I am trying to plot multiple boxplots on the same graph, split by date. My data is saved in a .csv, and the code I am trying right now is the following. I suspect my as.Date function is not working appropriately. Any help is much appreciated! Data is in csv columns of "Day" and "Strength". Date formatting in the .csv is, for example, "5/1/21" for May 1, 2021.

FG <- read.csv("/Users/L/Downloads/ResultsFG.csv", header=T, na.strings=c("")) library(ggplot2) FG$Day <- as.Date(FG$Day) FG$Day <- format(FG$Day, "%b-%d") FG$Day <- factor(FG$Day, levels=c("May-1","May-2","May-3", "May-4", "May-5", "May-6", "May-7", "May-87", "May-9", "May-10", "May-11", "May-14", "Mar-17", "May-18", "May-19", "May-20", "May-21", "May-22", "May-24", "May-25", "May-26", "May-27", "May-28", "May-29", "May-30", "May-31")) ggplot() + geom_boxplot(data = FG, aes(x = factor(Day, level = d_order), y = Strength), color = "forestgreen", fill= "forestgreen", alpha = 0.1) 

I don't get any error messages, but I do get one large boxplot labeled on the X axis as "NA". (screenshot below) Can you help me split this by date?

single boxplot generated, X axis = "NA"

ETA: Strange result after I call the Day column after trying to convert it

> FG$Day [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [9] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [17] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [25] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [33] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [41] <NA> <NA> <NA> <NA> May-21 May-21 May-21 May-21 [49] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [57] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [65] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [73] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [81] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [89] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [97] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> 26 Levels: May-1 May-2 May-3 May-4 May-5 May-6 May-7 ... May-31 
5
  • 1
    The please submit a reprex army will be along shortly. Your code is OK. But without the CSV we can't exactly replicate. Commented Jul 23, 2021 at 19:05
  • But you should have an x OR a y data not both. Your X needs to be moved to group=Day Commented Jul 23, 2021 at 19:07
  • I'm trying to have my X be the date... Multiple boxplots, one over each date. Commented Jul 23, 2021 at 19:11
  • We can't possibly help if you aren't showing us the data that goes in. You need to as a minimum show us: head(FG$Day) before and after each line of code Commented Jul 23, 2021 at 22:59
  • Here is a tip though. as.Date("5/1/21") will normally give "0005-01-21" because it's an ambiguous format. as.Date("5/1/21", tryFormats = "%m/%d/%y") will give "2021-05-01" which is what you want Commented Jul 23, 2021 at 23:10

1 Answer 1

1

As you didn't provide your sample data, you can try this.

library(gapminder) library(ggplot2) FG <- gapminder[1:444,] FG$Day <- FG$year FG$Day <- factor(FG$Day, levels=unique(FG$Day)) FG$Strength <- FG$lifeExp ggplot(data = FG, aes(x = factor(Day), y = Strength, fill= "forestgreen", color="red")) + geom_boxplot( alpha = 0.1) + scale_fill_manual(values="forestgreen") + scale_color_manual(values="red") 

output

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think there is a need for factor on the X= in ggplot as it's already been factored
I agree. I was just trying to leave the code as is - as I am not sure how his data frame is set-up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.