change color of only one bar in ggplot

后端 未结 2 901
梦毁少年i
梦毁少年i 2020-12-14 19:00

I want to color only one bar in ggplot. This is my data frame:

area <- c(\"Północ\", \"Południe\", \"Wschód\", \"Zachód\")
sale <- c(16.5, 13.5, 14, 13         


        
2条回答
  •  心在旅途
    2020-12-14 19:09

    If you like having everything in the ggplot call, you can use an ifelse statement within factor() for the fill as shown below.

    This also separates the legend into two categories (i.e. highlighted and not highlighted) so that you aren't repeating the values shown on the x axis. This also provides another illustrative dimension to the plot in the legend.

    plot.sale.bad2 <- ggplot(data=df.sale,
                             aes(x=area,
                                 y=sale,
                                 fill=factor(ifelse(area=="Południe","Highlighted","Normal")))) +
      geom_bar(stat="identity") +
      scale_fill_manual(name = "area", values=c("red","grey50")) +
      xlab(colnames(df.sale)[1]) +
      ylab(colnames(df.sale)[2]) +
      ggtitle("Porównanie sprzedaży") 
    
    plot.sale.bad2
    

    If the legend isn't needed you can add show.legend = FALSE to the geom_bar() call to produce the following:

提交回复
热议问题