Search code examples
rggplot2plotgraphtidyverse

Problems with a graph in R with tidyverse e ggplot


I have a dataset called 'data' and I tried to make some graphs. but i have a problem with one in particoular.

this is the image

as you can see the the x axis is not right, how can I change that?

of course if you have other type of graphs but with the same 'concept', they're welcome

the dataset looks like this (adulti=adults, bambini=children, totale_pagato=price)


Events                 Date          adulti   bambini       totale_pagato
   <fct>                  <fct>      <fct>     <fct>           <dbl>
 1 "Ski Days "            08/01/2022   1        0                39  
 2 "Ski Days "            09/01/2022   1        0                42  
 3 "Ski Days "            09/01/2022   1        1                42  
 4 "Franciacorta Village" 06/01/2022   2        1                19.8
 5 "Ski Days "            06/01/2022   1        0                39  
 6 "Ski Days "            09/01/2022   1        0                42  
 7 "Ski Days "            09/01/2022   3        2               126  
 8 "Ski Days "            06/01/2022   2        0                86  
 9 "Ski Days "            09/01/2022   1        1                42  
10 "Ski Days "            06/01/2022   1        1                34  


data %>% 
  group_by(adulti) %>% 
  mutate(mean = mean(totale_pagato),
         quantile = quantile(totale_pagato, probs = 0.9)) %>% 
  ungroup %>% 
  pivot_longer(cols = c(mean, quantile), names_to = "summary_stat") %>% 
  ggplot(aes(x = adulti, y = value, group = summary_stat)) +
  geom_jitter(aes(x = adulti, y = totale_pagato), inherit.aes = FALSE, alpha = 1/10) +
  geom_line(aes(lty = summary_stat, col = summary_stat)) +
  scale_colour_manual(values = c("orange", "red")) +
  labs(y = "trip.duration.hr")


this is the code I used, I would like something like this


Solution

  • Your current problem is that adulti is formatted as factor. What you want to have in your case is a continuous variable. Try converting it to as.integer or as.numeric. Alternatively you can adjust the levels of your factor if you are required to keep them as factors.