r - ggplot2 - 辅助重复轴日志转换不正确

时间:2016-12-07 06:59:50

标签: r ggplot2

我正在使用ggplot 2.2.0使用日志转换创建辅助重复轴。

# install.packages("install.load") # install to use the load_package function
install.load::load_package("ggplot2", "data.table")

sand <- structure(list(`Sieve #` = c("3/8”", "4", "8", "16", "30", "50", 
"Pan"), `Size (mm)` = c(9.525, 4.75, 2.36, 1.18, 0.6, 0.3, NA
), `Mass Sieve (kg)` = c(0.642, 0.508, 0.474, 0.408, 0.38, 0.348, 
0.376), `Mass Retained + Sieve (kg)` = c(0.642, 0.524, 0.58, 
0.526, 0.598, 0.899, 0.463), `Mass Retained (kg)` = c(0, 0.016, 
0.106, 0.118, 0.218, 0.551, NA), `Cumulative Mass Retained (kg)` = c(0, 
0.016, 0.122, 0.24, 0.458, 1.009, NA), `Cumulative % Retained` = c(0, 
1, 11, 22, 42, 92, NA), `% Passing` = c(100, 99, 89, 78, 58, 
8, NA)), .Names = c("Sieve #", "Size (mm)", "Mass Sieve (kg)", 
"Mass Retained + Sieve (kg)", "Mass Retained (kg)", "Cumulative Mass Retained (kg)", 
"Cumulative % Retained", "% Passing"), row.names = c(NA, -7L), class = c("data.table", 
"data.frame"))


x1 <- c(0.075, 0.15, 0.3, 0.6, 1.18, 2.36, 4.75, 9.5, 12.5, 19, 25, 37.5, 50)

x1_label <- c("0.075", "0.150", "0.300", "0.600", "1.180", "2.36", "4.75", "9.5", 
    "12.5", "19.0", "25.0", "37.5", "50.0")

x2 <- c("No. 200", "No. 100", "No. 50", "No. 30", "No. 16", "No. 8", "No. 4", 
    "3/8 in.", "1/2 in.", "3/4 in.", "1 in.", "1 1/2 in.", "2 in.")


ggplot(sand, aes(`Size (mm)`, `% Passing`)) + geom_point() + 
    geom_line() + scale_x_continuous(name = "Sieve size (mm)",
limits = c(0.075, 
    50), expand = c(0.001, 0), breaks = x1, labels = x1_label, minor_breaks = NULL, 
    trans = "log", position = "bottom", sec.axis = dup_axis(name = "Sieve size", 
        breaks = x1, labels = x2)) +
labs(title = "Group 1 Sand Gradation Results (ASTM C136)") + scale_y_continuous(limits =
c(0, 
    100), expand = c(0.01, 0), breaks = seq(0, 100, by = 10), minor_breaks = seq(0, 
    100, by = 5), name = "% Passing") +
theme_bw() + theme(plot.margin = margin(0.5, 0.5, 0.5, 0.5, "pt"),axis.text.x = element_text(angle = 90, vjust = 0.1))

底部x轴和顶部x轴在下图所示的图像中不匹配。

是否可以使用日志转换生成重复的辅助轴?

如果是这样,那么应该如何修改现有代码以获得所需的结果呢?

如果没有,那么你有什么建议。

谢谢。

enter image description here

0 个答案:

没有答案