ggplot2 0.9.0自动从plot图例中删除未使用的因子级别? [英] ggplot2 0.9.0 automatically dropping unused factor levels from plot legend?

查看:128
本文介绍了ggplot2 0.9.0自动从plot图例中删除未使用的因子级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从版本0.8.9升级到ggplot2 0.9.0,现在我知道我的情节图例只显示了情节中使用的因素级别(它省略了未使用的因素级别)。在它之前将所有因素级别包含在图例中。我正在运行Windows 7和R 2.15.0(今天之前的2.14.2)。

其他人是否也发现了这一点?有没有一种方法可以让我的情节图例中显示未使用的因子水平?

  library(ggplot2)

df < - data.frame(fruit = rep(c( apple,orange),times = 11),
year = 1990:2011,
qty = rnorm(22,100,20))

#现在只在传说中给出苹果。
#之前,我曾经同时获得苹果和橙色。
qplot(year,qty,data = subset(df,fruit ==apple),color = fruit)

qplot()用于在图例中为我提供apple和orange(即使只有apple的点数)。现在我只在传说中获得苹果。



这个原因出现了 - 我制作了很多数据集的子集,我希望图中的图例标准化(通常我会非常欣赏未使用的级别)下降,而不必键入小滴(),但这是我想要的那些未使用的水平)。道歉,如果这只是我的电脑本地的问题。

解决方案

是的,您想为您的颜色添加 drop = FALSE 比例尺:

  ggplot(子集(df,水果==苹果),aes(x = year,y = qty, color = fruit))+ 
geom_point()+
scale_colour_discrete(drop = FALSE)


I recently upgraded to ggplot2 0.9.0 from version 0.8.9, and now I'm getting that my plot legends only show the factor levels used in the plot (it omits the unused ones). Before it'd include all factor levels in the legend. I'm running Windows 7 and R 2.15.0 (2.14.2 before today).

Is anyone else finding this too? Is there a way I can get the unused factor levels to display in my plot legend?

library(ggplot2)

df <- data.frame(fruit = rep(c("apple", "orange"), times=11), 
                 year = 1990:2011, 
                 qty = rnorm(22, 100, 20))

# This plot only gives "apple" in the legend now.
# Before, I used to get both "apple" and "orange". 
qplot(year, qty, data = subset(df, fruit=="apple"), colour = fruit) 

The qplot() used to give me both "apple" and "orange" in the legend (even though there were only points for "apple"). Now I only get "apple" in the legend.

Reason this came up - I am making many plots of subsets of a data set and I want the legends standardized across plots (normally I'd appreciate the unused levels being automatically dropped and not having to type droplevels(), but this is the one case I want those unused levels). Apologies if this is a question local to my computer only.

解决方案

Yes, you want to add drop = FALSE to your colour scale:

ggplot(subset(df,fruit == "apple"),aes(x = year,y = qty,colour = fruit)) + 
    geom_point() + 
    scale_colour_discrete(drop = FALSE)

这篇关于ggplot2 0.9.0自动从plot图例中删除未使用的因子级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆