How to include all levels in a ggplot legend when using d_plyr

北战南征 提交于 2020-01-05 03:58:04

问题


goal: a legend which contains two levels of a factor, even if both levels are not represented on the figure

minimum reproducible example:

library(ggplot2)
library(plyr)

mre <- data.frame(plotfactor = factor(rep(c("response1", "response2"), c(2, 
    2))), linefactor = factor(rep(c("line1", "line2"), 2)), x1 = runif(n = 4), 
    x2 = runif(n = 4), y1 = runif(n = 4), y2 = runif(n = 4), ltype = c("foo", 
        "foo", "foo", "bar"))

## this looks great!
ggplot(mre, aes(x = x1, xend = x2, y = y1, yend = y2, colour = linefactor, 
linetype =  ltype)) + geom_segment() + facet_wrap(~plotfactor)

the problem

However, If I use a plyr function to print each plot to a separate page of a pdf, the legend for ltype only contains the single factor level that appears in the subset of the dataframe that d_ply passes to ggplot -- i.e., it only says "foo". I want it to say both "foo" and "bar", even if the level "bar" of the factor ltype does not occur in the graph:

pdf("test.pdf")
d_ply(mre, .(plotfactor), function(DF)
    g <- ggplot(DF, aes(x = x1, xend = x2, y = y1, yend = y2, colour = linefactor,
    linetype = ltype)) + geom_segment()
    print(g)
    })
dev.off()

(forgive me: I have no idea how to produce this effect in a png)


回答1:


You'll need to use scale_linetype_discrete to make sure both are on the legend.

mre$ltype <- factor(mre$ltype)


plot1 <- ggplot(subset(mre, plotfactor == "response1"), 
    aes(x = x1, xend = x2, y = y1, yend = y2, colour = linefactor, 
    linetype =  ltype)) + 
    geom_segment()  +
    scale_linetype_discrete(drop = FALSE)
    ggsave(plot1, file = "plot1.pdf")

plot2 <- ggplot(subset(mre, plotfactor == "response2"), 
    aes(x = x1, xend = x2, y = y1, yend = y2, colour = linefactor, 
    linetype =  ltype)) + 
    geom_segment()  +
    scale_linetype_discrete(drop = FALSE)
    ggsave(plot2, file = "plot2.pdf")


来源:https://stackoverflow.com/questions/20157385/how-to-include-all-levels-in-a-ggplot-legend-when-using-d-plyr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!