rggplot2geom-histogram

Adding multiple variables to fill the geom_hist


I am trying to make a histogram in ggplot with values of Salinity on the x-axis, but I need to categorize it with both Deposit and CL_Phase. I don't want to just change the outline color because it is difficult to see. But this code does not work.

Salinity <- df %>% ggplot(aes(x = Salinity, fill = (CL_Phase,Deposit))) +
  geom_histogram(color = "black", binwidth = 10)

This is the sample dataset

structure(list(Deposit = c("KA", "KA", "KA", "KA", "KA", "KA", 
"KA", "KA", "KA", "KA", "KA", "KA", "KA", "KA", "KA", "KA", "LS", 
"LS", "LS", "LS", "LS", "LS", "TF", "LS", "LS", "LS", "LS", "LS", 
"TF", "TF", "TF", "LS", "TF", "TF", "TF", "TF", "TF", "TF", "TF", 
"TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", 
"TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", 
"TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", 
"TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF", "TF"
), Salinity = c(1.905259367, 4.494745431, 7.864625, 7.864625, 
8.945789384, 9.855516125, 10.977317768, 12.047547359, 12.163384128, 
12.278617625, 12.845853, 13.937, 14.043035657, 14.461747125, 
17.522345621, 18.717681707, 29.7852583812417, 29.8328258147691, 
29.8368035674166, 30.0793702312925, 30.3339066496739, 30.3806645295019, 
30.6976763213867, 31.1790550169573, 31.2257750014753, 31.3435723173383, 
32.8063329077369, 33.2203487697482, 34.0674202932429, 34.4748269145405, 
34.6361603852619, 34.7049665592227, 34.8689059529115, 34.9515673252259, 
35.0603621635837, 35.0603621635837, 35.0925119138281, 35.1247305837681, 
35.1893750405706, 35.2153102736838, 35.2803423820001, 35.3194949209651, 
35.4573184402101, 35.5631542872796, 35.5697928649395, 35.6163415886054, 
35.7165533901774, 35.7366719228576, 35.8039174991621, 35.9120999790248, 
35.9392592522252, 35.9732724029392, 36.0483522801742, 36.1237779532062, 
36.17885070046, 36.1926475811928, 36.1995503293744, 36.2618044191618, 
36.3103854573673, 36.3312491422852, 36.4009824223941, 36.4429611760086, 
36.4569772567865, 36.5201931269833, 36.8972506639992, 36.9044439812344, 
36.9837645609842, 37.2678697934722, 37.6087773415271, 37.6162586135865, 
38.0400125033386, 38.3407352586274, 39.0042541315711, 39.0766355908795, 
39.189728130044, 39.706196639173, 40.4741922831436, 40.9335612547648, 
41.0827995678457, 41.1180484407973, 42.2730680102387, 44.3120939549952, 
45.299440918469), CL_Phase = c("1", "3", "3", "1", "2", "2", 
"3", "1", "3", "1", "3", "1", "2", "2", "2", "2", "1", "1", "1", 
"1", "1", "1", "3", "1", "1", "1", "1", "1", "1", "3", "3", "1", 
"1", "3", "1", "3", "1", "3", "1", "1", "1", "1", "3", "3", "1", 
"3", "1", "1", "1", "1", "3", "1", "3", "1", "1", "3", "3", "1", 
"3", "1", "1", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3")), row.names = c(NA, 
-83L), class = c("tbl_df", "tbl", "data.frame"))

Solution

  • Maybe you want to use interaction for your fill variables like this:

    library(ggplot2)
    library(dplyr)
    df %>% 
      ggplot(aes(x = Salinity, fill = interaction(CL_Phase, Deposit))) +
      geom_histogram(color = "black", binwidth = 10) +
      labs(fill = "Legend")
    

    Created on 2022-08-15 by the reprex package (v2.0.1)