Joachim Schork
Joachim Schork

Reputation: 2147

ggplot2: Legend Overlays Panel Border

I want to create a plot via the ggplot2 package with the theme_bw and a legend at the topright of the plot. Unfortunately, the legend overlays the border of the plot. Consider the following example in R:

library("ggplot2")

# Example data
df <- data.frame(x = 1:10,
                 y = 1:10,
                 col = as.factor(c(rep(1, 5), rep(2, 5))))

# Plot
ggplot(df, aes(x, y, col = col)) + 
  geom_line() +
  theme_bw() +
  theme(legend.title = element_blank(),
        legend.position = c(1, 1),
        legend.justification = c(1, 1))

Question: How could I keep the black border around the plot?

Upvotes: 1

Views: 626

Answers (1)

Nova
Nova

Reputation: 5881

Try adding the following line to your call to theme:

    ,legend.background = element_rect(fill = NA)

Upvotes: 1

Related Questions