【问题标题】:Tricky Figure: Plot values of two variable on same x-axis in R or excel棘手的图:在 R 或 excel 中的同一 x 轴上绘制两个变量的值
【发布时间】:2021-08-10 11:35:48
【问题描述】:

我正在尝试使用此 excel Data here 像此 制作一个图形,但我无法将两个具有不同值的变量放在 x 轴上。

library(xlsx2)
test <- read.xlsx2("E:/Plot/oz.xlsx",1, header=TRUE) 
test$Ozone = as.numeric(as.character(test$Ozone))
test$Altitude = as.numeric(as.character(test$Altitude))
test$Pressure = as.numeric(as.character(test$Pressure))
round(test$Altitude) # i want to round the values of Altitude
library(ggplot2)
ylim.prim <- c(0, 34)   # in this example, precipitation
ylim.sec <- c(1010, 10) 
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
ggplot(test, aes(x = Ozone, y = Altitude,color = 'Ozone Partial Pressure (mPa)'))+ 
  geom_path(aes(x = Ozone))+ scale_y_continuous(name="Altitude (km)",sec.axis=sec_axis(~(.- a)/b, name = 'Pressure (hPa)'))+
  scale_x_continuous(name="Temperature (km)",sec.axis=sec_axis(~(.), name = 'Temperature (C)'))+
  theme_test() + theme(legend.position = c(0.01, 0.14),legend.justification = c(0, -4))

但我得到了这样的情节。

在此阶段我将不胜感激。谢谢

【问题讨论】:

  • 看起来像 stackoverflow.com/questions/68722954/… 的副本,但还没有答案。
  • 同一用户有两个帐户。我迫切地想找到答案 :) 它的紧迫性
  • 这只是关于 x 轴上的标签还是你想在这个图上画第二条线?如果是后者,那么第二行的 x,y 变量应该是什么?
  • 只是轴名称的值:请看示例图...x轴应该有温度和臭氧...y轴将保持不变
  • 这个问题是在 2 年前提出的,但在 stackoverflow.com/questions/51833159/… 没有答案

标签: r excel ggplot2 plot profile


【解决方案1】:

如果您希望每个中断有两个标签,您可以使用\n 将它们分开。您可以手动执行此操作,或者如果您知道使用提供给比例尺的 labels 参数的函数进行转换。从示例图中我看到温度是4x - 100,其中x 是臭氧标签。但是,从您的数据来看,温度和臭氧似乎不是共线的。

library(ggplot2)

# Downloaded from google sheets as tsv
file <- file.choose()
df <- read.table(file, sep = "\t", header = TRUE)

# Per example figure
ozone2temp <- function(x){x * 4 - 100}

# Simplified for brevity
ggplot(df) +
  geom_path(aes(Ozone, Altitude, colour = "Ozone")) +
  scale_x_continuous(
    labels = function(x) {
      paste(x, ozone2temp(x), sep = "\n")
    },
    name = "Ozone\nTemp"
  )

reprex package (v1.0.0) 于 2021-08-10 创建

编辑:

如果您还想以温度作为 x 变量来绘制海拔高度,您还需要进行反向转换:

library(ggplot2)

# Downloaded from google sheets as tsv
file <- file.choose()
df <- read.table(file, sep = "\t", header = TRUE)

# Per example figure
ozone2temp <- function(x){x * 4 - 100}
temp2ozone <- function(x){(x + 100) / 4}

ggplot(df, aes(y = Altitude)) +
  geom_path(aes(Ozone, colour = "Ozone")) +
  geom_path(aes(temp2ozone(Temperature), 
                colour = "Temperature")) +
  scale_x_continuous(
    labels = function(x) {
      paste(x, ozone2temp(x), sep = "\n")
    },
    name = "Ozone\nTemp"
  )

reprex package (v1.0.0) 于 2021-08-10 创建

【讨论】:

  • 太好了!非常感谢你。为了增加温度,除了“geom_path(aes(x=Ozone),color="red")
  • 这次明白了。非常感谢您的支持。明天是审查的最后日期,希望这个数字会公布。我会回到这篇文章并让你知道。 :)
  • 嘿 ...@teunbrand 我回来感谢您的帮助,并感谢我今天发表的研究工作。谢谢你。 authors.elsevier.com/c/1e2jLB8ccr2Bu DOI 这里:doi.org/10.1016/j.scitotenv.2021.151135
  • 太棒了,恭喜发表 :) 我不确定我是否应该对这段小代码给予正式的认可,但谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多