HELP! Multiple variables on one axis

Hi R community,

I have 4 sets of continuous data which I want to group into one and map on one axis against a success variable on the other axis. I'm using ggplot2/tidyverse.

Screenshot 2021-06-03 at 08.24.02

The data in the screenshot shows each category which I want to map on one axis (type of purchase) and this will be plotted against number of sales/total sales on the other.

Thank you for any help! :slight_smile:


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:

Short Version

You can share your data in a forum friendly way by passing the data to share to the dput() function.
If your data is too large you can use standard methods to reduce it before sending to dput().
When you come to share the dput() text that represents your data, please be sure to format your post with triple backticks on the line before your code begins to format it appropriately.

( example_df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 
3.4, 2.9, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 
1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 
0.4, 0.3, 0.2, 0.2, 0.1), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")))

When trying to run the reprex, I get this error... Any ideas?

Please, dont use screenshots when you are sharing text, its unfortunately often harder to read, and certainly harder to refer back to with proposed changes.

if you use df_paste on the head of some data.frame you wish to share, do so in your console, and copy the resulting text which is code to construct the frame, and put only that into your script, and not also the df_paste code from which you constructed it.

You should probably look at pivot_longer : Pivot data from wide to long — pivot_longer • tidyr

Something like this should do the trick:

pivot_longer(df, cols=everything(), names_to="PurchaseType", values_to="count")

Or with sum:

df %>%
pivot_longer(cols=everything(), names_to="PurchaseType", values_to="count") %>%
group_by(PurchaseType) %>%
summarise(count = sum(count ))

Hi Julien,

Thanks for this, I had a play around and it's worked. Just had to restructure the data :slight_smile:

1 Like