从颜色中删除线条并填充图例
Remove lines from color and fill legends
我的情节包含三种不同的图例:一种是 linetype
,一种是 color
,另一种是 fill
。在 color
和 fill
传说中也有一些我想删除的行,但如何删除?
这是一些示例代码:
# some data
hline_df <- data.frame(name = c('a', 'b'), y = c(1, 2))
df <- data.frame(x = c(1, 2), y = c(0.5, 1.5), con = c('a', 'b'), col = c('d', 'e'))
# the plot
ggplot(df, aes(x, y, fill = con)) +
geom_bar(stat = 'identity') +
geom_point(aes(color = col)) +
geom_hline(data = hline_df, aes(yintercept = y, linetype = name),
color = 'red', show_guide = TRUE)
我得到了两条红线的 "name" 指南,很好。
"col" 指南中有红线穿过点,我想删除它们!
"con" 指南也有应该删除的红线。
我可以用
修改部分图例
guides(fill = guide_legend(override.aes = list(colour = NULL)),
color = guide_legend(override.aes = list(colour = NULL)))
这消除了颜色,但线条仍然存在。
提前致谢!
使用:
ggplot(df, aes(x,y,fill=con)) + geom_bar(stat='identity') +
geom_point(aes(color=col)) +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name),color='red',show_guide=FALSE) +
guides(linetype=FALSE,color=FALSE)
给我这个情节:
根据user20650的建议
ggplot(df, aes(x,y)) +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) +
geom_point(aes(color=col), size=5) +
geom_bar(aes(fill=con), stat='identity') +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F) +
guides(color = guide_legend(override.aes = list(linetype = 0)))
所以第一个 geom_hline 创建了图例,但线在栏后面...
第二次调用将线放在条形图前面,但不打印图例(好主意)。
las guide 正在用 0 覆盖美学线条类型...这样它就从图例中删除了线条...我尝试使用 NULL
但这之前没有用...
再次感谢。
您可以为 fill
和 color
[=16] 设置 linetype = 0
或 "blank"
(在不同的 linetype
上 here) =]在你的 override.aes
通话中。
另请注意,我将 fill
aes
从 ggplot
中的 'top level' 移动到了 geom_bar
。
ggplot(df, aes(x, y)) +
geom_bar(aes(fill = con), stat = 'identity') +
geom_point(aes(color = col)) +
geom_hline(data = hline_df, aes(yintercept = y, linetype = name), color = 'red', show_guide = TRUE) +
guides(fill = guide_legend(override.aes = list(linetype = 0)),
color = guide_legend(override.aes = list(linetype = 0)))
我的情节包含三种不同的图例:一种是 linetype
,一种是 color
,另一种是 fill
。在 color
和 fill
传说中也有一些我想删除的行,但如何删除?
这是一些示例代码:
# some data
hline_df <- data.frame(name = c('a', 'b'), y = c(1, 2))
df <- data.frame(x = c(1, 2), y = c(0.5, 1.5), con = c('a', 'b'), col = c('d', 'e'))
# the plot
ggplot(df, aes(x, y, fill = con)) +
geom_bar(stat = 'identity') +
geom_point(aes(color = col)) +
geom_hline(data = hline_df, aes(yintercept = y, linetype = name),
color = 'red', show_guide = TRUE)
我得到了两条红线的 "name" 指南,很好。
"col" 指南中有红线穿过点,我想删除它们!
"con" 指南也有应该删除的红线。
我可以用
修改部分图例guides(fill = guide_legend(override.aes = list(colour = NULL)),
color = guide_legend(override.aes = list(colour = NULL)))
这消除了颜色,但线条仍然存在。
提前致谢!
使用:
ggplot(df, aes(x,y,fill=con)) + geom_bar(stat='identity') +
geom_point(aes(color=col)) +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name),color='red',show_guide=FALSE) +
guides(linetype=FALSE,color=FALSE)
给我这个情节:
根据user20650的建议
ggplot(df, aes(x,y)) +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) +
geom_point(aes(color=col), size=5) +
geom_bar(aes(fill=con), stat='identity') +
geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F) +
guides(color = guide_legend(override.aes = list(linetype = 0)))
所以第一个 geom_hline 创建了图例,但线在栏后面...
第二次调用将线放在条形图前面,但不打印图例(好主意)。
las guide 正在用 0 覆盖美学线条类型...这样它就从图例中删除了线条...我尝试使用 NULL
但这之前没有用...
再次感谢。
您可以为 fill
和 color
[=16] 设置 linetype = 0
或 "blank"
(在不同的 linetype
上 here) =]在你的 override.aes
通话中。
另请注意,我将 fill
aes
从 ggplot
中的 'top level' 移动到了 geom_bar
。
ggplot(df, aes(x, y)) +
geom_bar(aes(fill = con), stat = 'identity') +
geom_point(aes(color = col)) +
geom_hline(data = hline_df, aes(yintercept = y, linetype = name), color = 'red', show_guide = TRUE) +
guides(fill = guide_legend(override.aes = list(linetype = 0)),
color = guide_legend(override.aes = list(linetype = 0)))