how to use facet_wrap

I am doing an assignment and in really need of some help. I am using the nycflights13 dataset, and need to create a comparison between some variables. I don't know how to share the dataset here so I'm gonna try my best to explain this,

say there were 5 columns of variables; origin, year, dewp, humidity and wind. How would create separate pannels using facet_wrap to show the trends in year, dewp, humidity and wind , using the year as the x axis, and the countries as the actual data inside.

sorry about the explanation but i dont really know how to show the data

facet_wrap can be used to map the different values of a variable into different facets. Since you're being asked to show a range of different variables like humidity and wind, you'll need to reshape the data to put the name of those variables into one column, and the respective values into another column. The pivot_longer function from tidyr, loaded up as part of the tidyverse, can help here.

As an example, if we just wanted to combine temp and dewp, we could use

nycflights13::weather %>%
  pivot_longer(temp:dewp)

and that would reshape those two columns so their former column name is in the "name" column and their former value is in the "value" column. You could adapt that to include the columns you need. Your new "name" column could be fed into facet_wrap to have each variable go to a different facet, and the new "value" column could be mapped to your y axis.

This is a handy approach for getting a sense for a multivariate dataset. One limitation of this approach, though, is that it won't work if the variables are of different types -- if one is a category variable, while another is a number, their respective values won't be compatible to put into the same single column. Another challenge is that the units and/or numeric ranges of the variables may be inconsistent, which could take some extra work to get the labels and breaks on your facets to make sense for each respective variable. In those cases, it may be more fruitful to make separate plots and combine them afterwards with, for instance, the patchwork package.

Thank you so much, it worked!

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.