Post

Publication-quality plotting with R

In-house code

Publication-quality plotting with R

Here are some scripts for plotting nice panels, but I haven’t tried many kinds of graphs yet. Will add them later.

Line charts

Here we have the data:

1
2
3
4
5
6
7
8
9
10
11
12
head(summary_data)

A tibble: 6 × 5

Parameter     X   Mean      SE
         
1	0     0    0.330 0.00854
2	0     1    0.327 0.00836
3	0     2    0.321 0.00816
4	0     3    0.312 0.00795
5	0     4    0.303 0.00762
6	0     5    0.292 0.00732

And we want to plot a line chart with Parameter as the grouping factor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
p <- ggplot(summary_data, aes(x = X, y = Mean, color = Parameter, group = Parameter)) +
	geom_line() +
	geom_ribbon(aes(ymin = Mean - SE, ymax = Mean + SE, fill = Parameter),
		alpha = 0.25, color = NA) +
	labs(
		x = "Day",
		y = "DG Similarity",
		color = expression(bolditalic(SI))
	) +
	scale_x_continuous(
        # Change the breaks here
		breaks = seq(min(summary_data$X), max(summary_data$X), by = 2),  
		expand = expansion(mult = c(0.01, 0.01))
	) +
	scale_y_continuous(
		breaks = seq(
			floor(min(summary_data$Mean - summary_data$SE)),
			ceiling(max(summary_data$Mean + summary_data$SE)),
			by = 0.1  # Change the breaks here
		),
		expand = expansion(mult = c(0.02, 0.02))
	) +
	scale_color_viridis_c(option = "plasma", guide = "colorbar") +
	scale_fill_viridis_c(option = "plasma", guide = "none") +
	theme_minimal()+
	theme(
		text = element_text(family = "sans", color = "black"),
		axis.text = element_text(size = 7, color = "black"),
		axis.title = element_text(size = 8, face = "bold"),
		axis.line = element_line(size = 0.4, color = "black"),
		axis.ticks = element_line(size = 0.4, color = "black"),
		legend.position = "right",
		legend.title = element_text(size = 8, face = "bold"),
		legend.text = element_text(size = 7),
		legend.background = element_rect(fill = "white", color = NA),
		legend.key.height = unit(0.5, "cm"),
		legend.key.width = unit(0.2, "cm")
	)

To save the plot as SVG file:

1
2
3
4
5
6
7
8
ggsave(
    "Figure1.svg",
    plot = p,
    width = 60,
    height = 40,
    units = "mm",
    device = "svg"
)

Miscellaneous

To use \(\LaTeX{}\) :

1
2
library("latex2exp")
TeX("\\textbf{Euler's identity} is $e^{i\\pi} + 1 = 0$.")

The color maps provided by viridisLite:

img

Eight options for the option parameter are available:"magma" (or "A")"inferno" (or "B")"plasma" (or "C")"viridis" (or "D")"cividis" (or "E")"rocket" (or "F")"mako" (or "G")"turbo" (or "H").

To reduce spacing around the legend:

1
2
3
4
theme(
  legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0)
)
This post is licensed under CC BY 4.0 by the author.