Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Created November 24, 2017 14:27
Show Gist options
  • Save valentinitnelav/3150e3f9f8137b09edd4ab43f61cd9e1 to your computer and use it in GitHub Desktop.
Save valentinitnelav/3150e3f9f8137b09edd4ab43f61cd9e1 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(ggsci) # for cool palettes
# read data
my_data <- read.csv("data/chickengrowthdiet.csv")
str(my_data)
# Diet should not be numeric but factor (or categorical)
my_data$Diet <- factor(my_data$Diet)
# -------------------------------------
# make plot 1
# -------------------------------------
our_plot_1 <-
ggplot(data = my_data,
aes(x = Time,
y = weight,
fill = Diet,
color = Diet)) +
# add the points
geom_point(shape = 1, cex = 1, alpha = 0.7, position = "jitter") +
# add CI ribbon and the fit lines
geom_smooth(method='loess', size = 1) + # or method='loess'
# set axis labels
labs(x = "Days",
y = "Weight (g)") +
theme_bw(base_size = 10) # base font size
# theme(panel.grid = element_blank()) # eliminate grids
# display the plot
our_plot_1
# save the plot as PDF
ggsave(filename = "chicks_loess_default_col.pdf",
plot = our_plot_1,
width = 15,
height = 10,
units = "cm",
scale = 1)
# -------------------------------------
# plot 2
# -------------------------------------
our_plot_2 <-
ggplot(data = my_data,
aes(x = Time,
y = weight,
color = Diet)) +
# add the points
geom_point(shape = 1, cex = 1, alpha = 0.7, position = "jitter") +
# add CI ribbon and the fit lines
geom_smooth(method='loess', size = 1, alpha = 0.2) + # or method='loess'
scale_color_npg() +
# set axis labels
labs(x = "Days",
y = "Weight (g)") +
theme_bw(base_size = 10) # base font size
# theme(panel.grid = element_blank()) # eliminate grids
# display the plot
our_plot_2
# save the plot as PDF
ggsave(filename = "chicks_loess_npg.pdf",
plot = our_plot_2,
width = 15,
height = 10,
units = "cm",
scale = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment