[英]Remove black box from ggplot2 legend
在谷歌搜索並環顧四周之后,我沒有發現其他人有這個問題,也許我的措辭有誤。 但是,在我的 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))
有任何想法嗎? 提前致謝。
看起來您在調用ggplot()
有一些不必要的語法,這會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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.