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

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

Bar ggplot with two y-axes

问题

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

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

以下是我的代码:

ggplot(mun_ndvi_lst_long, 
       aes(x = Municipality,
           y = Variable, 
           fill = Type)) + 
geom_col(position = "dodge") + 
scale_y_continuous(sec.axis = sec_axis(~ . * 0.015, name = "NDVI")) + 
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:

mun_ndvi_lst_long &lt;- data.frame(Municipality = c(&quot;City of Miami&quot;, &quot;City of Miami&quot;, 
&quot;Opa-Locka&quot;, &quot;Opa-Locka&quot;, &quot;Coral Gables&quot;, &quot;Coral Gables&quot;, &quot;South Miami&quot;, 
&quot;South Miami&quot;, &quot;Doral&quot;, &quot;Doral&quot;, &quot;Miami Beach&quot;, &quot;Miami Beach&quot;, 
&quot;Pinecrest&quot;, &quot;Pinecrest&quot;, &quot;Florida City&quot;, &quot;Florida City&quot;, &quot;Homestead&quot;, 
&quot;Homestead&quot;, &quot;Hialeah&quot;, &quot;Hialeah&quot;, &quot;Miami Shores&quot;, &quot;Miami Shores&quot;
), Variable = c(25.02, 0.181, 25.8, 0.183, 22.67, 0.275, 24.13, 
0.276, 24.57, 0.188, 23.71, 0.157, 23.36, 0.322, 23.1, 0.28, 
23.99, 0.262, 26.08, 0.163, 24.05, 0.259), Type = c(&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;, &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;, &quot;LST&quot;, &quot;NDVI&quot;, 
&quot;LST&quot;, &quot;NDVI&quot;))

Here is my code:

ggplot(mun_ndvi_lst_long, 
       aes(x = Municipality,
           y = Variable, 
           fill = Type)) + 
geom_col(position = &quot;dodge&quot;) + 
scale_y_continuous(sec.axis = sec_axis(~ . * 0.015, name = &quot;NDVI&quot;)) + 
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 值:

library(dplyr)
library(ggplot2)

mun_ndvi_lst_long %>%
  mutate(Variable = ifelse(Type == "NDVI", Variable / 0.015, Variable)) %>%
  ggplot(aes(x = Municipality, y = Variable, fill = Type)) + 
  geom_col(position = "dodge") + 
  scale_y_continuous(sec.axis = sec_axis(~ . * 0.015, name = "NDVI")) + 
  labs(y = "LST (°C)")

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

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

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

library(dplyr)
library(ggplot2)

mun_ndvi_lst_long %&gt;%
  mutate(Variable = ifelse(Type == &quot;NDVI&quot;, Variable / 0.015, Variable)) %&gt;%
  ggplot(aes(x = Municipality, y = Variable, fill = Type)) + 
  geom_col(position = &quot;dodge&quot;) + 
  scale_y_continuous(sec.axis = sec_axis(~ . * 0.015, name = &quot;NDVI&quot;)) + 
  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:

ggplot(
  mun_ndvi_lst_long,
  aes(x = Municipality, y = Variable, fill = Type)
  ) + 
  geom_col(show.legend = FALSE) + 
  facet_wrap(vars(Type), ncol = 1, scales = &quot;free&quot;) + 
  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:

确定