创建一个带有两个Y轴的ggplot。

huangapple go评论105阅读模式
英文:

Bar ggplot with two y-axes

问题

我尝试使用ggplot创建一个双Y轴的柱状图,但不确定我的代码哪里有问题,因为我已经与许多其他示例进行了比对。数据框(mun_ndvi_lst_long)已经处于如下的长格式:

  1. mun_ndvi_lst_long <- data.frame(Municipality = c("City of Miami", "City of Miami",
  2. "Opa-Locka", "Opa-Locka", "Coral Gables", "Coral Gables", "South Miami",
  3. "South Miami", "Doral", "Doral", "Miami Beach", "Miami Beach",
  4. "Pinecrest", "Pinecrest", "Florida City", "Florida City", "Homestead",
  5. "Homestead", "Hialeah", "Hialeah", "Miami Shores", "Miami Shores"
  6. ), Variable = c(25.02, 0.181, 25.8, 0.183, 22.67, 0.275, 24.13,
  7. 0.276, 24.57, 0.188, 23.71, 0.157, 23.36, 0.322, 23.1, 0.28,
  8. 23.99, 0.262, 26.08, 0.163, 24.05, 0.259), Type = c("LST", "NDVI",
  9. "LST", "NDVI", "LST", "NDVI", "LST", "NDVI", "LST", "NDVI", "LST",
  10. "NDVI", "LST", "NDVI", "LST", "NDVI", "LST", "NDVI", "LST", "NDVI",
  11. "LST", "NDVI"))

以下是我的代码:

  1. ggplot(mun_ndvi_lst_long,
  2. aes(x = Municipality,
  3. y = Variable,
  4. fill = Type)) +
  5. geom_col(position = "dodge") +
  6. scale_y_continuous(sec.axis = sec_axis(~ . * 0.015, name = "NDVI")) +
  7. labs(y = "LST (°C)")

我得到了以下结果,看起来NDVI仍然对应左侧的Y轴而不是右侧的Y轴:

创建一个带有两个Y轴的ggplot。

英文:

I'm trying to plot data in a dual y-axis bar plot via ggplot...not sure what it wrong with my code here as I've checked it against many other examples. The dataframe (mun_ndvi_lst_long) is already in long format as follows:

  1. mun_ndvi_lst_long &lt;- data.frame(Municipality = c(&quot;City of Miami&quot;, &quot;City of Miami&quot;,
  2. &quot;Opa-Locka&quot;, &quot;Opa-Locka&quot;, &quot;Coral Gables&quot;, &quot;Coral Gables&quot;, &quot;South Miami&quot;,
  3. &quot;South Miami&quot;, &quot;Doral&quot;, &quot;Doral&quot;, &quot;Miami Beach&quot;, &quot;Miami Beach&quot;,
  4. &quot;Pinecrest&quot;, &quot;Pinecrest&quot;, &quot;Florida City&quot;, &quot;Florida City&quot;, &quot;Homestead&quot;,
  5. &quot;Homestead&quot;, &quot;Hialeah&quot;, &quot;Hialeah&quot;, &quot;Miami Shores&quot;, &quot;Miami Shores&quot;
  6. ), Variable = c(25.02, 0.181, 25.8, 0.183, 22.67, 0.275, 24.13,
  7. 0.276, 24.57, 0.188, 23.71, 0.157, 23.36, 0.322, 23.1, 0.28,
  8. 23.99, 0.262, 26.08, 0.163, 24.05, 0.259), Type = c(&quot;LST&quot;, &quot;NDVI&quot;,
  9. &quot;LST&quot;, &quot;NDVI&quot;, &quot;LST&quot;, &quot;NDVI&quot;, &quot;LST&quot;, &quot;NDVI&quot;, &quot;LST&quot;, &quot;NDVI&quot;, &quot;LST&quot;,
  10. &quot;NDVI&quot;, &quot;LST&quot;, &quot;NDVI&quot;, &quot;LST&quot;, &quot;NDVI&quot;, &quot;LST&quot;, &quot;NDVI&quot;, &quot;LST&quot;, &quot;NDVI&quot;,
  11. &quot;LST&quot;, &quot;NDVI&quot;))

Here is my code:

  1. ggplot(mun_ndvi_lst_long,
  2. aes(x = Municipality,
  3. y = Variable,
  4. fill = Type)) +
  5. geom_col(position = &quot;dodge&quot;) +
  6. scale_y_continuous(sec.axis = sec_axis(~ . * 0.015, name = &quot;NDVI&quot;)) +
  7. labs(y = &quot;LST (&#176;C)&quot;)

I end up with the following, where it looks like NDVI is still corresponding to the left y-axis rather than the right:

创建一个带有两个Y轴的ggplot。

答案1

得分: 2

你需要使用 sec_axis 中的转换的逆转换来转换你的 Variable 值:

  1. library(dplyr)
  2. library(ggplot2)
  3. mun_ndvi_lst_long %>%
  4. mutate(Variable = ifelse(Type == "NDVI", Variable / 0.015, Variable)) %>%
  5. ggplot(aes(x = Municipality, y = Variable, fill = Type)) +
  6. geom_col(position = "dodge") +
  7. scale_y_continuous(sec.axis = sec_axis(~ . * 0.015, name = "NDVI")) +
  8. labs(y = "LST (°C)")

不过,在大多数情况下,这不是理想的可视化实践。我建议使用单独的轴来进行分面:

  1. ggplot(
  2. mun_ndvi_lst_long,
  3. aes(x = Municipality, y = Variable, fill = Type)
  4. ) +
  5. geom_col(show.legend = FALSE) +
  6. facet_wrap(vars(Type), ncol = 1, scales = "free") +
  7. labs(y = "LST (°C)")
英文:

You would need to transform your Variable values with the inverse of the transformation in sec_axis:

  1. library(dplyr)
  2. library(ggplot2)
  3. mun_ndvi_lst_long %&gt;%
  4. mutate(Variable = ifelse(Type == &quot;NDVI&quot;, Variable / 0.015, Variable)) %&gt;%
  5. ggplot(aes(x = Municipality, y = Variable, fill = Type)) +
  6. geom_col(position = &quot;dodge&quot;) +
  7. scale_y_continuous(sec.axis = sec_axis(~ . * 0.015, name = &quot;NDVI&quot;)) +
  8. labs(y = &quot;LST (&#176;C)&quot;)

创建一个带有两个Y轴的ggplot。

However, this isn’t ideal visualization practice in most cases. I would recommend facets with separate axes instead:

  1. ggplot(
  2. mun_ndvi_lst_long,
  3. aes(x = Municipality, y = Variable, fill = Type)
  4. ) +
  5. geom_col(show.legend = FALSE) +
  6. facet_wrap(vars(Type), ncol = 1, scales = &quot;free&quot;) +
  7. labs(y = &quot;LST (&#176;C)&quot;)

创建一个带有两个Y轴的ggplot。

huangapple
  • 本文由 发表于 2023年4月4日 11:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定