r - Can I have one log scale y-axis and one normal second y-axis?
问题描述
I will start off by saying I know this is generally frowned upon because it can be VERY misleading. However in my case I am just showing a timing relationship on the x-axis (the actual values on the second y-axis are fairly arbitrary in terms of my interpretations).
My first y-axis should be in log10 to show patterns in low concentration data. My second y-axis should just be "normal" with no transformation. Is this possible to do in ggplot? I found this previous question here, but the answer doesn't pertain to my issue necessarily.
Here is what I got going on now in my script:
toy$date <- as.POSIXct(strptime(all.data$date, format = "%m/%d/%y"))
dis$date <- as.POSIXct(strptime(discharge$date, format = "%m/%d/%y"))
p.ch4 <- ggplot(toy, aes(x=date, y=value)) +
geom_line(aes(color=type)) +
geom_point(aes(shape=type, fill=type), color = "black", size=4) +
geom_bar(data=dis, aes(x=date,y=discharge*20), inherit.aes = FALSE,
stat='identity', alpha=.25) +
scale_shape_manual(name = "Location:", values = c(23:25)) +
scale_fill_manual(name = "Location:",
values = c("orange2","olivedrab2","steelblue2")) +
scale_color_manual(name = "Location:",
values = c("orange2","olivedrab2","steelblue2")) +
scale_y_log10() +
scale_y_continuous(sec.axis = sec_axis(trans = ~./20, name = "Discharge (cms)")) +
labs(y = expression(paste('Methane (', mu, 'M)')), x = "", color = "Location",
shape = "Location")
print(p.ch4)
AAAND here is some toy data that mimics mine (but is much smaller/shorter):
> dput(toy)
structure(list(date = c("11/5/17", "11/6/17", "11/6/17", "1/5/18",
"1/5/18", "1/5/18", "3/27/18", "3/28/18", "3/29/18", "6/20/18",
"6/20/18", "6/20/18"), type = c("4cm", "12cm", "0cm", "4cm",
"12cm", "0cm", "4cm", "12cm", "0cm", "4cm", "12cm", "0cm"), variable = c("ch4",
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4",
"ch4", "ch4"), value = c(0.37, 0.7, 0.43, 10.21, 0.3, 0.29, 32.59,
7.1, 7.42, 117.09, 2.4, 1.54)), class = "data.frame", row.names = c(NA,
-12L))
> dput(dis)
structure(list(date = c("11/5/17", "1/5/18", "3/27/18", "6/20/18"
), discharge = c(0.64, 0.33, 0.31, 3.7)), class = "data.frame", row.names = c(NA,
-4L))
TIA!!!
解决方案
也许是这样的:
p.ch4 <- ggplot(toy, aes(x=date, y=value)) +
geom_line(aes(color=type)) +
geom_point(aes(shape=type, fill=type), color = "black", size=4) +
geom_bar(data=dis, aes(x=date,y=discharge*20), inherit.aes = FALSE,
stat='identity', alpha=.25) +
scale_shape_manual(name = "Location:", values = c(23:25)) +
scale_fill_manual(name = "Location:",
values = c("orange2","olivedrab2","steelblue2")) +
scale_color_manual(name = "Location:",
values = c("orange2","olivedrab2","steelblue2")) +
scale_y_log10(sec.axis = sec_axis(trans = ~./20, name = "Discharge (cms)", labels = scales::label_number())) +
labs(y = expression(paste('Methane (', mu, 'M)')), x = "", color = "Location",
shape = "Location")
print(p.ch4)
输出:
推荐阅读
- excel - Excel Sumif 我要查找的数字在字符串中
- php - PHP:如何增加序列号
- angular - 从前端发送的授权令牌不起作用(JWT)
- android - 发生内部错误。[无法解析主机“www.googleapis.com”:没有与主机名关联的地址]
- c - C预处理器:将未定义的宏视为0的动机是什么?
- python-3.x - OSERROR:未找到协议
- android - 无法按下具有 Navigator.push 功能的 Flutter Button (GestureDetector)
- python - 如何将一列字符串转换为python文字并提取值
- kubernetes - Kubernetes 服务类型:ExternalName 未正确解析
- python - 部署 Dash 应用程序时出现 Heroku 应用程序错误:“OSError:xlwings 需要安装 Excel,因此仅适用于 Windows 和 macOS。”