ggplot2图例中的重复项目

时间:2018-01-04 11:29:51

标签: r ggplot2

我在ggplot2中制作了一个带有堆叠列和两行的图。但是,这些行的图例项也会显示在列的图例中。任何人都知道如何从列图例中删除它们?

以下代码:

##Remove Objects
rm(list=ls(all=TRUE))
##Load packages
library(ggplot2)
library(dplyr)
library(reshape)
##Data
set.seed(12345)
d.fig6.1 <- data.frame(mm=c("Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"),a.1=(rnorm(12)*5)^2)
d.fig6.1$a.2 <- (rnorm(12)*5)^2
d.fig6.1$a.3 <- (rnorm(12)*5)^2
d.fig6.1$a.4 <- (rnorm(12)*5)^2
d.fig6.1$a.5 <- (rnorm(12)*5)^2
d.fig6.1$a.6 <- (rnorm(12)*5)^2
d.fig6.1$a.7 <- (rnorm(12)*5)^2
d.fig6.2 <- data.frame(mm=c("Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"),a.8=(rnorm(12)*5)^2)
d.fig6.2$a.9 <- (rnorm(12)*5)^2
d.fig6.1 <- melt(d.fig6.1,id="mm")
d.fig6.2 <- melt(d.fig6.2,id="mm")
d.fig6.1
d.fig6.2
##Plot
theme_set(theme_bw(7)) #25
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 
"#D55E00", "#CC79A7","red")
sp.6 <- ggplot(d.fig6.1, aes(x=mm, y=value, fill=variable)) + geom_col() 
+ labs(x="") + labs(y="[Units]") 
+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 
+ scale_fill_manual(values=cbPalette,name="") 
+ geom_text(data=d.fig6.1, aes(label = round(value,digits=2)), position = position_stack(vjust=0.5), size=2) 
+ theme(legend.title = element_blank()) 
+ geom_line(data=d.fig6.2, aes(x=as.numeric(mm), y=value, color=variable),size=1,inherit.aes = FALSE) 
+ geom_text(data=d.fig6.2, aes(label=round(value,digits=2)),hjust=0, vjust=0, size=2.5)
sp.6

sp.6

2 个答案:

答案 0 :(得分:0)

尝试在最后添加+ guides(color = FALSE)

答案 1 :(得分:0)

fill = variable移出顶级ggplot(aes(...))映射。只保留那里所有geoms共有的美学映射。这样你就不需要inherit.aes = FALSE

ggplot(d.fig6.1,
       aes(x = mm, y = value, label = round(value, digits = 2))) +

  geom_col(aes(fill = variable)) +
  geom_text(position = position_stack(vjust = 0.5), size = 2) +

  geom_line(data = d.fig6.2,
            aes(color = variable, group = variable),
            size = 1) +
  geom_text(data = d.fig6.2, 
            hjust = 0, vjust = 0, size = 2.5) +

  labs(x = "", y = "[Units]") +
  scale_fill_manual(values = cbPalette, name="") +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_blank())

plot

解释:通过在顶级美学映射中加入fill = variable,每个没有明确设置inherit.aes = FALSE的geom都将继承此作为审美的一部分映射其级别。

在这种情况下,geom_line&amp;第二个geom_text都使用不同的数据源(d.fig6.2而不是d.fig6.1),但geom_text仍然从顶级映射继承fill = variable。所以来自d.fig6.2(&#34; a.8&#34;&amp;&#34; a.9&#34;)的变量值被添加到填充调色板中,即使它们没有被使用任何地方。

我删除了你的代码,找到了一个更简单的例子来重现下面的问题:

# this will result in a.8 & a.9 in the column legend
# because geom_text inherits aes(fill = variable) from ggplot()
ggplot(d.fig6.1,
       aes(x = mm, y = value, fill = variable,
           label = round(value, digits = 2))) +
  geom_col() +
  geom_text(data = d.fig6.2)

# this will not
# because aes(fill = variable) is moved from ggplot() to geom_col()
ggplot(d.fig6.1,
       aes(x = mm, y = value,
           label = round(value, digits = 2))) +
  geom_col(aes(fill = variable)) +
  geom_text(data = d.fig6.2)

# this will not, either
# because geom_text() includes all the aes mappings it requires, & inherit.aes = FALSE
ggplot(d.fig6.1,
       aes(x = mm, y = value, fill = variable,
           label = round(value, digits = 2))) +
  geom_col() +
  geom_text(data = d.fig6.2, 
            aes(x = mm, y = value, 
                label = round(value, digits = 2)),
            inherit.aes = FALSE)