Make the border on one bar darker than the others

前端 未结 2 571
滥情空心
滥情空心 2021-02-05 22:01

I have created a bar graph in ggplot2 where 3 bars represent the probability of making 1 of 3 choices.

I want to add a bolded border around the bar that

相关标签:
2条回答
  • 2021-02-05 22:31

    Similar to Troy's answer, but rather than creating a layer of invisible bars, you can use the size aesthetic and scale_size_manual:

    require(ggplot2)
    data(diamonds)
    
    diamonds$choose = factor(diamonds$clarity == "SI1")
    
    ggplot(diamonds) + 
      geom_bar(aes(x = clarity, fill=clarity, size=choose), color="black") +
      scale_size_manual(values=c(0.5, 1), guide = "none") +
      facet_wrap(~ cut)
    

    Which produces the following plot:

    enter image description here

    0 讨论(0)
  • 2021-02-05 22:41

    I haven't got your data so I have used the diamonds dataset to demonstrate.

    Basically you need to 'overplot' a second geom_bar() call, where you filter the data= attribute to only draw the bars you want to highlight. Just filter the original data to exclude anything you don't want. e.g below we replot the subset diamonds[(diamonds$clarity=="SI2"),]

    d <- ggplot(diamonds) +  geom_bar(aes(clarity, fill=color))    # first plot
    d + geom_bar(data=diamonds[(diamonds$clarity=="SI2"),],        # filter
    aes(clarity), alpha=0, size=1, color="black") +                # plot outline only
      facet_wrap(~ cut) 
    

    NB obviously your filter will be more complicated, e.g.

    data=yourdata[(yourdata$visualcondition=="LeftCust" & yourdata$report=="Left" |
                     yourdata$visualcondition=="SIMCust" & yourdata$report=="SIM" |
                    yourdata$visualcondition=="RightCust" & yourdata$report=="Right"),]
    

    enter image description here

    OK updated with your data. I had to make up confidence intervals because they weren't available in the AggBar2 data:

    ######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
    HighlightData<-AggBar2[AggBar2$Report==gsub("Cust","",AggBar2$Visual),]
    #####################################################
    
    prob.bar = ggplot(AggBar2, aes(x = Report, y = Prob, fill = Report)) + theme_bw() + facet_grid(Audio~Visual)
    prob.bar + geom_bar(position=position_dodge(.9), stat="identity", colour="black") + theme(legend.position = "none") + labs(x="Report", y="Probability of Report") + scale_fill_grey() +
    
    ######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
      geom_bar(data=HighlightData, position=position_dodge(.9), stat="identity", colour="pink",size=1) +
    ######################################################
    
      labs(title = expression("Visual Condition")) +
      theme(plot.title = element_text(size = rel(1)))+
      geom_errorbar(aes(ymin=Prob-ci, ymax=Prob+ci),
                    width=.2, # Width of the error bars
                    position=position_dodge(.9))+
      theme(plot.title = element_text(size = rel(1.5)))+
      scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 10)))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题