


Pivot_longer(Q1:Q6, names_to = "question", values_to = "response") You can convert this into a longer data frame where the question number is stored in one column and the response is stored in a separate column: longer_data % In this example, I’m going to look at some mocked-up survey data, with six questions stored in variables Q1 through Q6. The best structure for your data depends on what you’re trying to do with it, and in this situation, even if your data is in the right form for analysis, it may not be right for some of the plots you want to make.įortunately, restructuring your data into the right form is straightforward using the tidyr package and the pivot_longer() function. Likewise, if you want to split a plot into panels (or facets, in ggplot2 -speak), you must plot a single response variable, with a grouping variable to indicate which panel the data should be plotted in.

As a bonus, it will probably be easier to analyse your data in that form too. The usual answer in this scenario is that you should restructure your data before plotting it. For example, in situations where you want to plot two columns on a graph as points with different colours, the two columns often really represent the same variable, and there is a hidden grouping factor which distinguishes the data points you want to colour differently. Ggplot2 doesn’t provide an easy facility to plot multiple variables at once because this is usually a sign that your data is not “tidy”. Pivoting longer: turning your variables into rows
