how to factor something that is pivoted

Hi there,

I am trying to work in a factor line for one of the new pivoted variables. The code is below and the line I want to add is at the very bottom.

CRUISE_EN_651_Sheet1_4$depth_order <- factor(CRUISE_EN_651_Sheet1_4$depth_interval,level= c('5-6', '6-7','7-8', '10-12', '12-14', '14-16', '18-20', '26-27', '29-30', '30-31', '31-32', '32-33', '42-43'))
CRUISE_EN_651_Sheet1_4 %>%
pivot_longer(c(clay, silt, very_fine_sand, fine_sand, medium_sand, coarse_sand, very_coarse_sand, total_sand), names_to = "all_sed_type", values_to = "all_percent_in_sample" )
ggplot(mapping = aes(x = depth_order, y = all_percent_in_sample, fill = all_sed_type))+
geom_bar(stat = "identity")+
theme(axis.text.x=element_text(angle = -90, hjust = 0))+
facet_wrap(~site)+
ggtitle('Subsample Depth vs. Sediment Size Class')+
labs(x = 'Subsample Depth Interval (cm)', y = '% in Sample')
CRUISE_EN_651_Sheet1_4$all_sed_type <- factor(CRUISE_EN_651_Sheet1_4$all_sed_type,level= c('clay', 'silt','very_fine_sand', 'fine-sand', 'medium_sand', 'coarse_sand', 'very_coarse_sand', 'total_sand'))

Im trying to include this in but everywhere I try it does not work

 CRUISE_EN_651_Sheet1_4$all_sed_type <- factor(CRUISE_EN_651_Sheet1_4$all_sed_type,level= c('clay', 'silt','very_fine_sand', 'fine-sand', 'medium_sand', 'coarse_sand', 'very_coarse_sand', 'total_sand'))

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hiya, you're not clear on what "does not work", but just a pointer: in the function factor(), the argument to define the levels is levels =, not level = as in your code.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.