Search code examples
rggplot2geom-bar

How can I create a bar plot without 'fill' in R?


I have a dataframe which contains data like below

     Monsoon                 F_Monsoon                         avg_Min_temp_year
  1 Post-Monsoon Autumn    avg_Min_temp                                 21.4
  2 Pre-Monsoon Hot Summer avg_Min_temp                                 22.5
  3 Rainy                  avg_Min_temp                                 25.6
  4 Winter                 avg_Min_temp                                 13.9
  5 Post-Monsoon Autumn    avg_Min_temp_monsoon                         20.7
  6 Pre-Monsoon Hot Summer avg_Min_temp_monsoon                         22.1
  ...
  ...

I am trying to create a plot without any color (only black and white)

My code is given below

plot <- ggplot(plot_final_df, aes(Monsoon, avg_Min_temp_year))+
 geom_bar(stat = "identity", position = "dodge")+
 theme_bw()+
 facet_wrap(~Monsoon)+
 theme(panel.grid = element_blank())+
 xlab("Monsoon")+
 ylab("Avg Min Temp")+
 ggtitle("Avg Min Temp vs Monsoon")

My plot is showing only one bar for each monsoon but it must show 2 bar (one for avg_Min_temp and for avg_Min_temp_monsoon)

When I am using fill properties inside aes(fill = F_Monsoon) it is showing 2 bar for each monsoon with color but I need black and white

Any kind of suggestion is appreciable.


Solution

  • Since you are already faceting by Monsoon, and you want each facet to contain to have two bars (one each for both levels in F_Monsoon, you should have F_Monsoon as the x axis:

    ggplot(plot_final_df, aes(F_Monsoon, avg_Min_temp_year)) +
     geom_bar(stat = "identity", position = "dodge") +
     facet_wrap(~Monsoon) +
     theme_bw() +
     theme(panel.grid = element_blank()) +
     xlab("Monsoon") +
     ylab("Avg Min Temp") +
     ggtitle("Avg Min Temp vs Monsoon")
    

    enter image description here

    Note I had to add two extra rows to your example data to get this to work:

    Data

    plot_final_df <- structure(list(Monsoon = c("Post-Monsoon Autumn", 
    "Pre-Monsoon Hot Summer", 
    "Rainy", "Winter", "Post-Monsoon Autumn", "Pre-Monsoon Hot Summer", 
    "Rainy", "Winter"), F_Monsoon = c("avg_Min_temp", "avg_Min_temp", 
    "avg_Min_temp", "avg_Min_temp", "avg_Min_temp_monsoon", "avg_Min_temp_monsoon", 
    "avg_Min_temp_monsoon", "avg_Min_temp_monsoon"), avg_Min_temp_year = c(21.4, 
    22.5, 25.6, 13.9, 20.7, 22.1, 24.6, 12.9)), class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5", "6", "7", "8"))