Link to Home

Trying different things with ggplot2

# Loading ggplot2

library(ggplot2)
library(ggrepel)

# Reading in my salamander data

sal.data <- read.table("CleanSalamanderData/mysalamanderdata.csv",
                       header = TRUE,
                       sep=",")
head(sal.data)
##   Tank  SVL      Morph Density
## 1    1 34.0 Paedomorph       H
## 2    1 33.0 Paedomorph       H
## 3    1 32.5 Paedomorph       H
## 4    1 32.0 Paedomorph       H
## 5    2 49.0 Paedomorph       L
## 6    2 49.0 Paedomorph       L
# Making a box plot of SVL for different salamander morphs with ggplot

sal.plot <- ggplot(data = sal.data)+
  geom_boxplot(aes(x=Morph, y=SVL),fill="blueviolet", color="black")+
  labs(title = "Snout-Vent Length (SVL) of Salamanders by Morph",
       y="SVL (mm)",
       x="Morph")

sal.plot

# Trying out different colors

sal.plot.2 <- ggplot(data = sal.data)+
  geom_boxplot(aes(x=Morph, y=SVL),fill="cyan4", color="chocolate2")+
  labs(title = "Snout-Vent Length (SVL) of Salamanders by Morph",
       y="SVL (mm)",
       x="Morph")

sal.plot.2

# Increasing the font size

sal.plot + theme(text = element_text(size = 15))

# Adding text label

sal.plot + geom_label(aes(x=1.5,y=30,label="this is random text")) 

# Adding an arrow pointing to something

sal.plot + geom_segment(aes(x = 1, y = 30, xend = 1.6, yend = 39), arrow = arrow(length = unit(0.5, "cm")))

# Flipping the axes

sal.plot + coord_flip()

# Trying the dark theme

sal.plot + theme_dark()

# Trying the minimal theme

sal.plot + theme_minimal()

# Trying the void theme

sal.plot + theme_void()

# Trying the classic theme - this one is my favorite!

best.sal.plot <- sal.plot + theme_classic()

best.sal.plot

# Overlay some dots
best.sal.plot <- best.sal.plot +  geom_point(aes(x=Morph, y=SVL),position=position_jitter(width=0.1, height=0.7),
             color="black", size=2)

best.sal.plot

Exporting my plot to pdf and jpg files

# Using ggsave() to save as a pdf

ggsave(filename = "salPlot.pdf", plot = best.sal.plot)
## Saving 7 x 5 in image
# Using ggsave() to save as a jpg

ggsave(filename = "salPlot.jpg", plot = best.sal.plot)
## Saving 7 x 5 in image