我正在嘗試將主 y 軸用于 mean_section_eur,并將第二個 y 軸用于 incr_eur,目前輸出不是我想要的,因為兩個資料仍使用主 y 軸。這是我的資料:
tibble::tribble(
~Wells_per_section, ~mean_section_eur, ~incr_eur,
1L, 746279.041157111, 746279.041157111,
2L, 1431269.95778565, 684990.916628538,
3L, 2108357.80794982, 677087.85016417,
4L, 2772843.81583265, 664486.007882829,
5L, 3405157.94680437, 632314.130971724,
6L, 3649485.94300659, 244327.996202213,
7L, 3815891.88964587, 166405.946639284,
8L, 3954427.79923768, 138535.909591812,
9L, 4080577.72763043, 126149.928392747,
10L, 4191966.2500121, 111388.522381674,
11L, 4296140.38025762, 104174.13024552,
12L, 4400781.49373418, 104641.113476554,
13L, 4499603.6595165, 98822.165782324,
14L, 4594918.07191796, 95314.4124014592,
15L, 4685908.80682599, 90990.7349080276,
16L, 4768224.63681244, 82315.8299864484
)
sec.axis = sec_axis( trans=~.*1, name="Second Axis")
我的代碼:
avg_dual_plot <- function(reservoir_model, simulation_case, optimal_spacing,
Wells_per_section, well_eur, tolerance){
avg_section <- qualified_eur %>%
dplyr::group_by(Wells_per_section) %>%
dplyr::summarise(mean_section_eur = mean(section_eur)) %>%
dplyr::mutate(incr_eur = incrementalEUR(mean_section_eur))
avg_dual_plots <- avg_section %>%
ggplot2::ggplot(avg_section, mapping = aes(x = Wells_per_section))
geom_line(mapping =aes(y = mean_section_eur))
geom_line(mapping =aes(y = incr_eur))
scale_y_continuous(
# Features of the first axis
name = "section eur",
# Add a second axis and specify its features
sec.axis = sec_axis(~rescale(., c(0, 500000)), name="incr eur"))
#return(avg_dual_plots)
return(avg_section)
}
我的代碼是基于其他函式的函式,因此 avg_section 是另一個函式的結果,我正在使用 avg_section 這是我提供的資料來制作繪圖。我得到的結果是:
我期待的輸出:
uj5u.com熱心網友回復:
請記住,當您向繪圖添加輔助軸時,它只是一個惰性注釋。它絕不會改變繪圖上線條或點的外觀。如果沒有輔助軸的線條看起來不對,那么它們也會看起來不對。
您需要做的是乘以(或除以,或以其他方式轉換)您的資料系列之一,使其成為您想要在繪圖上的大小。次軸簡單地進行逆變換,以便我們可以正確解釋轉換后的系列的數字。
在您的示例中,該incr_eur
線大約是您想要的垂直大小的六分之一,因此我們需要將incr_eur
資料乘以 6 以獲得我們想要的大小。然后我們告訴sec_axis
顯示 y 值是主 y 軸上的值的 1/6:
ggplot(avg_section, mapping = aes(x = Wells_per_section))
geom_line(mapping =aes(y = mean_section_eur),
color = '#ec7e34')
geom_point(aes(y = mean_section_eur), color = '#ec7e34')
geom_line(mapping = aes(y = incr_eur * 6),
color = '#2e4a7d')
geom_point(aes(y = incr_eur * 6), color = '#2e4a7d')
scale_y_continuous(
labels = scales::comma,
name = "section eur",
sec.axis = sec_axis(~.x/6, name="incr eur", labels = scales::comma))
lims(x = c(0, 25))
theme_light()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491041.html