Home > database >  How to plot two variables over the top of each other using ggplot2
How to plot two variables over the top of each other using ggplot2

Time:10-02

I have a data frame that looks as below:

Engine | Predicted RUL| Actual RUL 
1      | 172          | 112      
2      | 126          | 98
3      | 27           | 69
4      | 52           | 82
5      | 84           | 91
...    | ...          | ...
100    | 5            | 20

I would like to visualize Predicted RUL compared to Actual RUL for each Engine. Similar to enter image description here

Perhaps, using dodged bar plot would be easier to visualize ?

df %>%
  pivot_longer(cols = -Engine, names_to = "RUL_Type") %>%
  ggplot(aes(Engine, value, fill = RUL_Type))   
  geom_col(position = "dodge")   
  scale_fill_manual(values = c("blue", "red"))

enter image description here

data

df <- structure(list(Engine = 1:5, Predicted_RUL = c(172L, 126L, 27L, 
52L, 84L), Actual_RUL = c(112L, 98L, 69L, 82L, 91L)), row.names = c(NA, 
5L), class = "data.frame")
  • Related