R 从ggplot2图例中删除黑框

R 从ggplot2图例中删除黑框,r,ggplot2,R,Ggplot2,在谷歌搜索了一番并四处查看之后,我还没有发现其他人有这个问题,也许我的措辞是错误的。但是,在我的ggplot 2图例中,它会在黑框周围显示与我的条形图关联的颜色,但不会填充框。看起来是这样的: 这是我的密码 post_pre_active <- data.frame("survey" = c("pre", "pre", "pre", "pre", "pre", "pre", "pre", "post"

在谷歌搜索了一番并四处查看之后,我还没有发现其他人有这个问题,也许我的措辞是错误的。但是,在我的ggplot 2图例中,它会在黑框周围显示与我的条形图关联的颜色,但不会填充框。看起来是这样的:

这是我的密码

post_pre_active <- data.frame("survey" = c("pre", "pre", "pre", "pre", "pre", 
                                           "pre", "pre", "post", "post", "post",
                                           "post", "post", "post", "post"), 
                              "percentage" = c(14, 18, 16, 13, 11, 11, 17, 01, 07, 
                                               17, 18, 20, 10, 27),
                              "minutes" = c("0", "1-30", "31-60", "61-90", "91-120", 
                                            "121-150", "150+", "0", "1-30", "31-60", 
                                            "61-90", "91-120", "121-150", "150+"))
post_pre_active$minutes <- factor(post_pre_active$minutes, 
                                  levels = c("0", "1-30", "31-60", "61-90", 
                                             "91-120", "121-150", "150+"))
post_pre_active$survey <- factor(post_pre_active$survey, 
                                 levels = c("pre", "post"))

ggplot(post_pre_active, aes(x=minutes, y=percentage))+
  geom_bar(stat="identity", position="dodge", 
           aes(fill = survey, colour = survey))+
  scale_fill_discrete(name="Survey",
                      breaks=c(1, 2),
                      labels=c("Pre", "Post"))+
  xlab("Minutes of Physical Activity")+
  ylab("Percentage") +
  theme(legend.key = element_rect(colour = post_pre_active$survey,
                                  fill = post_pre_active$survey))

post\u pre\u active在调用
ggplot()
时,似乎有一些不必要的语法,这会弄乱图例的绘制方式,因为填充和色标上的中断不匹配。您可以摆脱色彩美学,只留下填充,也可以摆脱
breaks=c(1,2)
,一切都应该正常:

ggplot(post_pre_active, aes(x=minutes, y=percentage, fill=survey))+
  geom_bar(stat="identity",position="dodge")+
  scale_fill_discrete(name="Survey",
                      labels=c("Pre", "Post"))+
  xlab("Minutes of Physical Activity")+ylab("Percentage") 

欢迎使用SO,感谢您提供了一个可复制的示例!如果您不介意,请接受答案(单击复选标记),让其他人知道这解决了您的问题!