Set text position of panel to top c năm 2024

Since you are using headings, you can re-organize your document quickly. Drag sections instead of cut and paste.

The Navigation Pane

  • Click View > Navigation Pane and to turn on the Navigation Pane. Only words that have a Heading setting will appear here. Click and hold to move them around, all text will move under that setting will also move.

Instant Table of Contents (TOC)

  • Click References > Table of Contents to turn your Headings into an instant table of contents. See the training courses below for more ways to format your TOC.

Want more?

Move around in a document using the Navigation pane

Introduction to Tables of Contents (TOCs)

Take tables of contents (TOCs) to the next level

Another thing that you can do and this started with Word 2010 is the Navigation Pane.

Let’s go show you that.

You go the VIEW tab and click Navigation Pane box.

And on the left hand side of your document, all those headings appear.

Again, just headings appear.

Anything else that you style in a different type of style won’t appear, just headings will appear.

And this is actually kind of your document laid out.

Now, let’s say your teacher, who’s an astronomy teacher, who’s also an English teacher, wants the planets in alphabetical order.

One, you should question that. But two, okay.

If I had to do that on my regular document, I would have to take 'Mercury' and I would have to take this whole thing, and make sure I have all the correct...

If it goes on, I have to cut it, and then I figure out where to put it and scroll down my document, and find the right place and then paste it.

And you do that for a long document and you pick up extra spaces and all that, or here I put it in the wrong place.

It can get pretty cumbersome very quickly, cutting and pasting to reorganize your document.

Let’s look over at the Navigation Pane. And what I can do here is I can drag sections.

Okay, so let’s do this in alphabetical order.

'Earth' is first, then 'Jupiter'. I am just going to click on it, and then unclick when I have it.

All the moons of 'Jupiter' also appear in this order, so I can quickly make this alphabetized.

'Neptune', 'Saturn' goes above 'Venus'. U, V, Venus is last. But before 'The Dwarf' planets.

And there I have quickly organized my document alphabetically and the whole document is changed on the side.

Now, let me go up to the Table of Contents. The Table of Contents does not update automatically like a live preview.

You have to tell it to update it. It is really easy, though. I am just going to click on the Table of Contents.

You can also do this in the ribbon, and you can click Update Table. You can Update just the page numbers. Let’s do that.

And now you can see, Mercury is on Page 4, Saturn is on Page 6.

But I am going to update the whole Table of Contents even in the order.

So Update Table, Update entire table, and now the planets are in alphabetical order, and the Table of Contents is in that order.

So, with Styles, you can change the look of your document quickly, and it is uniform.

You can get an instant Table of Contents and you can organize your document through the Navigation Pane, and change things around very simply.

Themes are a powerful way to customize the non-data components of your plots: i.e. titles, labels, fonts, background, gridlines, and legends. Themes can be used to give plots a consistent customized look. Modify a single plot's theme using theme(); see theme_update() if you want modify the active theme, to affect all subsequent plots. Use the themes available in complete themes if you would like to use a complete theme such as`theme_bw(), theme_minimal()`, and more. Theme elements are documented together according to inheritance, read more about theme inheritance below.

Usage

theme(
  ...,
  line,
  rect,
  text,
  title,
  aspect.ratio,
  axis.title,
  axis.title.x,
  axis.title.x.top,
  axis.title.x.bottom,
  axis.title.y,
  axis.title.y.left,
  axis.title.y.right,
  axis.text,
  axis.text.x,
  axis.text.x.top,
  axis.text.x.bottom,
  axis.text.y,
  axis.text.y.left,
  axis.text.y.right,
  axis.text.theta,
  axis.text.r,
  axis.ticks,
  axis.ticks.x,
  axis.ticks.x.top,
  axis.ticks.x.bottom,
  axis.ticks.y,
  axis.ticks.y.left,
  axis.ticks.y.right,
  axis.ticks.theta,
  axis.ticks.r,
  axis.minor.ticks.x.top,
  axis.minor.ticks.x.bottom,
  axis.minor.ticks.y.left,
  axis.minor.ticks.y.right,
  axis.minor.ticks.theta,
  axis.minor.ticks.r,
  axis.ticks.length,
  axis.ticks.length.x,
  axis.ticks.length.x.top,
  axis.ticks.length.x.bottom,
  axis.ticks.length.y,
  axis.ticks.length.y.left,
  axis.ticks.length.y.right,
  axis.ticks.length.theta,
  axis.ticks.length.r,
  axis.minor.ticks.length,
  axis.minor.ticks.length.x,
  axis.minor.ticks.length.x.top,
  axis.minor.ticks.length.x.bottom,
  axis.minor.ticks.length.y,
  axis.minor.ticks.length.y.left,
  axis.minor.ticks.length.y.right,
  axis.minor.ticks.length.theta,
  axis.minor.ticks.length.r,
  axis.line,
  axis.line.x,
  axis.line.x.top,
  axis.line.x.bottom,
  axis.line.y,
  axis.line.y.left,
  axis.line.y.right,
  axis.line.theta,
  axis.line.r,
  legend.background,
  legend.margin,
  legend.spacing,
  legend.spacing.x,
  legend.spacing.y,
  legend.key,
  legend.key.size,
  legend.key.height,
  legend.key.width,
  legend.key.spacing,
  legend.key.spacing.x,
  legend.key.spacing.y,
  legend.frame,
  legend.ticks,
  legend.ticks.length,
  legend.axis.line,
  legend.text,
  legend.text.position,
  legend.title,
  legend.title.position,
  legend.position,
  legend.position.inside,
  legend.direction,
  legend.byrow,
  legend.justification,
  legend.justification.top,
  legend.justification.bottom,
  legend.justification.left,
  legend.justification.right,
  legend.justification.inside,
  legend.location,
  legend.box,
  legend.box.just,
  legend.box.margin,
  legend.box.background,
  legend.box.spacing,
  panel.background,
  panel.border,
  panel.spacing,
  panel.spacing.x,
  panel.spacing.y,
  panel.grid,
  panel.grid.major,
  panel.grid.minor,
  panel.grid.major.x,
  panel.grid.major.y,
  panel.grid.minor.x,
  panel.grid.minor.y,
  panel.ontop,
  plot.background,
  plot.title,
  plot.title.position,
  plot.subtitle,
  plot.caption,
  plot.caption.position,
  plot.tag,
  plot.tag.position,
  plot.tag.location,
  plot.margin,
  strip.background,
  strip.background.x,
  strip.background.y,
  strip.clip,
  strip.placement,
  strip.text,
  strip.text.x,
  strip.text.x.bottom,
  strip.text.x.top,
  strip.text.y,
  strip.text.y.left,
  strip.text.y.right,
  strip.switch.pad.grid,
  strip.switch.pad.wrap,
  complete = FALSE,
  validate = TRUE
)

Arguments

...

additional element specifications not part of base ggplot2. In general, these should also be defined in the element tree argument. Splicing a list is also supported.

line

all line elements (element_line())

rect

all rectangular elements (element_rect())

text

all text elements (element_text())

title

all title elements: plot, axes, legends (element_text(); inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

1)

aspect.ratio

aspect ratio of the panel

axis.title, axis.title.x, axis.title.y, axis.title.x.top, axis.title.x.bottom, axis.title.y.left, axis.title.y.right

labels of axes (element_text()). Specify all axes' labels (

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

3), labels by plane (using

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

4 or

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

5), or individually for each axis (using

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

6,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

7,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

8,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

9). `theme()`0 inherits from`theme()`1 which inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

3, which in turn inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

1

axis.text, axis.text.x, axis.text.y, axis.text.x.top, axis.text.x.bottom, axis.text.y.left, axis.text.y.right, axis.text.theta, axis.text.r

tick labels along axes (element_text()). Specify all axis tick labels (theme()`5), tick labels by plane (using `theme()`6 or `theme()`7), or individually for each axis (using `theme()`8, `theme()`9,theme_update()0, theme_update()1). theme_update()2 inherits fromtheme_update()`3 which inherits from `theme()`5, which in turn inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

1

axis.ticks, axis.ticks.x, axis.ticks.x.top, axis.ticks.x.bottom, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right, axis.ticks.theta, axis.ticks.r

tick marks along axes (element_line()). Specify all tick marks (theme_update()`7), ticks by plane (using theme_update()8 or theme_update()9), or individually for each axis (using theme_bw()0, theme_bw()1,theme_bw()2, theme_bw()3). theme_bw()4 inherits fromtheme_bw()5 which inherits from theme_update()7, which in turn inherits from theme_bw()`7

axis.minor.ticks.x.top, axis.minor.ticks.x.bottom, axis.minor.ticks.y.left, axis.minor.ticks.y.right, axis.minor.ticks.theta, axis.minor.ticks.r,

minor tick marks along axes (element_line()). theme_bw()`9 inherit from the corresponding major ticks theme_bw()`4.

axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right, axis.ticks.length.theta, axis.ticks.length.r

length of tick marks (`theme_minimal()`1)

axis.minor.ticks.length, axis.minor.ticks.length.x, axis.minor.ticks.length.x.top, axis.minor.ticks.length.x.bottom, axis.minor.ticks.length.y, axis.minor.ticks.length.y.left, axis.minor.ticks.length.y.right, axis.minor.ticks.length.theta, axis.minor.ticks.length.r

length of minor tick marks (theme_minimal()`1), or relative to theme_minimal()3 when provided with theme_minimal()`4.

axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right, axis.line.theta, axis.line.r

lines along axes (element_line()). Specify lines along all axes (theme_minimal()`6), lines for each plane (using theme_minimal()7 or theme_minimal()8), or individually for each axis (using theme_minimal()9, `element tree`0,`element tree`1, `element tree`2). `element tree`3 inherits from`element tree`4 which inherits from theme_minimal()6, which in turn inherits from theme_bw()`7

legend.background

background of legend (element_rect(); inherits from `element tree`8)

legend.margin

the margin around each legend (`element tree`9)

legend.spacing, legend.spacing.x, legend.spacing.y

the spacing between legends (theme_minimal()`1). element_line()1 & element_line()2 inherit from element_line()`3 or can be specified separately

legend.key

background underneath legend keys (element_rect(); inherits from `element tree`8)

legend.key.size, legend.key.height, legend.key.width

size of legend keys (theme_minimal()`1); key background height & width inherit fromelement_line()`7 or can be specified separately

legend.key.spacing, legend.key.spacing.x, legend.key.spacing.y

spacing between legend keys given as a theme_minimal()`1. Spacing in the horizontal (x) and vertical (y) direction inherit from element_line()`9 or can be specified separately.

legend.frame

frame drawn around the bar (element_rect()).

legend.ticks

tick marks shown along bars or axes (element_line())

legend.ticks.length

length of tick marks in legend (`theme_minimal()`1)

legend.axis.line

lines along axes in legends (element_line())

legend.text

legend item labels (element_text(); inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

1)

legend.text.position

placement of legend text relative to legend keys or bars ("top", "right", "bottom" or "left"). The legend text placement might be incompatible with the legend's direction for some guides.

legend.title

title of legend (element_text(); inherits from`element_rect()`7)

legend.title.position

placement of legend title relative to the main legend ("top", "right", "bottom" or "left").

legend.position

the default position of legends ("none", "left", "right", "bottom", "top", "inside")

legend.position.inside

A numeric vector of length two setting the placement of legends that have the `element_rect()`8 position.

legend.direction

layout of items in legends ("horizontal" or "vertical")

legend.byrow

whether the legend-matrix is filled by columns (element_rect()`9, the default) or by rows (element_text()`0).

legend.justification

anchor point for positioning legend inside plot ("center" or two-element numeric vector) or the justification according to the plot area when positioned outside the plot

legend.justification.top, legend.justification.bottom, legend.justification.left, legend.justification.right, legend.justification.inside

Same as element_text()`1 but specified per element_text()`2 option.

legend.location

Relative placement of legends outside the plot as a string. Can be element_text()`3 (default) to align legends to the panels orelement_text()`4 to align legends to the plot as a whole.

legend.box

arrangement of multiple legends ("horizontal" or "vertical")

legend.box.just

justification of each legend within the overall bounding box, when there are multiple legends ("top", "bottom", "left", or "right")

legend.box.margin

margins around the full legend area, as specified using `element tree`9

legend.box.background

background of legend area (element_rect(); inherits from `element tree`8)

legend.box.spacing

The spacing between the plotting area and the legend box (`theme_minimal()`1)

panel.background

background of plotting area, drawn underneath plot (element_rect(); inherits from `element tree`8)

panel.border

border around plotting area, drawn on top of plot so that it covers tick marks and grid lines. This should be used with

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

01 (element_rect(); inherits from `element tree`8)

panel.spacing, panel.spacing.x, panel.spacing.y

spacing between facet panels (`theme_minimal()`1).

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

05 &

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

06 inherit from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

07 or can be specified separately.

panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y

grid lines (element_line()). Specify major grid lines, or minor grid lines separately (using

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

09 or

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

  1. or individually for each axis (using

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

11,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

12,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

13,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

14). Y axis grid lines are horizontal and x axis grid lines are vertical.

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

15 inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

16 which inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

17, which in turn inherits from `theme_bw()`7

panel.ontop

option to place the panel (background, gridlines) over the data layers (

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

19). Usually used with a transparent or blank

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

20.

plot.background

background of the entire plot (element_rect(); inherits from `element tree`8)

plot.title

plot title (text appearance) (element_text(); inherits from `element_rect()`7) left-aligned by default

plot.title.position, plot.caption.position

Alignment of the plot title/subtitle and caption. The setting for

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

25 applies to both the title and the subtitle. A value of "panel" (the default) means that titles and/or caption are aligned to the plot panels. A value of "plot" means that titles and/or caption are aligned to the entire plot (minus any space for margins and plot tag).

plot.subtitle

plot subtitle (text appearance) (element_text(); inherits from `element_rect()`7) left-aligned by default

plot.caption

caption below the plot (text appearance) (element_text(); inherits from `element_rect()`7) right-aligned by default

plot.tag

upper-left label to identify a plot (text appearance) (element_text(); inherits from `element_rect()`7) left-aligned by default

plot.tag.position

The position of the tag as a string ("topleft", "top", "topright", "left", "right", "bottomleft", "bottom", "bottomright") or a coordinate. If a coordinate, can be a numeric vector of length 2 to set the x,y-coordinate relative to the whole plot. The coordinate option is unavailable for

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

32.

plot.tag.location

The placement of the tag as a string, one of`element_text()3, element_text()`4 or

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

35. Respectively, these will place the tag inside the panel space, anywhere in the plot as a whole, or in the margin around the panel space.

plot.margin

margin around entire plot (`theme_minimal()`1 with the sizes of the top, right, bottom, and left margins)

strip.background, strip.background.x, strip.background.y

background of facet labels (element_rect(); inherits from `element tree`8). Horizontal facet background (

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

  1. & vertical facet background (

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

  1. inherit from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

41 or can be specified separately

strip.clip

should strip background edges and strip labels be clipped to the extend of the strip background? Options are

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

42 to clip,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

43 to disable clipping or

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

44 (default) to take the clipping setting from the parent viewport.

strip.placement

placement of strip with respect to axes, either "inside" or "outside". Only important when axes and strips are on the same side of the plot.

strip.text, strip.text.x, strip.text.y, strip.text.x.top, strip.text.x.bottom, strip.text.y.left, strip.text.y.right

facet labels (element_text(); inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

1). Horizontal facet labels (

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

  1. & vertical facet labels (

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

  1. inherit from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

49 or can be specified separately. Facet strips have dedicated position-dependent theme elements (

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

50,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

51,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

52,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

  1. that inherit from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

47 and

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

48, respectively. As a consequence, some theme stylings need to be applied to the position-dependent elements rather than to the parent elements

strip.switch.pad.grid

space between strips and axes when strips are switched (`theme_minimal()`1)

strip.switch.pad.wrap

space between strips and axes when strips are switched (`theme_minimal()`1)

complete

set this to `element_text()`0 if this is a complete theme, such as the one returned by

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

59. Complete themes behave differently when added to a ggplot object. Also, when setting

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

60 all elements will be set to inherit from blank elements.

validate

`element_text()`0 to run

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

62, `element_rect()`9 to bypass checks.

Theme inheritance

Theme elements inherit properties from other theme elements hierarchically. For example,

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

6 inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

4 which inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

3, which in turn inherits from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

1. All text elements inherit directly or indirectly from

p1 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(title = "Fuel economy declines as weight increases")
p1


# Plot ---------------------------------------------------------------------
p1 + theme(plot.title = element_text(size = rel(2)))

p1 + theme(plot.background = element_rect(fill = "green"))


# Panels --------------------------------------------------------------------

p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))

p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))

p1 + theme(panel.grid.major = element_line(colour = "black"))

p1 + theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank()
)


# Put gridlines on top of data
p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)


# Axes ----------------------------------------------------------------------
# Change styles of axes texts and lines
p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))

p1 + theme(axis.text = element_text(colour = "blue"))

p1 + theme(axis.ticks = element_line(linewidth = 2))


# Change the appearance of the y-axis title
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))


# Make ticks point outwards on y-axis and inwards on x-axis
p1 + theme(
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)


# \donttest{
# Legend --------------------------------------------------------------------
p2 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
  labs(
    x = "Weight (1000 lbs)",
    y = "Fuel economy (mpg)",
    colour = "Cylinders",
    shape = "Transmission"
   )
p2


# Position
p2 + theme(legend.position = "none")

p2 + theme(legend.justification = "top")

p2 + theme(legend.position = "bottom")


# Or place legends inside the plot using relative coordinates between 0 and 1
# legend.justification sets the corner that the position refers to
p2 + theme(
  legend.position = "inside",
  legend.position.inside = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)


# The legend.box properties work similarly for the space around
# all the legends
p2 + theme(
  legend.box.background = element_rect(),
  legend.box.margin = margin(6, 6, 6, 6)
)


# You can also control the display of the keys
# and the justification related to the plot area can be set
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))

p2 + theme(legend.text = element_text(size = 8, colour = "red"))

p2 + theme(legend.title = element_text(face = "bold"))


# Strips --------------------------------------------------------------------

p3 <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl)
p3


p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))

p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))

# More direct strip.text.x here for top
# as in the facet_wrap the default strip.position is "top"
p3 + theme(strip.text.x.top = element_text(colour = "white", face = "bold"))

p3 + theme(panel.spacing = unit(1, "lines"))

# }

1; all lines inherit from`theme_bw()`7, and all rectangular objects inherit from `element tree`8. This means that you can modify the appearance of multiple elements by setting a single high-level component.

How do I move text to the top in CSS?

To move text up in CSS, you can use the margin-top or padding-top properties to create space between the top of the element containing the text and the top of its parent element.nullHow to move text up in CSS - Quorawww.quora.com › How-do-I-move-text-up-in-CSSnull

How do I align text to the top of a table cell in HTML?

To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.null(Legacy) HTML: Tables: Alignment Within a Table - Knowledge Basekb.uwec.edu › articles › html-tables-alignment-within-a-tablenull

How do I align text to the top in HTML?

The HTML

valign Attribute is used to specify the vertical alignment of text content in a cell. It accepts several values such as “top,” “middle,” “bottom,” and “baseline” to specify the vertical alignment. If not explicitly set, the content in a table cell is vertically aligned to the middle by default.nullHTML

valign Attribute - GeeksforGeekswww.geeksforgeeks.org › html-td-valign-attributenull

How do I align text to the top right in CSS?

For example, you could position text in the top right corner using position: absolute; top: 0px; right: 0px; .nullPosition Text in Top-Right Corner with CSS - SheCodeswww.shecodes.io › athena › 4345-position-text-in-top-right-corner-with-cssnull