Reputation: 19
I have data that pertains to chlorophyll-a biomass from two size classes of phytoplankton: 'WSW' and "<20 µm" size classes. The data is also divided by regions of the Gulf of Mexico (5 in total). Each region has both WSW and <20 µm values. I am trying to get a box plot to show the biomass data for each region. Each region needs to be a certain color but that does not allow for differentiation between the size classes so I chose to add a pattern to one of the size classes. I was able to do the pattern with some help but now there is these default colors and ggplot2 will not use my specified colors. The colors are a big deal because are several other plots that are color coded to these colors. Here is my code so far:
region_colors <- c('WFS' = 'darkgreen', 'TLS' = 'cyan3', 'EMS' = 'orangered2', 'CB' = 'yellow3', 'YC/FS' = 'plum2')
surf_boxplot= ggplot(surf_box, aes(x = region, y = Chla, fill = region, pattern = Size)) +
geom_boxplot_pattern(pattern_fill = "white", pattern_color = "white",
pattern_angle = 45, pattern_size = 0,
pattern_spacing = 0.025) +
scale_pattern_fill_manual(values = region_colors) +
scale_pattern_manual(values = patterns) +
theme(panel.background = element_blank(),
legend.position = 'top',
axis.line.x = element_line(color="black", size = 1),
axis.line.y = element_line(color="black", size = 1),
plot.title = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0),
hjust = 0.5, size = 15, face = 'bold'),
legend.key = element_blank(),
legend.title = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0),
size = 10, face = "bold")) +
labs(x=expression(bold(paste("Regions of the Gulf of Mexico"))),
y=expression(bold(paste("Chl-",bolditalic ("a"),
" biomass (µg L"^'-1',')'))),
title = expression(bold(paste("Surface Chl-",
bolditalic ("a"), ' Biomass'))))
Here is my data:
structure(list(Size = c("WSW", "WSW", "WSW", "WSW", "WSW", "WSW",
"WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW",
"WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW",
"WSW", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm",
"<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm",
"<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm",
"<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm",
"<20 µm", "<20 µm"), Chla = c(0.0158553374250702, 0.0681835535205316,
1.69758559710116, 0.38623655064116, 0.18999889055035, 0.0475280806809533,
0.0518441820414833, 1.38653044994664, 0.811092911431239, 0.0627435113039509,
0.0600206049988672, 0.76576153727216, 0.0131834301368552, 0.0139065718743198,
0.0801018539960821, 0.0476673877342761, 0.0170883088104008, 0.0353610062847066,
0.0862740442259294, 0.54829425475163, 0.354272113611421, 0.0419050024352668,
0.14044768258058, 0.0693766746908316, 0.0539223064788303, 0.0159113633523673,
0.033895686014726, 0.592754310802978, 0.251252264220314, 0.177521351469432,
0.0574533958589567, 0.0447197172789116, 1.75919476060424, 0.789171481392557,
0.104392221393643, 0.0613148498505948, 0.646043888487357, 0.0200254634990205,
0.0165766336741892, 0.0619120579844718, 0.0324340598144779, 0.0264756363476934,
0.0276985089356931, 0.105004593301296, 0.288904529677619, 0.295226761342851,
0.0419623280199936, 0.10089302911911, 0.0580098748827292, 0.0647851594974109
), region = c("WFS", "WFS", "WFS", "WFS", "WFS", "WFS", "TLS",
"TLS", "TLS", "TLS", "TLS", "TLS", "EMS", "EMS", "EMS", "EMS",
"EMS", "CB", "CB", "CB", "CB", "CB", "CB", "YC/FS", "YC/FS",
"WFS", "WFS", "WFS", "WFS", "WFS", "WFS", "TLS", "TLS", "TLS",
"TLS", "TLS", "TLS", "EMS", "EMS", "EMS", "EMS", "EMS", "CB",
"CB", "CB", "CB", "CB", "CB", "YC/FS", "YC/FS"), depth_name = c("Surface",
"Surface", "Surface", "Surface", "Surface", "Surface", "Surface",
"Surface", "Surface", "Surface", "Surface", "Surface", "Surface",
"Surface", "Surface", "Surface", "Surface", "Surface", "Surface",
"Surface", "Surface", "Surface", "Surface", "Surface", "Surface",
"Surface", "Surface", "Surface", "Surface", "Surface", "Surface",
"Surface", "Surface", "Surface", "Surface", "Surface", "Surface",
"Surface", "Surface", "Surface", "Surface", "Surface", "Surface",
"Surface", "Surface", "Surface", "Surface", "Surface", "Surface",
"Surface"), distance = c("Offshore", "Offshore", "Nearshore",
"Nearshore", "Intermediate", "Offshore", "Offshore", "Nearshore",
"Nearshore", "Intermediate", "Intermediate", "Nearshore", "Nearshore",
"Intermediate", "Offshore", "Intermediate", "Offshore", "Offshore",
"Intermediate", "Nearshore", "Nearshore", "Offshore", "Intermediate",
"Offshore", "Intermediate", "Offshore", "Offshore", "Nearshore",
"Nearshore", "Intermediate", "Offshore", "Offshore", "Nearshore",
"Nearshore", "Intermediate", "Intermediate", "Nearshore", "Nearshore",
"Intermediate", "Offshore", "Intermediate", "Offshore", "Offshore",
"Intermediate", "Nearshore", "Nearshore", "Offshore", "Intermediate",
"Offshore", "Intermediate")), row.names = c(NA, -50L), class = c("tbl_df",
"tbl", "data.frame"))
Upvotes: -1
Views: 43
Reputation: 124213
As you are mapping on the fill
aesthetic (whereas pattern_fill
is fixed to "white"
) the issue is that you use scale_pattern_fill_manual
where you should use scale_fill_manual
:
ggplot(surf_box, aes(x = region, y = Chla, fill = region, pattern = Size)) +
geom_boxplot_pattern(
pattern_fill = "white", pattern_color = "white",
pattern_angle = 45, pattern_size = 0,
pattern_spacing = 0.025
) +
scale_fill_manual(values = region_colors) +
theme(
panel.background = element_blank(),
legend.position = "top",
axis.line.x = element_line(color = "black", size = 1),
axis.line.y = element_line(color = "black", size = 1),
plot.title = element_text(
margin = margin(t = 0, r = 0, b = 0, l = 0),
hjust = 0.5, size = 15, face = "bold"
),
legend.key = element_blank(),
legend.title = element_text(
margin = margin(t = 0, r = 0, b = 0, l = 0),
size = 10, face = "bold"
)
) +
labs(
x = expression(bold(paste("Regions of the Gulf of Mexico"))),
y = expression(bold(paste(
"Chl-", bolditalic("a"),
" biomass (µg L"^"-1", ")"
))),
title = expression(bold(paste(
"Surface Chl-",
bolditalic("a"), " Biomass"
)))
)
EDIT We could fix the order of your Size
categories by converting to a factor with the order of the levels set in the desired order:
region_colors <- c("WFS" = "darkgreen", "TLS" = "cyan3", "EMS" = "orangered2", "CB" = "yellow3", "YC/FS" = "plum2")
surf_box$Size <- factor(surf_box$Size, c("WSW", "<20 µm"))
ggplot(surf_box, aes(x = region, y = Chla, fill = region, pattern = Size)) +
geom_boxplot_pattern(
pattern_fill = "white", pattern_color = "white",
pattern_angle = 45, pattern_size = 0,
pattern_spacing = 0.025
) +
scale_fill_manual(values = region_colors) +
scale_pattern_manual(values = c('WSW' = 'none', '<20 µm' = 'crosshatch')) +
theme(
panel.background = element_blank(),
legend.position = "top",
axis.line.x = element_line(color = "black", size = 1),
axis.line.y = element_line(color = "black", size = 1),
plot.title = element_text(
margin = margin(t = 0, r = 0, b = 0, l = 0),
hjust = 0.5, size = 15, face = "bold"
),
legend.key = element_blank(),
legend.title = element_text(
margin = margin(t = 0, r = 0, b = 0, l = 0),
size = 10, face = "bold"
)
) +
labs(
x = expression(bold(paste("Regions of the Gulf of Mexico"))),
y = expression(bold(paste(
"Chl-", bolditalic("a"),
" biomass (µg L"^"-1", ")"
))),
title = expression(bold(paste(
"Surface Chl-",
bolditalic("a"), " Biomass"
)))
)
Upvotes: 1