ggplot2 legend for plot combining geom_bar and geo

2020-05-07 19:07发布

I am trying to make a plot to show the returns of various securities in a portfolio in a bar plot and then superimpose points over the bars indicating exposure to those securities. However, the legend I get completely ignores the points and only draws a legend for the bars.

To produce a dataframe with similar structure:

out<-data.frame(security=c("A", "B", "C", "D", "A", "B", "C", "D"), avg_weight=c(0.1,0.2,0.3,0.4, 0.1, 0.2, 0.3, 0.4), return_type=c(rep("systematic",4), rep("idiosyncratic",4)), return=rnorm(8))

Now, for plotting

g <- ggplot(data=out, aes(x=factor(security, levels=out$security), y=return))
g <- g + geom_bar(stat="identity", position="dodge", aes(fill=return_type))
g <- g + geom_point(aes(x=factor(security, levels=out$security), y=avg_weight))
g <- g + ggtitle("Systematic and Idiosyncratic Returns")
g <- g + theme(axis.text.x=element_text(angle=70, hjust=1))
g + xlab("Security Description") + ylab("Return")

Plot results

How can I get a third entry in the legend, something like:

  • Exposure

标签: r ggplot2 tidyr
1条回答
家丑人穷心不美
2楼-- · 2020-05-07 19:41

ggplot generates legends only when you create an aesthetic mapping inside aes. This is usually done by mapping a data column to an aesthetic, like fill, shape, or color. Here we don't actually want to map avg_weight to an aesthetic, so we'll use shape as a "dummy" aesthetic, just to get the legend.

First, set a seed for data reproducibility:

# Set a seed for reproducibility
set.seed(4)
out<-data.frame(security=c("A", "B", "C", "D", "A", "B", "C", "D"), 
                avg_weight=c(0.1,0.2,0.3,0.4, 0.1, 0.2, 0.3, 0.4), 
                return_type=c(rep("systematic",4), rep("idiosyncratic",4)), return=cumsum(rnorm(8,0,0.1)))

In the code below, we add a "dummy" shape aesthetic to geom_point so that a shape legend will be generated. Then in labs we set shape=NULL so that the shape legend won't have a title.

ggplot(data=out, aes(x=security)) + 
  geom_bar(stat="identity", aes(y=return, fill=return_type, group=return_type), position="dodge") +
  geom_point(aes(y=avg_weight, shape="Exposure")) + 
  ggtitle("Systematic and Idiosyncratic Returns") +
  theme(axis.text.x=element_text(angle=70, hjust=1)) +
  labs(x="Security Description", y="Return", shape=NULL) +
  theme_classic()

enter image description here

查看更多
登录 后发表回答