I need a graph with a secondary y axis like this, but I'm having some issues: Graph I want

我已经try 了在我的代码中进行了很多更改,但似乎轴刻度不起作用,现在我得到了以下结果:

Graph I have now

这是我使用的代码:

    # Datos Clorofila-Radiación
    horario <- c("D1-6:30", "D1-12:00", "D1-18:00", "D2-6:30", "D2-12:00", "D2-18:00", "D3-        6:30", "D3-12:00", "D3-18:00")
    trh3 <- c(1.85, 1.21, 2.23, 1.50, 1.11, 1.86, 1.74, 1.2, 1.63)
    trh5 <- c(1.99, 1.50, 1.98, 1.90, 1.14, 2.01, 1.59, 1.04, 1.53)
    rad <- c(22.53, 1909.10, 30.32, 22.32, 2342.10, 12.65, 28.70, 1279.8, 28.68)

    # Crear un data frame
    datos <- data.frame(horario, trh3, trh5, rad)

    # Ordenar los datos por el orden deseado en el eje x
   datos$horario <- factor(datos$horario, levels = c("D1-6:30","D1-12:00","D1-18:00","D2-6:30","D2-12:00","D2-18:00","D3-6:30","D3-12:00","D3-18:00"))

   # Gráfico
   ggplot(datos, aes(x = horario)) +
      geom_line(aes(y = trh3, color = "trh3", group = 1)) +
      geom_line(aes(y = trh5, color = "trh5", group = 1)) +
      geom_line(aes(y = rad, color = "rad", group = 1), linetype = "dashed") +
      scale_y_continuous(
        name = "Clorofila (µg/ml)",
        breaks = seq(1, 2.5, by = 0.2),
        labels = seq(1, 2.5, by = 0.2)) +
      scale_y_continuous(
        sec.axis = sec_axis(~./1000, name = "Radiación solar (W/m²)", breaks = seq(0, 2500, by = 500)/1000),
        name = "Radiación solar (W/m²)",
        breaks = seq(0, 2500, by = 500),
        labels = seq(0, 2500, by = 500)) +
      scale_color_manual(
        values = c("trh3" = "blue", "trh5" = "green", "rad" = "red"),
        labels = c("trh3 (Clorofila)" = "trh3", "trh5 (Clorofila)" = "trh5", "Radiación solar")) +
      labs(x = "Semana de medición", y = "Parámetros", color = "Variable") +
      theme_minimal() +
      theme(legend.position = "bottom")

推荐答案

您的代码可能有几个问题.这可能会解决其中的一些问题:

ggplot(datos, aes(x = horario)) +
  geom_line(aes(y = trh3*1000, color = "trh3", group = 1)) +
  geom_line(aes(y = trh5*1000, color = "trh5", group = 1)) +
  geom_line(aes(y = rad, color = "rad", group = 1), linetype = "dashed") +
  scale_y_continuous(
    sec.axis = sec_axis(~./1000, name = "Radiación solar (W/m²)", breaks = seq(0, 2500, by = 500)/1000),
   name = "Clorofila (µg/ml)",
    breaks = seq(0, 2500, by = 500),
    labels = seq(0, 2500, by = 500)) +
  scale_color_manual(
    values = c("trh3" = "blue", "trh5" = "green", "rad" = "red"),
    labels = c("trh3 (Clorofila)" = "trh3", "trh5 (Clorofila)" = "trh5", "Radiación solar")) +
  labs(x = "Semana de medición", y = "Parámetros", color = "Variable") +
  theme_minimal() +
  theme(legend.position = "bottom")

enter image description here

R相关问答推荐

如何在弹性表中为类别值的背景上色

高质量地将R格式的图表从Word中输出

从多个前置日期中获取最长日期

具有多个依赖变量/LHS的逻辑模型

如何使用R对每组变量进行随机化?

derrr mutate case_when grepl不能在R中正确返回值

如何提取所有完美匹配的10个核苷酸在一个成对的匹配与生物字符串在R?>

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

用约翰逊分布进行均值比较

为什么我对圆周率图的蒙特卡罗估计是空的?

在ggplot2上从多个数据框创建复杂的自定义图形

`-`是否也用于数据帧,有时使用引用调用?

将列的值乘以在不同数据集中找到的值

Conditional documentr::R中数据帧的summarize()

抽样变换-REXP与RWEIBUR

使用一个标签共享多个组图图例符号

如何在R中的两列以上使用联合(&U)?

带查找数据的FCT_REORDER.帧

如果y中存在x中的值,则将y行中的多个值复制到相应的x行中

如何在类应用函数中访问函数本身