如何突出显示ggplot2中的列

Joh*_*Ton 4 r ggplot2

我有下图,我想突出显示两个西瓜的列,因为它的juice_content和重量最高。我知道如何更改列的颜色,但我想突出显示整个列。关于如何实现这一点的任何想法?网上似乎没有任何类似的内容。

fruits <- c("apple","orange","watermelons")
juice_content <- c(10,1,1000)
weight <- c(5,2,2000)
df <- data.frame(fruits,juice_content,weight)
df <- gather(df,compare,measure,juice_content:weight, factor_key=TRUE)
plot <- ggplot(df, aes(fruits,measure, fill=compare)) + geom_bar(stat="identity", position=position_dodge()) + scale_y_log10()
Run Code Online (Sandbox Code Playgroud)

我要强调整个西瓜专栏。

Mau*_*ers 6

一种选择是使用 gghighlight

library(gghighlight)
ggplot(df, aes(fruits,measure, fill = compare)) +
    geom_col(position = position_dodge()) +
    scale_y_log10() +
    gghighlight(fruits == "watermelons")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


根据您的评论,如何使用不同的alpha价值观

ggplot(df, aes(fruits,measure)) +
    geom_col(data = . %>% filter(fruits == "watermelons"),
        mapping = aes(fill = compare),
        position = position_dodge()) +
    geom_col(data = . %>% filter(fruits != "watermelons"),
        mapping = aes(fill = compare),
        alpha = 0.2,
        position = position_dodge()) +
    scale_y_log10()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


或者,您也可以使用一个geom_col和一个条件来实现相同的效果alpha(感谢@Tjebo)

ggplot(df, aes(fruits, measure)) +
    geom_col(
        mapping = aes(fill = compare, alpha = fruits == 'watermelons'),
        position = position_dodge()) +
    scale_alpha_manual(values = c(0.2, 1)) +
    scale_y_log10()
Run Code Online (Sandbox Code Playgroud)

  • @JohnnyTon您可以* upvote *(使用箭头符号)任意数量的答案,但只能*接受*(设置绿色的选中标记)一个答案。您最终接受的答案是您的电话,通常指最能解决您的原始问题的答案。不用担心选择一个答案,没有什么难过的感觉:-) Stack Overflow是关于建立一个包含各种好的答案的综合数据库。特别是涉及到R时,通常有多个解决方案。 (2认同)