yuchen xiao
yuchen xiao

Reputation: 17

ggplot with 2 y axes on each side and different scales for different data

I encounter a problem in plotting data for dual-y axis in ggplot.

I am plotting the injection history of a number of injection wells. Each injection well has a unique 'UWI' associated with it, in addition, it has 'INJ', the monthly injection volume for each 'DATE' for that well. I also calculated the cumulative injection volume for each well, 'cum_inj' and total injection volume for each well, 'tol_inj'.

Also, I calculated the cumulative injection volume of all wells combined for each 'DATE', in "CUMU_INJ'.

I want to display the 'cumu_inj' for each wells and 'CUMU_INJ' of all wells combined in one plot.

Below is my current code, I think because I have many individual wells colored by unique 'UWI', I can't get the right scales for the second y axis.

ggplot(monthly_inj) +
geom_line(aes(x = DATE, y = INJ, color = UWI)) +
geom_line(aes(x = DATE, y = CUMU_INJ), color = "black") +
scale_x_date('Year', date_breaks = "2 year", date_labels = '%Y', expand = expansion (add = 5, mult = 0.01) ) + 
scale_y_continuous("Cumulative Injection Volume of All Wells (MMbbl)", 
                     sec.axis = sec_axis(~ ./10, name = "Cumulative Injection Volume of Each Wells (MMbbl)")) +
labs(title = "Cumulative Injection Volume ",
       subtitle = "Cumulative Injection Volume for 34 SWD") + theme_bw(base_size = 15) +
theme(legend.position = "none",
        axis.text.y.right = element_text(color = 'black'), 
        axis.title.y.right = element_text(color = 'black')
) 

The data is here at my github: https://github.com/yuchenxiao95/Raster-Method-for-Oklahoma-Catalog/blob/master/monthly_inj.csv

A display of the dataframe

enter image description here

Here is a display of my current plot

enter image description here

Upvotes: 1

Views: 168

Answers (1)

YBS
YBS

Reputation: 21297

You should try this:

geom_line(aes(y = INJ*15, color = UWI))

You will get:

output

It is hard to distinguish 34 shades of color in my plot. Also, you can try multiplying by 10 or 20.

Upvotes: 1

Related Questions