繁体   English   中英

R:在ggplot中使用log10刻度的y轴自定义刻度标记

[英]R: custom tick markers for y-axis using log10 scale in ggplot

当我使用log10刻度时,尝试创建漂亮的y轴时遇到很大的困难。我尝试了在SO上看到的东西,但它们给了我一片混乱。 这是我想要的y轴: 在此处输入图片说明

我需要在scale_y_log10()函数中提供哪种类型的参数才能获得所需的结果? 任何帮助或建议,我们将不胜感激! 提前致谢! 我正在使用的代码:

library(ggplot2)

box_whisk_graph<-ggplot(data = box_fresh_chloride, aes(x = factor(year), y = val)) +
  geom_boxplot(coef=5) +
  geom_hline(aes(yintercept = 230,colour="red"),size=1.3)+
  geom_hline(aes(yintercept = 860,colour="#FF3333"),size=1.3,linetype="dotted")+
     scale_y_continuous(trans="log10",limits=c(10,2000),breaks=c(10,2000,100))

我得到的情节: 在此处输入图片说明

样本数据集:

box_fresh_chloride = structure(list(orgid = c("USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ"), locid = c("USGS-01482500", 
"USGS-0146453250", "USGS-01392150", "USGS-01411035", "USGS-01411466", 
"USGS-01411444", "USGS-01407821", "USGS-01464527", "USGS-01405340", 
"USGS-01409387", "USGS-01403385", "USGS-01467005", "USGS-01409815", 
"USGS-0146708240", "USGS-01411110", "USGS-01400000", "USGS-01467150", 
"USGS-01391500", "USGS-01467010", "USGS-0140940950"), stdate = structure(c(16394, 
16610, 16328, 16583, 16602, 16602, 16602, 16588, 16588, 16588, 
16588, 16589, 16590, 16590, 16594, 16589, 16589, 16595, 16602, 
16583), class = "Date"), sttime = structure(c(35100, 39600, 38400, 
35100, 43200, 37800, 36900, 33600, 45000, 46200, 40500, 47400, 
42300, 36000, 39600, 40500, 34200, 39600, 34200, 45000), class = c("hms", 
"difftime"), units = "secs"), charnam = c("Chloride", "Chloride", 
"Chloride", "Chloride", "Chloride", "Chloride", "Chloride", "Chloride", 
"Chloride", "Chloride", "Chloride", "Chloride", "Chloride", "Chloride", 
"Chloride", "Chloride", "Chloride", "Chloride", "Chloride", "Chloride"
), val = c(23.6, 221, 144, 10.8, 10.5, 5.76, 37.1, 22.5, 78.3, 
8.67, 51.5, 17.9, 4.48, 55.8, 16.1, 62.9, 67.6, 187, 47.5, 27.7
), valunit = c("mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", 
"mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", 
"mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l"), swqs = c("FW2-NT", 
"FW2-NT", "FW2-NT", "PL", "FW2-NT", "PL", "FW2-NT", "FW2-NT", 
"FW2-NT", "PL", "FW2-NT", "FW2-NT", "PL", "FW2-NT", "PL", "FW2-NT", 
"FW2-NT", "FW2-NT", "FW2-NT", "PL"), WMA = c(17L, 20L, 4L, 15L, 
17L, 16L, 12L, 20L, 9L, 14L, 9L, 19L, 14L, 18L, 15L, 8L, 18L, 
4L, 19L, 14L), year = c(2014, 2015, 2014, 2015, 2015, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
2015, 2015)), .Names = c("orgid", "locid", "stdate", "sttime", 
"charnam", "val", "valunit", "swqs", "WMA", "year"), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

解决此问题的一种方法是手动为自己构建“ breaks”值的向量,然后将这些值提供给scale_y_continuous()函数调用。

breaks_vec = rep(seq(from=1, to=10), 3) * 10^rep(seq(from=1, to=3), each=10)

box_whisk_graph <- ggplot(data=box_fresh_chloride, aes(x=factor(year), y=val)) +
                   theme_bw() +
                   geom_boxplot(coef=5) +
                   geom_hline(yintercept=230, colour="red", size=1.3)+
                   geom_hline(yintercept=860, colour="#FF3333", size=1.3, 
                              linetype="dotted")+
                   scale_y_continuous(trans="log10", limits=c(10,2000), 
                                      breaks=breaks_vec, minor_breaks=NULL)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM