我正在使用ggplot2绘制条形图,并使用gghighlight突出显示特定的条形图。
但是使用gghighlight也会在输出中输出一些label_key。
我要删除打印在顶部图上的label_key。
请帮助。
ggplot(data=plot, aes(x=subdomain_name, y=mean)) +
geom_bar(stat="identity", color="blue", fill="blue",width = nrow(plot)/10)+
geom_text(aes(label=format(round(mean,2))),hjust=0)+
coord_flip() + theme(axis.line = element_blank(),axis.line.x = element_blank(),
axis.line.y = element_blank(),plot.margin=unit(c(-0.6,1,1,1),"cm"),
panel.background=element_blank(),panel.border=element_blank(),
axis.title.x = element_blank(),axis.title.y = element_blank() )
+ylim(0,max+(0.05*max)) + gghighlight(grepl('Domain',subdomain_name),
unhighlighted_colour = alpha("red",1),
label_key = NULL))
答案 0 :(得分:1)
在这种情况下,您需要use_direct_label = FALSE
。
library(ggplot2)
library(gghighlight)
plot <- data.frame(
subdomain_name = c(paste("Domain ", letters[1:3]), "foo"),
mean = 1:4
)
max <- 4
ggplot(data = plot, aes(x = subdomain_name, y = mean)) +
geom_bar(stat = "identity", color = "blue", fill = "blue", width = nrow(plot) / 10) +
geom_text(aes(label = format(round(mean, 2))), hjust = 0) +
coord_flip() +
theme(
axis.line = element_blank(), axis.line.x = element_blank(),
axis.line.y = element_blank(), plot.margin = unit(c(-0.6, 1, 1, 1), "cm"),
panel.background = element_blank(), panel.border = element_blank(),
axis.title.x = element_blank(), axis.title.y = element_blank()
) +
ylim(0, max + (0.05 * max)) +
gghighlight(grepl("Domain", subdomain_name),
unhighlighted_colour = alpha("red", 1),
use_direct_label = FALSE
)
由reprex package(v0.2.1)于2018-12-23创建
答案 1 :(得分:0)
您应像这样将label_key设置为F:您也可以尝试使用?guides
library(gghighlight)
ggplot(data=plot, aes(x=subdomain_name, y=mean)) +
geom_bar(stat="identity", color="blue", fill="blue",width = nrow(plot)/10)+
geom_text(aes(label=format(round(mean,2))),hjust=0)+
coord_flip() + theme(axis.line = element_blank(),axis.line.x = element_blank(),
axis.line.y = element_blank(),plot.margin=unit(c(-0.6,1,1,1),"cm"),
panel.background=element_blank(),panel.border=element_blank(),
axis.title.x = element_blank(),axis.title.y = element_blank() )
+ylim(0,max+(0.05*max)) + gghighlight(grepl('Domain',subdomain_name),
unhighlighted_colour = alpha("red",1),
label_key = F))