Two Y axis histogram (point and boxplot)

I have this kind of df :

df<- tribble(
~Value, ~Depth, ~Date
-96.14,	10,	20211013
-94.62,	20,	20211013
-92.41,	30,	20211014
-91.27, 40, 20211014
-84.14,	50,	20211017
-74.91,	60,	20211017
-72.61,	70,	20211017
-69.74,	80,	20211018
-69.73,	90,	20211018
-73.83,	100,20211019
-77.27,	110,20211019
-79.79,	120,20211019
-80.89,	130,20211020
-82.44,	140,20211020
-82.43,	150,20211023
-80.94,	160,20211023
-80.11	170,20211025
-78.18,	180,20211025

I would like to have two different kind of plot :

  • first one a plot with X = date, Y1 = depth Y2=Value ; where I would have one point (for each date) equal to the mean value for each date
  • second, same configuration X = date, Y1 = depth Y2=Value, but instead of the point a boxplot depending of all the "value" value for each date

I really don't know how to explain so don't hesite to ask if you need more info about it

Thanks a lot

As Y2 for value, I don't want a other axis but a color gradient according to the value

This would work for your first plot:

library(dplyr)
library(tibble)
library(ggplot2)
library(tidyr)

df<- tribble(
    ~Value, ~Depth, ~Date,
    -96.14,	10,	20211013,
    -94.62,	20,	20211013,
    -92.41,	30,	20211014,
    -91.27, 40, 20211014,
    -84.14,	50,	20211017,
    -74.91,	60,	20211017,
    -72.61,	70,	20211017,
    -69.74,	80,	20211018,
    -69.73,	90,	20211018,
    -73.83,	100,20211019,
    -77.27,	110,20211019,
    -79.79,	120,20211019,
    -80.89,	130,20211020,
    -82.44,	140,20211020,
    -82.43,	150,20211023,
    -80.94,	160,20211023,
    -80.11,	170,20211025,
    -78.18,	180,20211025
)

# Transform Date so that it is an Actual Date class

df1 <- df %>% 
    mutate(
        Date = as.character(Date) %>% 
            as.Date(format = '%Y%m%d')
    )

# Plot one
df1 %>% 
    pivot_longer(
        cols = Value:Depth,
        names_to = 'measure'
    ) %>% 
    group_by(Date, measure) %>% 
    summarize(
        value = mean(value)
    ) %>% 
    ungroup() %>% 
    ggplot(aes(x = Date, y = value, color = measure)) +
    geom_point()

image

Regarding your second plot... could you explain more about what you want for the boxplots? A boxplot (such as the one drawn by ggplot2::geom_boxplot relies on computing quartiles, but your data doesn't have enough observations per date to do this computation. Is that just a product of your reprex, and your real dataset has more? Or are you just trying to get a sense of the data distribution for each date and for the "Depth" and "Value" measures?

1 Like

thanks for your response !
For my first plot it something like this

Mean_df <- df %>% 
  group_by(date) %>% 
  summarise(AvgValue = mean(Value), AvgDepth = mean(Depth))

Mean_df  %>% 
  group_by(date) %>% 
  ggplot(aes(date, y = AvgDepth, col =AvgValue)) +
  geom_line () +
  geom_point() +
  scale_colour_gradient(low = "yellow", high = "red", na.value = NA) +
  labs(y = "Average Depth (m)", x = "Date")

And indeed I have a df more complexe than this example so I have many data for each date
And I would like to have instead of the point have a boxplot for all the value according to the date

I tried this code but I have all my point doing a "bar" and I have a big boxplot for all the df and note for each date

df %>% 
  group_by(date) %>% 
  ggplot(aes(x=date,y=depth, col=value)) +
  geom_jitter(width=0.25) +
  geom_boxplot(alpha=0.5, outlier.shape=NA) + 
  theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))+
  scale_colour_gradient(low = "yellow", high = "red", na.value = NA)

Complicated to explain I'm sorry

I succeed to have a df like this,
and now I just want to plot boxplots per date (in X) where there is all the values point according to the depth and coloured by scale_color_gradient

df <- tribble(
  ~Depth, ~20211015, ~20211016, ~20211017, ~20211018, ~20211019
10, 0.5, 0.36, 0.12, 0.45, 0.58
20, 0.25, 0.36,  0.78, 0.15, 0.26
30, 0.12, 0.36, 0.58, 0.47, 0.23
40, 0.19, 0.98, 0.45, 0.32, 0.14
50, 0.14, 0.58, 0.47, 0.26, 0.42
60, 0.36, 0.48, 0.58, 0.14, 0.85 
70, 0.58, 0.75, 0.91, 0.47, 0.25, 
80, 0.54, 0.31, 0.25, 0.41, NA,
90, NA, 0.47, 0.36, 0.45, NA, 
100,NA, 0.71, NA, 0.19, NA,

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.