Now that we have covered the important aspects of ggplot
, meaning getting the actual plot you wanted, let us now look into secondary elements of the plot.
If we look at the iris
data plot that we made before:
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(color=Sepal.Width))+
geom_smooth(method="lm")
The continuous x axis breaks are with 2,4,6 and so on. If we would like to have 1,2,3… We change this using scale_x_continuous()
and breaks
.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(size=Sepal.Width, color=Species))+
geom_smooth(method="lm") +
scale_x_continuous(breaks = 1:7)
You can do the same with y-axis
.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(size=Sepal.Width, color=Species))+
geom_smooth(method="lm") +
scale_x_continuous(breaks = 1:7) +
scale_y_continuous(breaks = seq(0,3,0.5))
By using limits
, we can also decide on the parts to plot to be shown:
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(size=Sepal.Width, color=Species)) +
geom_smooth(method="lm") +
scale_x_continuous(limits=c(3, 7))
We can do the same with discrete x values like in the case of our gene counts
dataset.
gc_long %>%
group_by(Time, Replicate) %>%
summarise(mean=mean(log10(count +1)),se=se(log10(count +1))) %>%
ggplot(aes(x=Time, y=mean, fill = Replicate)) +
geom_col() +
scale_x_discrete(limits=c("t0","t24"))
One can also use xlim()
and ylim()
functions that function the same as limits
with scale_x_continous()
or scale_x_discrete()
You can also customize the axis labels using the name
option within scale_x_continous
and scale_y_continous
.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(size=Sepal.Width, color=Species))+
geom_smooth(method="lm") +
scale_x_continuous(name = "Length", breaks = 1:7) +
scale_y_continuous(name = "Width", breaks = seq(0,3,0.5))
with labels
in combination with the scales
package, one can change or make the unit of the axis look more comprehensible, when needed. Like using percentage
option or scientific
option.
library(scales)
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(size=Sepal.Width, color=Species))+
geom_smooth(method="lm") +
scale_y_continuous(name = "Width", breaks = seq(0,3,0.5), labels = scientific)
There are many ways to control the legends, below are some of the examples:
First by using guides()
function.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(color=Species,size=Sepal.Width))+
guides(size="none")
We can also turn off legends by geom.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(color=Species,size=Sepal.Width),show.legend=FALSE)
The legends can be edited by scale_<aesthetic>_<discrete or continous>
function that we have been using. Take the below figure for example, we have the Sepal.Width
and the Species
with the size
and color
aestheitcs respectively.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(size=Sepal.Width, color=Species))+
geom_smooth(method="lm")
Let’s try to edit the legends here like mentioned before:
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(size=Sepal.Width, color=Species))+
geom_smooth(method="lm") +
scale_size_continuous(name = "Width of Sepal") +
scale_color_discrete(name = "Organism", labels = c("Iris setosa", "Iris versicolor", "Iris virginica"))
Legends can be moved around using theme.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(color=Species,size=Sepal.Width)) +
theme(legend.position="top",
legend.justification="right")
Legend rows can be controlled in a finer manner.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width))+
geom_point(aes(color=Species,size=Sepal.Width))+
guides(size=guide_legend(nrow=2,byrow=TRUE),
color=guide_legend(nrow=3,byrow=T))+
theme(legend.position="top",
legend.justification="right")
Now that we started into theme()
, it is possible to much more editing of the plot with this function. Let us look into some of the parameters that would be very helpful to work with.
You can change the style of the axis texts in the following way:
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width)) +
geom_point(aes(color=Species,size=Sepal.Width)) +
scale_x_continuous(name = "Length", breaks = 1:7) +
scale_y_continuous(name = "Width", breaks = seq(0,3,0.5)) +
theme(axis.text.x = element_text(face="bold", color="#993333", size=14, angle=45),
axis.text.y = element_text(face="bold", color="#993333", size=14, angle=45))
It is also possible hide the ticks
.
ggplot(data=iris,mapping=aes(x=Petal.Length,y=Petal.Width)) +
geom_point(aes(color=Species,size=Sepal.Width)) +
scale_x_continuous(name = "Length", breaks = 1:7) +
scale_y_continuous(name = "Width", breaks = seq(0,3,0.5)) +
theme(axis.text.x = element_text(face="bold", color="#993333", size=14, angle=45),
axis.text.y = element_text(face="bold", color="#993333", size=14, angle=45),
axis.ticks = element_blank())
There are many things one can use to style the axis and/or axis labels. Just use ?theme()
to look for all the different one can use to stylize the plots.
Let’s consider the plot below and save it as an object P
for the sake of simplicity.
P <- gc_long %>%
group_by(Time, Replicate) %>%
summarise(mean=mean(log10(count +1)),se=se(log10(count +1))) %>%
ggplot(aes(x= Time, y= mean, fill = Replicate)) +
geom_col(position = position_dodge2()) +
geom_errorbar(aes(ymin=mean-se, ymax=mean+se), position = position_dodge2(.9, padding = .6)) +
theme(axis.ticks = element_blank())
P
theme_light()
, theme_minimal()
, theme_classic()
and theme_bw()
are a couple of themes that are used very often in publications.
P + theme_bw(base_size = 16)
P + theme_minimal(base_size = 16)
Let’s look into some of the fancier themes that comes in this package
Q <- ggplot(data = gc_long, mapping = aes(x = Sample_Name, y = log10(count + 1), fill = Time)) + geom_boxplot()
Q
Using the theme_tufte()
library(ggthemes)
Q + theme_tufte()
Q + theme_economist() +
scale_color_economist()
Q + theme_stata() +
scale_color_stata()
Task Try to replicate the plot below if you have enough time.
Tip: 1 geom_line()
is a bit tricky when you use it together with groups
. It tries to draw lines within the group. In this case, if you would like to draw lines between the groups (like in the above plot, between t0
through t2
to t24
), you initate the ggplot with aesthetics for the line and add geom_line(aes(group=1))
this way.
Tip: 2 This figure has theme_light()
sessionInfo()
## R version 4.1.3 (2022-03-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
##
## locale:
## [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
## [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
## [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
## [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggthemes_5.0.0 scales_1.3.0 ggrepel_0.9.5
## [4] wesanderson_0.3.7 forcats_1.0.0 stringr_1.5.1
## [7] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1
## [10] tibble_3.2.1 tidyverse_2.0.0 reshape2_1.4.4
## [13] ggplot2_3.4.4 formattable_0.2.1 kableExtra_1.4.0
## [16] dplyr_1.1.4 lubridate_1.9.3 leaflet_2.2.1
## [19] yaml_2.3.8 fontawesome_0.5.2.9000 captioner_2.2.3
## [22] bookdown_0.37 knitr_1.45
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.12 svglite_2.1.3 lattice_0.20-45 digest_0.6.34
## [5] utf8_1.2.4 R6_2.5.1 plyr_1.8.9 evaluate_0.23
## [9] highr_0.10 pillar_1.9.0 rlang_1.1.3 rstudioapi_0.15.0
## [13] jquerylib_0.1.4 Matrix_1.6-5 rmarkdown_2.25 labeling_0.4.3
## [17] splines_4.1.3 htmlwidgets_1.6.4 munsell_0.5.0 compiler_4.1.3
## [21] xfun_0.41 pkgconfig_2.0.3 systemfonts_1.0.5 mgcv_1.8-39
## [25] htmltools_0.5.7 tidyselect_1.2.0 fansi_1.0.6 viridisLite_0.4.2
## [29] tzdb_0.4.0 withr_3.0.0 grid_4.1.3 nlme_3.1-155
## [33] jsonlite_1.8.8 gtable_0.3.4 lifecycle_1.0.4 magrittr_2.0.3
## [37] cli_3.6.2 stringi_1.8.3 cachem_1.0.8 farver_2.1.1
## [41] xml2_1.3.6 bslib_0.6.1 ellipsis_0.3.2 generics_0.1.3
## [45] vctrs_0.6.5 tools_4.1.3 glue_1.7.0 hms_1.1.3
## [49] crosstalk_1.2.1 fastmap_1.1.1 timechange_0.3.0 colorspace_2.1-0
## [53] sass_0.4.8