0

I have some measurement data for Central Texas lizards. Each lizards has several traits measured, is assigned a decade based on the year it was collected (these range from 1940-2010), and is associated with one of three distinct environmental categories-high urban (HU), low urban (LU), and rural (R). I am trying to create a boxplot that plots the measurement (I am using the measurement "svl" here as an example) by decade, but with each decade having three boxes for each environmental category (which is called "local"type" in my dataset). However, I am only getting one box per decade. How could I rectify this? Thanks.

library(ggplot2)
library(plyr)
olivaceus <- read.csv("FL_olivaceus_measurements_11-2-2020.csv")
View(olivaceus)
plot <- ggplot(olivaceus, aes(decade, svl, fill=local_type)) + geom_boxplot(na.rm = TRUE) + theme_classic() + stat_boxplot(geom = 'errorbar', width = 0.2)
plot

this is the plot I am getting

  • 1
    `aes(decade, svl, group = local_type, fill=local_type)` – akash87 Nov 02 '20 at 20:46
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Nov 02 '20 at 20:46
  • Thanks for the comment akash87. It didn't work unfortunately. – Francisco Llauger Nov 02 '20 at 21:24

1 Answers1

0

It's possible the issue is because you have decade as numeric. If we switch it to factor, it seems to work I think. I will note that I kept the four categories (You mentioned only three, but your graph showed four), and that if the stat_boxplot has width of .2 it doesn't seem to align correctly

    set.seed(123)
df1<-data.frame("decade" = sample(c(1950, 1960, 1970, 1980, 1990, 2000), size = 500, replace = T),
                "svl" = sample(60:100, size = 500, replace = T),
                "local_type" = c("HU", "LU", "R", "U")
)



ggplot(df1) + aes(factor(decade), svl, fill=local_type) + 
  geom_boxplot(na.rm = TRUE) +
  theme_classic()+ stat_boxplot(geom = 'errorbar')

enter image description here

Silentdevildoll
  • 1,187
  • 1
  • 6
  • 12