首页 > 解决方案 > 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!!!

标签: rggplot2

解决方案


也许是这样的:

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)

输出:

在此处输入图像描述


推荐阅读