diff --git a/docs/404.html b/docs/404.html index 7d5bbbd..6cb5599 100644 --- a/docs/404.html +++ b/docs/404.html @@ -1,74 +1,34 @@ - - -
- + + + + -Issues are meant to report bugs or request features. If you have questions about how to correctly use this package, please don’t use the issue system. Such questions can be asked on stackoverflow https://stackoverflow.com/ or the RStudio community https://community.rstudio.com/. If you are not sure where to go, please try https://stackoverflow.com/ first.
-Issues must contain reproducible code examples. Please use the reprex package to create your example (see here: http://reprex.tidyverse.org/). Issues without reprex may be closed without comment.
+Issues are meant to report bugs or request features. If you have questions about how to correctly use this package, please don’t use the issue system. Such questions can be asked on stackoverflow https://stackoverflow.com/ or the RStudio community https://community.rstudio.com/. If you are not sure where to go, please try https://stackoverflow.com/ first.
+Issues must contain reproducible code examples. Please use the reprex package to create your example (see here: http://reprex.tidyverse.org/). Issues without reprex may be closed without comment.
Please delete these instructions after you have read them.
-Brief description of the problem or desired feature.
-# insert reprex here
+Brief description of the problem or desired feature.
+{r} # insert reprex here
vignettes/aligning_plots.Rmd
+ Source: vignettes/aligning_plots.Rmd
aligning_plots.Rmd
There are several packages that can be used to align plots. The most widely used ones beside cowplot are egg and patchwork. All these packages use slightly different approaches to plot alignment, and the respective approaches have different strengths and weaknesses. If you cannot achieve your desired result with one of these packages try another one.
-Most importantly, while egg and patchwork align and arrange plots at the same time, cowplot aligns plots independently of how they are arranged. This makes it possible to align plots and then reproduce them separately, or even overlay them on top of each other.
-We’ll start with a very simple example. Let’s consider these two plots.
--library(ggplot2) -library(cowplot) - -p1 <- ggplot(mtcars, aes(disp, mpg)) + - geom_point() -p2 <- ggplot(mtcars, aes(disp, hp)) + - geom_point() -p1 -
There are several packages that can be used to align plots. The most +widely used ones beside cowplot are egg and patchwork. All these +packages use slightly different approaches to plot alignment, and the +respective approaches have different strengths and weaknesses. If you +cannot achieve your desired result with one of these packages try +another one.
+Most importantly, while egg and patchwork align and arrange plots at +the same time, cowplot aligns plots independently of how they are +arranged. This makes it possible to align plots and then reproduce them +separately, or even overlay them on top of each other.
+We’ll start with a very simple example. Let’s consider these two +plots.
+
+library(ggplot2)
+library(cowplot)
+
+p1 <- ggplot(mtcars, aes(disp, mpg)) +
+ geom_point()
+p2 <- ggplot(mtcars, aes(disp, hp)) +
+ geom_point()
+p1
-p2
-
+p2
Both plots have the same x axis, but the width of the plot area is slightly larger in the first than in the second, because the y axis labels have different lengths. These differences in plot width could be distracting in a document where both plots are shown near each other. We can fix this problem by vertically aligning the two plots and then drawing them individually.
--aligned <- align_plots(p1, p2, align = "v") -ggdraw(aligned[[1]]) -
Both plots have the same x axis, but the width of the plot area is +slightly larger in the first than in the second, because the y axis +labels have different lengths. These differences in plot width could be +distracting in a document where both plots are shown near each other. We +can fix this problem by vertically aligning the two plots and then +drawing them individually.
+
+aligned <- align_plots(p1, p2, align = "v")
+ggdraw(aligned[[1]])
-ggdraw(aligned[[2]]) -
+ggdraw(aligned[[2]])
Vertical alignment (align = "v"
) means that any vertical reference lines, such as the right and left y axis lines, are aligned in the plots. By contrast, horizontal alignment (align = "h"
) aligns horizontal reference lines. Both types of alignment can be used separately (align = "v"
or align = "h"
) or in combination (align = "vh"
or align = "hv"
).
If we want to align and then arrange plots, we can call plot_grid()
and provide it with an align
argument. The plot_grid()
function calls align_plots()
to align the plots and then arranges them.
-plot_grid(p1, p2, ncol = 1, align = "v") -
Vertical alignment (align = "v"
) means that any vertical
+reference lines, such as the right and left y axis lines, are aligned in
+the plots. By contrast, horizontal alignment (align = "h"
)
+aligns horizontal reference lines. Both types of alignment can be used
+separately (align = "v"
or align = "h"
) or in
+combination (align = "vh"
or
+align = "hv"
).
If we want to align and then arrange plots, we can call
+plot_grid()
and provide it with an align
+argument. The plot_grid()
function calls
+align_plots()
to align the plots and then arranges
+them.
+plot_grid(p1, p2, ncol = 1, align = "v")
The vertical and horizontal alignment as described above tries to align every vertical or horizontal element in all plots. In other words, the axis titles are aligned to each other, the axis ticks are aligned to each other, the plot panels are aligned to each other, and so on. This method of alignment only works if all plots have exactly the same elements. Therefore, it fails, for example, if we try to align a plot that is faceted with one that is not.
--p1 <- ggplot(mtcars, aes(disp, mpg)) + - geom_point() + - theme_minimal_grid(14) + - panel_border(color = "black") - -p2 <- ggplot(mtcars, aes(factor(vs))) + - geom_bar() + - facet_wrap(~am) + - scale_y_continuous(expand = expansion(mult = c(0, 0.1))) + - theme_minimal_hgrid(12) + - panel_border(color = "black") + - theme(strip.background = element_rect(fill = "gray80")) - -plot_grid(p1, p2, align = "h", rel_widths = c(1, 1.3)) -
## Warning: Graphs cannot be horizontally aligned unless the axis parameter is set.
-## Placing graphs unaligned.
+The vertical and horizontal alignment as described above tries to +align every vertical or horizontal element in all plots. In other words, +the axis titles are aligned to each other, the axis ticks are aligned to +each other, the plot panels are aligned to each other, and so on. This +method of alignment only works if all plots have exactly the same +elements. Therefore, it fails, for example, if we try to align a plot +that is faceted with one that is not.
+
+p1 <- ggplot(mtcars, aes(disp, mpg)) +
+ geom_point() +
+ theme_minimal_grid(14) +
+ panel_border(color = "black")
+
+p2 <- ggplot(mtcars, aes(factor(vs))) +
+ geom_bar() +
+ facet_wrap(~am) +
+ scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
+ theme_minimal_hgrid(12) +
+ panel_border(color = "black") +
+ theme(strip.background = element_rect(fill = "gray80"))
+
+plot_grid(p1, p2, align = "h", rel_widths = c(1, 1.3))
## Warning: Graphs cannot be horizontally aligned unless the axis parameter is
+## set. Placing graphs unaligned.
-We receive a warning that the plots cannot be aligned, and the plots are drawn without alignment. To align these plots, we need to tell the align_plots()
function which parts of the plots should be aligned. There are two alternatives, and both can be meaningful. First, we can align only the bottom axis (axis = "b"
).
We receive a warning that the plots cannot be aligned, and the plots
+are drawn without alignment. To align these plots, we need to tell the
+align_plots()
function which parts of the plots should be
+aligned. There are two alternatives, and both can be meaningful. First,
+we can align only the bottom axis (axis = "b"
).
Second, we can align both the bottom and the top axis (axis = "bt"
).
Second, we can align both the bottom and the top axis
+(axis = "bt"
).
In general, the axis
argument tells the align_plots()
function to only align specific axes. It understands the values "t"
(top), "r"
(right), "b"
(bottom), and "l"
(left), in any combination.
As a second example, I will show how aligning by axis can be useful even if the plots contain the same number of elements. Consider this vertical arrangement of two plots, showing first the number of cars in different classes in the mpg
dataset and second a scatter plot of the mean city mpg for each class versus the number of cars in the class.
-library(dplyr) -library(forcats) - -city_mpg <- mpg %>% - mutate(class = fct_lump(class, 4, other_level = "other")) %>% - group_by(class) %>% - summarize( - mean_mpg = mean(cty), - count = n() - ) %>% mutate( - class = fct_reorder(class, count) - ) - - -p1 <- ggplot(city_mpg, aes(class, count)) + - geom_col() + - ylim(0, 65) + - coord_flip() - -p2 <- ggplot(city_mpg, aes(mean_mpg, count)) + - geom_point() - -plot_grid(p1, p2, ncol = 1, align = 'v') -
In general, the axis
argument tells the
+align_plots()
function to only align specific axes. It
+understands the values "t"
(top), "r"
(right),
+"b"
(bottom), and "l"
(left), in any
+combination.
As a second example, I will show how aligning by axis can be useful
+even if the plots contain the same number of elements. Consider this
+vertical arrangement of two plots, showing first the number of cars in
+different classes in the mpg
dataset and second a scatter
+plot of the mean city mpg for each class versus the number of cars in
+the class.
+library(dplyr)
+library(forcats)
+
+city_mpg <- mpg %>%
+ mutate(class = fct_lump(class, 4, other_level = "other")) %>%
+ group_by(class) %>%
+ summarize(
+ mean_mpg = mean(cty),
+ count = n()
+ ) %>% mutate(
+ class = fct_reorder(class, count)
+ )
+
+
+p1 <- ggplot(city_mpg, aes(class, count)) +
+ geom_col() +
+ ylim(0, 65) +
+ coord_flip()
+
+p2 <- ggplot(city_mpg, aes(mean_mpg, count)) +
+ geom_point()
+
+plot_grid(p1, p2, ncol = 1, align = 'v')
Because the y axis labels of the first plot are very long, the y axis title of the second plot is located far removed from the axis labels. This looks strange. It would be better to move the axis title closer to the labels. We can do this by aligning by axis. I’m aligning here only the left axis. Aligning the left and the right axis would have produced the same result.
--plot_grid(p1, p2, ncol = 1, align = 'v', axis = 'l') -
Because the y axis labels of the first plot are very long, the y axis +title of the second plot is located far removed from the axis labels. +This looks strange. It would be better to move the axis title closer to +the labels. We can do this by aligning by axis. I’m aligning here only +the left axis. Aligning the left and the right axis would have produced +the same result.
+
+plot_grid(p1, p2, ncol = 1, align = 'v', axis = 'l')
Finally, I’ll make two separate plots and then draw them on top of each other. First, let’s again draw a bar plot of the number of cars in different classes in the mpg
dataset.
-city_mpg <- city_mpg %>% - mutate(class = fct_reorder(class, -mean_mpg)) - -p1 <- ggplot(city_mpg, aes(class, count)) + - geom_col(fill = "#6297E770") + - scale_y_continuous( - expand = expansion(mult = c(0, 0.05)), - position = "right" - ) + - theme_minimal_hgrid(11, rel_small = 1) + - theme( - panel.grid.major = element_line(color = "#6297E770"), - axis.line.x = element_blank(), - axis.text.x = element_blank(), - axis.title.x = element_blank(), - axis.ticks = element_blank(), - axis.ticks.length = grid::unit(0, "pt"), - axis.text.y = element_text(color = "#6297E7"), - axis.title.y = element_text(color = "#6297E7") - ) -p1 -
Finally, I’ll make two separate plots and then draw them on top of
+each other. First, let’s again draw a bar plot of the number of cars in
+different classes in the mpg
dataset.
+city_mpg <- city_mpg %>%
+ mutate(class = fct_reorder(class, -mean_mpg))
+
+p1 <- ggplot(city_mpg, aes(class, count)) +
+ geom_col(fill = "#6297E770") +
+ scale_y_continuous(
+ expand = expansion(mult = c(0, 0.05)),
+ position = "right"
+ ) +
+ theme_minimal_hgrid(11, rel_small = 1) +
+ theme(
+ panel.grid.major = element_line(color = "#6297E770"),
+ axis.line.x = element_blank(),
+ axis.text.x = element_blank(),
+ axis.title.x = element_blank(),
+ axis.ticks = element_blank(),
+ axis.ticks.length = grid::unit(0, "pt"),
+ axis.text.y = element_text(color = "#6297E7"),
+ axis.title.y = element_text(color = "#6297E7")
+ )
+p1
Now let’s plot mean city mpg versus car class, as a scatter plot.
--p2 <- ggplot(city_mpg, aes(class, mean_mpg)) + - geom_point(size = 3, color = "#D5442D") + - scale_y_continuous(limits = c(10, 21)) + - theme_half_open(11, rel_small = 1) + - theme( - axis.ticks.y = element_line(color = "#BB2D05"), - axis.text.y = element_text(color = "#BB2D05"), - axis.title.y = element_text(color = "#BB2D05"), - axis.line.y = element_line(color = "#BB2D05") - ) -p2 -
+p2 <- ggplot(city_mpg, aes(class, mean_mpg)) +
+ geom_point(size = 3, color = "#D5442D") +
+ scale_y_continuous(limits = c(10, 21)) +
+ theme_half_open(11, rel_small = 1) +
+ theme(
+ axis.ticks.y = element_line(color = "#BB2D05"),
+ axis.text.y = element_text(color = "#BB2D05"),
+ axis.title.y = element_text(color = "#BB2D05"),
+ axis.line.y = element_line(color = "#BB2D05")
+ )
+p2
Now we align the two plots and then overlay them. The resulting plot has dual axes, and making such plots is usually discouraged. I present this example here only to show the types of effects we can achieve when plot alignment and plot placement are separated.
--aligned_plots <- align_plots(p1, p2, align="hv", axis="tblr") -ggdraw(aligned_plots[[1]]) + draw_plot(aligned_plots[[2]]) -
Now we align the two plots and then overlay them. The resulting plot +has dual axes, and making such plots is usually discouraged. I present +this example here only to show the types of effects we can achieve when +plot alignment and plot placement are separated.
+
+aligned_plots <- align_plots(p1, p2, align="hv", axis="tblr")
+ggdraw(aligned_plots[[1]]) + draw_plot(aligned_plots[[2]])
Developed by Claus O. Wilke.
+ +Developed by Claus O. Wilke.
vignettes/drawing_with_on_plots.Rmd
+ Source: vignettes/drawing_with_on_plots.Rmd
drawing_with_on_plots.Rmd
The cowplot package provides functions to draw with and on plots. These functions enable us to take plots and add arbitrary annotations or backgrounds, to place plots inside of other plots, to arrange plots in more complicated layouts, or to combine plots from different graphic systems (ggplot2, grid, lattice, base). This functionality is build on top of ggplot2, i.e., the resulting plots are ggplot2 objects and they can be modified, extended, and outputted just like regular ggplot2 plots.
-We start with some simple annotations, such as labels or watermarks. Let’s begin with a basic plot of the mpg
dataset.
-library(ggplot2) -library(cowplot) - -p <- ggplot(mpg, aes(displ, cty)) + - geom_point() + - theme_minimal_grid(12) - -p -
The cowplot package provides functions to draw with and on plots. +These functions enable us to take plots and add arbitrary annotations or +backgrounds, to place plots inside of other plots, to arrange plots in +more complicated layouts, or to combine plots from different graphic +systems (ggplot2, grid, lattice, base). This functionality is build on +top of ggplot2, i.e., the resulting plots are ggplot2 objects and they +can be modified, extended, and outputted just like regular ggplot2 +plots.
+We start with some simple annotations, such as labels or watermarks.
+Let’s begin with a basic plot of the mpg
dataset.
+library(ggplot2)
+library(cowplot)
+
+p <- ggplot(mpg, aes(displ, cty)) +
+ geom_point() +
+ theme_minimal_grid(12)
+
+p
Next we’re going to watermark this plot with the word “Draft”. To do so, we wrap the plot into a drawing environment via the ggdraw()
call, and then add annotations via various draw_*()
functions.
-ggdraw(p) + - draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) -
Next we’re going to watermark this plot with the word “Draft”. To do
+so, we wrap the plot into a drawing environment via the
+ggdraw()
call, and then add annotations via various
+draw_*()
functions.
+ggdraw(p) +
+ draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45)
What ggdraw(p)
does is it captures a snapshot of the plot, so that the plot effectively turns into an image, and then it draws that image into a new ggplot2 canvas without visible axes or background grid. The draw_*
functions are simply wrappers around regular geoms. So, in the above example we could have used geom_text()
instead of draw_label()
.
-ggdraw(p) + - geom_text( - data = data.frame(x = 0.5, y = 0.5, label = "Draft"), - aes(x, y, label = label), - hjust = 0.5, vjust = 0.5, angle = 45, size = 100/.pt, - color = "#C0A0A0", - inherit.aes = FALSE - ) -
What ggdraw(p)
does is it captures a snapshot of the
+plot, so that the plot effectively turns into an image, and then it
+draws that image into a new ggplot2 canvas without visible axes or
+background grid. The draw_*
functions are simply wrappers
+around regular geoms. So, in the above example we could have used
+geom_text()
instead of draw_label()
.
+ggdraw(p) +
+ geom_text(
+ data = data.frame(x = 0.5, y = 0.5, label = "Draft"),
+ aes(x, y, label = label),
+ hjust = 0.5, vjust = 0.5, angle = 45, size = 100/.pt,
+ color = "#C0A0A0",
+ inherit.aes = FALSE
+ )
However, notice how much more verbose the call to geom_text()
is. Also, geom_text()
interprets font sizes in mm, so we need to divide by .pt
if we want to specify font sizes in the more conventional point metric. By contrast, draw_label()
performs this conversion for us, so we can specify font sizes directly in points.
Because ggdraw()
is built on top of ggplot2, we can treat its output like a ggplot2 plot. For example, we can use the theme()
function to change the background color.
-ggdraw(p) + - draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) + - theme( - plot.background = element_rect(fill = "cornsilk", color = NA) - ) -
However, notice how much more verbose the call to
+geom_text()
is. Also, geom_text()
interprets
+font sizes in mm, so we need to divide by .pt
if we want to
+specify font sizes in the more conventional point metric. By contrast,
+draw_label()
performs this conversion for us, so we can
+specify font sizes directly in points.
Because ggdraw()
is built on top of ggplot2, we can
+treat its output like a ggplot2 plot. For example, we can use the
+theme()
function to change the background color.
+ggdraw(p) +
+ draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) +
+ theme(
+ plot.background = element_rect(fill = "cornsilk", color = NA)
+ )
We can also save the annotated plots in the standard way via ggsave()
.
-draft <- ggdraw(p) + - draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) - -ggsave("draft.pdf", draft) -
(However, the cowplot package provides an alternative to ggsave()
, the function save_plot()
, which makes it easier to save plots with appropriate sizing, in particular when making compound plots. See the documentation of save_plot()
for details.)
Frequently, we may want to have the annotations underneath the plot rather than on top of it. We can achieve this effect by first setting up an empty drawing layer with ggdraw()
, then adding the label, and then adding the plot with draw_plot()
.
-ggdraw() + - draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) + - draw_plot(p) -
We can also save the annotated plots in the standard way via
+ggsave()
.
+draft <- ggdraw(p) +
+ draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45)
+
+ggsave("draft.pdf", draft)
(However, the cowplot package provides an alternative to
+ggsave()
, the function save_plot()
, which
+makes it easier to save plots with appropriate sizing, in particular
+when making compound plots. See the documentation of
+save_plot()
for details.)
Frequently, we may want to have the annotations underneath the plot
+rather than on top of it. We can achieve this effect by first setting up
+an empty drawing layer with ggdraw()
, then adding the
+label, and then adding the plot with draw_plot()
.
+ggdraw() +
+ draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) +
+ draw_plot(p)
This requires that the plot has a transparent background, and all cowplot themes meet this requirement. By contrast, this is not necessarily the case for ggplot2 themes. For example, if we change the theme of the plot to theme_classic()
the underlying label is hidden by the theme’s white background.
-ggdraw() + - draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) + - draw_plot( - p + theme_classic() - ) -
This requires that the plot has a transparent background, and all
+cowplot themes meet this requirement. By contrast, this is not
+necessarily the case for ggplot2 themes. For example, if we change the
+theme of the plot to theme_classic()
the underlying label
+is hidden by the theme’s white background.
+ggdraw() +
+ draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) +
+ draw_plot(
+ p + theme_classic()
+ )
The cowplot theme theme_half_open()
does not have this limitation.
-ggdraw() + - draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) + - draw_plot( - p + theme_half_open(12) - ) -
The cowplot theme theme_half_open()
does not have this
+limitation.
+ggdraw() +
+ draw_label("Draft", color = "#C0A0A0", size = 100, angle = 45) +
+ draw_plot(
+ p + theme_half_open(12)
+ )
The draw_plot()
function also allows us to place plots at arbitrary locations and at arbitrary sizes onto the canvas. This is useful for combining subplots into a layout that is not a simple grid, e.g. with an inset plotted inside a larger graph.
-inset <- ggplot(mpg, aes(drv)) + - geom_bar(fill = "skyblue2", alpha = 0.7) + - scale_y_continuous(expand = expansion(mult = c(0, 0.05))) + - theme_minimal_hgrid(11) - -ggdraw(p + theme_half_open(12)) + - draw_plot(inset, .45, .45, .5, .5) + - draw_plot_label( - c("A", "B"), - c(0, 0.45), - c(1, 0.95), - size = 12 - ) -
The draw_plot()
function also allows us to place plots
+at arbitrary locations and at arbitrary sizes onto the canvas. This is
+useful for combining subplots into a layout that is not a simple grid,
+e.g. with an inset plotted inside a larger graph.
+inset <- ggplot(mpg, aes(drv)) +
+ geom_bar(fill = "skyblue2", alpha = 0.7) +
+ scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
+ theme_minimal_hgrid(11)
+
+ggdraw(p + theme_half_open(12)) +
+ draw_plot(inset, .45, .45, .5, .5) +
+ draw_plot_label(
+ c("A", "B"),
+ c(0, 0.45),
+ c(1, 0.95),
+ size = 12
+ )
This feature is not limited to ggplot2 plots. It works with base graphics as well.
--inset <- ~{ - counts <- table(mpg$drv) - par( - cex = 0.8, - mar = c(3, 3, 1, 1), - mgp = c(2, 1, 0) - ) - barplot(counts, xlab = "drv", ylab = "count") -} - -ggdraw(p + theme_half_open(12)) + - draw_plot(inset, .45, .45, .5, .5) + - draw_plot_label( - c("A", "B"), - c(0, 0.45), - c(1, 0.95), - size = 12 - ) -
This feature is not limited to ggplot2 plots. It works with base +graphics as well.
+
+inset <- ~{
+ counts <- table(mpg$drv)
+ par(
+ cex = 0.8,
+ mar = c(3, 3, 1, 1),
+ mgp = c(2, 1, 0)
+ )
+ barplot(counts, xlab = "drv", ylab = "count")
+}
+
+ggdraw(p + theme_half_open(12)) +
+ draw_plot(inset, .45, .45, .5, .5) +
+ draw_plot_label(
+ c("A", "B"),
+ c(0, 0.45),
+ c(1, 0.95),
+ size = 12
+ )
By default, the coordinate system used by ggdraw()
uses relative coordinates running from 0 to 1 for both x and y. Sometimes, however, we need to be able to place graphical elements at absolute units, e.g. exactly 1 inch from the left border. We can do so via the grid graphic system, which is supported through the draw_grob()
function. For example, the following code generates a blue square of 1 inch width and height that is located exactly 1 inch from the left and 1 inch from the top border of the plot area.
-library(grid) -rect <- rectGrob( - x = unit(1, "in"), - y = unit(1, "npc") - unit(1, "in"), - width = unit(1, "in"), - height = unit(1, "in"), - hjust = 0, vjust = 1, - gp = gpar(fill = "skyblue2", alpha = 0.5) -) - -ggdraw(p) + - draw_grob(rect) -
By default, the coordinate system used by ggdraw()
uses
+relative coordinates running from 0 to 1 for both x and y. Sometimes,
+however, we need to be able to place graphical elements at absolute
+units, e.g. exactly 1 inch from the left border. We can do so via the
+grid graphic system, which is supported through the
+draw_grob()
function. For example, the following code
+generates a blue square of 1 inch width and height that is located
+exactly 1 inch from the left and 1 inch from the top border of the plot
+area.
+library(grid)
+rect <- rectGrob(
+ x = unit(1, "in"),
+ y = unit(1, "npc") - unit(1, "in"),
+ width = unit(1, "in"),
+ height = unit(1, "in"),
+ hjust = 0, vjust = 1,
+ gp = gpar(fill = "skyblue2", alpha = 0.5)
+)
+
+ggdraw(p) +
+ draw_grob(rect)
If we regenerate the plot in a different size, the blue square remains at the same absolute position and retains its absolute size.
- +If we regenerate the plot in a different size, the blue square +remains at the same absolute position and retains its absolute size.
+The drawing layer also supports images, through the function draw_image()
. This function, which requires the magick package to be installed, can take images in many different formats and combine them with plots. For example, we can use an image as a plot background:
-library(magick) -library(dplyr) -library(forcats) - -img <- system.file("extdata", "cow.jpg", package = "cowplot") %>% - image_read() %>% - image_resize("570x380") %>% - image_colorize(35, "white") - -p <- PASWR::Cows %>% - filter(breed != "Canadian") %>% - mutate(breed = fct_reorder(breed, butterfat)) %>% - ggplot(aes(butterfat, fill = breed)) + - geom_density(alpha = 0.7) + - scale_fill_grey() + - coord_cartesian(expand = FALSE) + - theme_minimal_hgrid(11, color = "grey30") - -ggdraw() + - draw_image(img) + - draw_plot(p) -
The drawing layer also supports images, through the function
+draw_image()
. This function, which requires the magick package to
+be installed, can take images in many different formats and combine them
+with plots. For example, we can use an image as a plot background:
+library(magick)
+library(dplyr)
+library(forcats)
+
+img <- system.file("extdata", "cow.jpg", package = "cowplot") %>%
+ image_read() %>%
+ image_resize("570x380") %>%
+ image_colorize(35, "white")
+
+p <- PASWR::Cows %>%
+ filter(breed != "Canadian") %>%
+ mutate(breed = fct_reorder(breed, butterfat)) %>%
+ ggplot(aes(butterfat, fill = breed)) +
+ geom_density(alpha = 0.7) +
+ scale_fill_grey() +
+ coord_cartesian(expand = FALSE) +
+ theme_minimal_hgrid(11, color = "grey30")
+
+ggdraw() +
+ draw_image(img) +
+ draw_plot(p)
We can also add an image as a logo onto a plot. We use halign
and valign
in addition to hjust
and vjust
to align the image flush in the top right corner.
-logo_file <- system.file("extdata", "logo.png", package = "cowplot") - -ggdraw() + - draw_plot(p) + - draw_image( - logo_file, x = 1, y = 1, hjust = 1, vjust = 1, halign = 1, valign = 1, - width = 0.15 - ) -
We can also add an image as a logo onto a plot. We use
+halign
and valign
in addition to
+hjust
and vjust
to align the image flush in
+the top right corner.
+logo_file <- system.file("extdata", "logo.png", package = "cowplot")
+
+ggdraw() +
+ draw_plot(p) +
+ draw_image(
+ logo_file, x = 1, y = 1, hjust = 1, vjust = 1, halign = 1, valign = 1,
+ width = 0.15
+ )
Developed by Claus O. Wilke.
+ +Developed by Claus O. Wilke.