【问题标题】:How to highlight a column in ggplot2如何突出显示ggplot2中的列
【发布时间】:2019-11-14 21:11:26
【问题描述】:

我有下图,我想突出显示西瓜的列(两者),因为它具有最高的果汁含量和重量。我知道如何更改列的颜色,但我想突出显示整个列。关于如何实现这一目标的任何想法?网上好像没有类似的。

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()

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一个选项是使用gghighlight

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


    针对您的评论,如何使用不同的 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()
    


    或者你也可以用一个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()
    

    【讨论】:

    • 好主意!但是苹果和橙子的果汁内容和重量现在不清楚......我有两个想法:1.是否可以将苹果和橙子的列更改为灰色和黑色,所以我能够区分果汁内容/重量。 2.(首选)突出显示西瓜的整个列,包括背景网格。
    • @JohnnyTon 我明白了。我进行了编辑以显示另一个选项。
    • 您还可以保存数据帧的分隔并通过aes 中的逻辑语句对geom_col 进行双重调用:ggplot(df, aes(fruits,measure)) + geom_col(data = df, mapping = aes(fill = compare, alpha = fruits == 'watermelons')) + scale_alpha_manual(values = c(0.2,1))
    • @JohnnyTon 您可以投票(使用箭头符号)任意数量的答案,但您只能接受(设置绿色复选标记)一个答案。您最终接受的答案是您的电话,通常是指最能解决您最初问题的答案。不要担心选择一个答案而不是另一个答案,没有什么不好的感觉:-) Stack Overflow 是关于建立一个包含良好和多样化答案的综合数据库。尤其是涉及到 R 时,通常有不止一种解决方案。
    【解决方案2】:

    您可以使用geom_area 突出显示栏后面。您必须首先强制 x 比例离散,这就是我使用 geom_blank 的原因(请参阅此答案 geom_ribbon overlay when x-axis is discrete)注意到 geom_ribbongeom_area 实际上是相同的,除了 geom_area 总是有 0 作为 ymin

    #minor edit so that the level isn't hard coded
     watermelon_level <- which(levels(df$fruits) == "watermelons")
    
    AreaDF <- data.frame(fruits = c(watermelon_level-0.5,watermelon_level+0.5))
    
    
    plot <- ggplot(df, aes(fruits)) + 
      geom_blank(aes(y=measure, fill=compare))+
      geom_area(data = AreaDF, aes( y = max(df$measure)), fill= "yellow")+
      geom_bar(aes(y=measure, fill=compare),stat="identity", position=position_dodge()) + scale_y_log10()
    

    编辑地址评论

    如果您想突出显示多个水果,则可以执行以下操作。您需要一个 data.frame,其中包含您想要 geom_area x 和 y 的位置,包括将其降至 0 之间。我确信有一些更简洁的方法来获取 data.frame 但这一个有效

    highlight_level <- which(levels(df$fruits) %in% c("apple", "watermelons"))
    
    AreaDF <- data.frame(fruits = unlist(lapply(highlight_level, function(x) c(x -0.51,x -0.5,x+0.5,x+0.51))),
                         yval = rep(c(1,max(df$measure),max(df$measure),1), length(highlight_level)))
                           
    
    
       
    AreaDF <- AreaDF %>% mutate(
      yval = ifelse(floor(fruits) %in% highlight_level & ceiling(fruits) %in% highlight_level, max(df$measure), yval)) %>%
      arrange(fruits) %>% distinct()
                           
    
    plot <- ggplot(df, aes(fruits)) + 
      geom_blank(aes(y=measure, fill=compare))+
      geom_area(data = AreaDF, aes(y = yval ), fill= "yellow")+
      geom_bar(aes(y=measure, fill=compare),stat="identity", position=position_dodge()) + scale_y_log10()
    plot
    

    【讨论】:

    • 仅供参考 geom_col() 将等同于 geom_bar(stat = "identity")
    • @Sarah 如果我也想突出显示“apple”怎么办?
    • 嗨 @JohnnyTon 更新了一种方法,使其适用于突出显示的苹果和西瓜
    • @Sarah 嗨,非常感谢。但我实际上有更多的列需要着色(大约 30 列)。我需要更改功能吗? function(x) c(x -0.51,x -0.5,x+0.5,x+0.51)这部分。
    • 不应该没问题。您只需将要突出显示的名称添加到 highlight_level 例如highlight_level &lt;- which(levels(df$fruits) %in% c("apple", "watermelons","pears","strawberries") 或您在 df 中的任何内容,它应该可以正常工作
    猜你喜欢
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 2014-02-27
    • 1970-01-01
    相关资源
    最近更新 更多