0
votes

When I run the below code, a density plot and histogram will be created. I've added two vertical line to show mean and median. I want to display a legend ("Mean" with dotted red and "Median" with green color) at the top-right corner of the plot. You can run this code as the df is already available in R-studio.

ggplot(USArrests,aes(x=Murder)) + 
  geom_histogram(aes(y=..density..),binwidth=.5,col="black",fill="white") +
  geom_density(alpha=.2,fill="coral") +
  geom_vline(aes(xintercept=mean(Murder,na.rm=T)),color="red",linetype="dashed",size=1) +
  geom_vline(aes(xintercept=median(Murder,na.rm=T)),color="green",size=1) 

My question is shall I use theme() or something else to display legend in my plot?

2

2 Answers

1
votes

No need for extra data.frames.

library(ggplot2)

ggplot(USArrests,aes(x=Murder)) + 
  geom_histogram(aes(y=..density..),binwidth=.5,col="black",fill="white") +
  geom_density(alpha=.2,fill="coral") +
  geom_vline(aes(xintercept=mean(Murder,na.rm=TRUE), color="mean", linetype="mean"), size=1) +
  geom_vline(aes(xintercept=median(Murder,na.rm=TRUE), color="median", linetype="median"), size=1) +
  scale_color_manual(name=NULL, values=c(mean="red", median="green"), drop=FALSE) +
  scale_linetype_manual(name=NULL, values=c(mean="dashed", median="solid")) +
  theme(legend.position=c(0.9, 0.9))
0
votes

You're probably better off creating an additional data.frame of the summary statistics and then adding this to the plot instead of trying to fiddle around with manually creating each legend element. Legend position can be adjusted with theme(legend.position = c())

library("ggplot2")
library("reshape2")
library("dplyr")

# Summary data.frame
summary_df <- USArrests %>% 
              summarise(Mean = mean(Murder), Median = median(Murder)) %>% 
              melt(variable.name="statistic")

# Specifying colors and linetypes for the legend since you wanted to map both color and linetype
# to the same variable.

summary_cols <- c("Mean" = "red", "Median" = "green")
summary_linetypes <- c("Mean" = 2, "Median" = 1)


ggplot(USArrests,aes(x=Murder)) + 
      geom_histogram(aes(y=..density..),binwidth=.5,col="black",fill="white") +
  geom_density(alpha=.2,fill="coral") +
  geom_vline(data = summary_df, aes(xintercept = value, color = statistic, 
             lty = statistic)) +
  scale_color_manual(values = summary_cols) +
  scale_linetype_manual(values = summary_linetypes) +
  theme(legend.position = c(0.85,0.85))

giving

figure_with_legend