p1 <- ggplot(data= <DATA>) +
aes(<MAPPINGS>) +
<GEOM_FUNCTION>(aes(<MAPPINGS>),
stat=<STAT>,
position=<POSITION>) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>
print(p1)
ggsave(plot=p1,
filename="MyPlot",
width=5,
height=3,
units="in",
device="pdf")
Use package Inkscape
## tibble [234 × 11] (S3: tbl_df/tbl/data.frame)
## $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
## $ model : chr [1:234] "a4" "a4" "a4" "a4" ...
## $ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
## $ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
## $ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
## $ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
## $ drv : chr [1:234] "f" "f" "f" "f" ...
## $ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
## $ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
## $ fl : chr [1:234] "p" "p" "p" "p" ...
## $ class : chr [1:234] "compact" "compact" "compact" "compact" ...
##
## c d e p r
## 1 5 8 52 168
# basic scatter plot
ggplot(data=d) +
aes(x=displ,y=hwy) +
geom_point() +
geom_smooth() +
geom_smooth(method="lm",col="red")
# add a linear regression line
ggplot(data=d) +
aes(x=displ,y=hwy) +
geom_point() +
geom_smooth(method = "lm",col="red")
# bar plot with specified counts or meansw
x_treatment <- c("Control","Low","High")
y_response <- c(12,2.5,22.9)
summary_data <- data.frame(x_treatment,y_response)
ggplot(data=summary_data) +
aes(x=x_treatment,y=y_response) +
geom_col(fill=c("grey50","goldenrod","goldenrod"),col="black")
# basic curves and functions
my_vec <- seq(1,100,by=0.1)
# plot simple mathematical functions
d_frame <- data.frame(x=my_vec,y=sin(my_vec))
ggplot(data=d_frame) +
aes(x=x,y=y) +
geom_line()
# plot probability functions
d_frame <- data.frame(x=my_vec,y=dgamma(my_vec,shape=5, scale=3))
ggplot(data=d_frame) +
aes(x=x,y=y) +
geom_line()
# plot user-defined functions
my_fun <- function(x) sin(x) + 0.1*x
d_frame <- data.frame(x=my_vec,y=my_fun(my_vec))
ggplot(data=d_frame) +
aes(x=x,y=y) +
geom_line()
# use theme parameters to modify font and font size
p1 + theme_classic(base_size=30,base_family="serif")
# defaults: theme_grey, base_size=16,base_family="Helvetica")
# font families (Mac): Times, Ariel, Monaco, Courier, Helvetica, serif,sans
# code for adding additional fonts
library(extrafont)
font_import() # Imports all system fonts (run once)
## Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
## Continue? [y/n]
# loadfonts(device = "win") # For Windows
# fonts() # will list loaded fonts
p1 + theme_classic(base_size=35,
base_family="Chalkduster")
# use coordinate_flip to invert entire plot
p2 <- ggplot(data=d, mapping=aes(x=fl,fill=fl)) + geom_bar()
print(p2)
# use labs for different plot labels
p1 <- ggplot(data=d) +
aes(x=displ,y=cty) +
geom_point() +
labs(title="My graph title here",
subtitle="An extended subtitle",
x="Displacement",
y="City Mileage",
caption="Add a caption here") +
theme_bw(base_size=25,base_family="Monaco")
# xlim(0,4) + ylim(0,20)
print(p1)
# use attributes for point size shape, color
p1 <- ggplot(data=d) +
aes(x=displ,y=cty) +
geom_point(size=4,
shape=21,
color="black",fill="cyan") +
theme_bw(base_size=25,base_family="Monaco")
print(p1)
# use x and/or y limits to clip data set
p1 <- ggplot(data=d) +
aes(x=displ,y=cty) +
geom_point(size=4,
shape=21,
color="black",fill="cyan") +
xlim(4,7) +
ylim(-10,40) +
theme_bw(base_size=25,base_family="Monaco")
print(p1)