#> Error in `library()`:
#> ! there is no package called 'paletteer'
#> Error in `loadNamespace()`:
#> ! there is no package called 'paletteer'
scale_fill_* vs. scale_color_*
ggplot( vertebrates,aes(x = section,y = length_1_mm,color = unittype )) +geom_boxplot() +scale_color_paletteer_d(palette ="ggsci::default_uchicago" )#> Error in `scale_color_paletteer_d()`:#> ! could not find function "scale_color_paletteer_d"
ggplot( vertebrates,aes(x = section,y = length_1_mm,fill = unittype )) +geom_boxplot() +scale_fill_paletteer_d(palette ="ggsci::default_uchicago" )#> Error in `scale_fill_paletteer_d()`:#> ! could not find function "scale_fill_paletteer_d"
labs: Change axis and legend titles and add plot title
g <- g +labs(x ="Length [mm]",y ="Weight [g]",color ="Species",title ="Length-Weight relationship",subtitle ="There seems to be an exponential relationship",caption ="Data from the `lterdatasampler` package" )g
labs: Change axis and legend titles and add plot title
theme_*: change appearance
ggplot2 offers many pre-defined themes that we can apply to change the appearance of a plot.
g +theme_classic()
g +theme_bw()
theme_*: change appearance
ggplot2 offers many pre-defined themes that we can apply to change the appearance of a plot.
g +theme_minimal()
g +theme_dark()
theme_*: change appearance
Since ggplot2 v. 4.0.0, you can change overall color choices of a pre-defined theme.
paper: affects background elements
ink: affects foreground elements (text, lines, points, …)
accent: affects elements that are used to highlight information (like geom_smooth() lines)
g +theme_bw(ink ="#BBBBBB",paper ="#333333" )
theme_*: change appearance
Since ggplot2 v. 4.0.0, you can change overall color choices of a pre-defined theme.
paper: affects background elements
ink: affects foreground elements (text, lines, points, …)
accent: affects elements that are used to highlight information (like geom_smooth() lines)
g +theme_minimal(paper ="cornsilk",ink ="navy" )
theme(): customize theme
You can manually change a theme or even create an entire theme yourself. The elements you can control in the theme are:
titles (plot, axis, legend, …)
labels
background
borders
grid lines
legends
If you want a full list of what you can customize, have a look at
?theme
Look here for an overview of the elements that you can change
theme(): customize theme
To edit a theme, just add another theme() layer to your plot.
This is very practical if you want to achieve a consistent look, e.g. for a scientific journal.
ggsave()
A ggplot object can be saved on disk in different formats.
Without specifications:
# save plot g in img as my_plot.pdfggsave(filename ="img/my_plot.pdf", plot = g)# save plot g in img as my_plot.pngggsave(filename ="img/my_plot.png", plot = g)
Or with specifications:
# save a plot named g in the img directory under the name my_plot.png# with width 16 cm and height 9 cmggsave(filename ="img/my_plot.png",plot = g,width =16,heigth =9,units ="cm")