如何在R中创建一个带有次要分组Y轴的条形图?

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

How to create a bar plot with a secondary grouped y-axis in R?

问题

我想创建一个类似以下数据的条形图:

卫星	        参考文献	            原位观测	遥感观测
Landsat 8 OLI	Zhu et al., 2022	0.624	0.554325
Landsat 8 OLI	Huang et al., 2021	2.15	1.52
Landsat 5 TM	Huang et al., 2021	2.15	2.3
MODIS	        Wu et al., 2022	    0.778	0.64
MODIS	        Yu et al., 2016	    0.0895	0.10546
MODIS	        Xu et al., 2010	    0.1305	0.11799
Sentinel2A MSI	Huang et al., 2023	0.31	0.41

我已经尝试了以下代码,但结果不符合预期。因此,我在寻求帮助。

ggplot(d1, aes(x = pH.value, y = Reference, fill = Type)) +
   geom_col(position = position_dodge()) +
   facet_wrap(vars(Satellite, Reference), strip.position = "left")

如何在R中创建一个带有次要分组Y轴的条形图?


<details>
<summary>英文:</summary>

I would like to create an bar graph like that with the following data

Satellite Reference In-situ Remote sensing
Landsat 8 OLI Zhu et al., 2022 0.624 0.554325
Landsat 8 OLI Huang et al., 2021 2.15 1.52
Landsat 5 TM Huang et al., 2021 2.15 2.3
MODIS Wu et al., 2022 0.778 0.64
MODIS Yu et al., 2016 0.0895 0.10546
MODIS Xu et al., 2010 0.1305 0.11799
Sentinel2A MSI Huang et al., 2023 0.31 0.41

[![enter image description here][1]][1]


I have tried the following code, but not expecting what is showing.. Therefore i am seeking help.

ggplot(d1, aes(x =pH.value, y =Reference, fill = Type))+
geom_col(position = position_dodge()) +
facet_wrap(vars(Satellite, Reference), strip.position = "left")



  [1]: https://i.stack.imgur.com/lCKSX.jpg

</details>


# 答案1
**得分**: 2

只要将数据转换为长格式,就可以在图表左侧的外部使用分面展示。

无需翻转坐标轴 - 您可以简单地将值放在x轴上:

<details>
<summary>英文:</summary>

As long as you pivot to long format, you can plot this with facets placed on the outside on the left of the plot.

You don&#39;t need to flip co-ordinates - you can simply put the values on the x axis:
``` r
library(tidyverse)

d1 %&gt;%
  mutate(Satellite = factor(Satellite, rev(unique(Satellite))),
         Reference = factor(Reference, unique(Reference))) %&gt;%
  pivot_longer(`In-situ`:`Remote sensing`) %&gt;%
  mutate(name = factor(name, unique(name))) %&gt;%
  ggplot(aes(value, Reference, fill = name)) +
  geom_col(position = position_dodge(), width = 0.7) +
  facet_grid(Satellite~., scales = &#39;free_y&#39;, space = &#39;free_y&#39;, switch = &#39;y&#39;) +
  scale_fill_manual(NULL, values = c(&#39;#4473c5&#39;, &#39;#ec7f2d&#39;)) +
  theme_minimal(base_size = 16) +
  theme(text = element_text(family = &#39;serif&#39;),
        strip.placement = &#39;outside&#39;,
        strip.background = element_rect(color = &#39;gray&#39;, fill = NA),
        strip.text = element_text(size = 10, face = 2),
        panel.grid.major.y = element_blank(),
        panel.spacing.y = unit(0, &#39;mm&#39;),
        legend.position = &#39;bottom&#39;)

如何在R中创建一个带有次要分组Y轴的条形图?

答案2

得分: 1

你可以转换为长格式,制作一个条形图并翻转它,即:

library(tidyverse)

df %>%
  pivot_longer(
    cols = c(`In.situ`, `Remote.sensing`),
    names_to = "Type",
    values_to = "Value") %>%
  ggplot(aes(x = interaction(Satellite, Reference, sep = ", "), y = Value, fill = Type)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  theme_minimal() +
  labs(x = "Satellite, Reference", y = "Value", fill = "Type")

这会生成如下图所示的结果:

如何在R中创建一个带有次要分组Y轴的条形图?


**数据**

```R
structure(list(Satellite = c("Landsat 8 OLI", "Landsat 8 OLI", 
"Landsat 5 TM", "MODIS", "MODIS", "MODIS", "Sentinel2A MSI"), 
    Reference = c("Zhu et al., 2022", "Huang et al., 2021", "Huang et al., 2021", 
    "Wu et al., 2022", "Yu et al., 2016", "Xu et al., 2010", 
    "Huang et al., 2023"), In.situ = c(0.624, 2.15, 2.15, 0.778, 
    0.0895, 0.1305, 0.31), Remote.sensing = c(0.554325, 1.52, 
    2.3, 0.64, 0.10546, 0.11799, 0.41)), class = "data.frame", row.names = c(NA, 
-7L))
英文:

You can convert to long, do a bar chart and flip it, i.e.

library(tidyverse)

df %&gt;%
  pivot_longer(
    cols = c(`In.situ`, `Remote.sensing`),
    names_to = &quot;Type&quot;,
    values_to = &quot;Value&quot;) %&gt;%
  ggplot(aes(x = interaction(Satellite, Reference, sep = &quot;, &quot;), y = Value, fill = Type)) +
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot;) +
  coord_flip() +
  theme_minimal() +
  labs(x = &quot;Satellite, Reference&quot;, y = &quot;Value&quot;, fill = &quot;Type&quot;)

which yields,

如何在R中创建一个带有次要分组Y轴的条形图?

DATA

structure(list(Satellite = c(&quot;Landsat 8 OLI&quot;, &quot;Landsat 8 OLI&quot;, 
&quot;Landsat 5 TM&quot;, &quot;MODIS&quot;, &quot;MODIS&quot;, &quot;MODIS&quot;, &quot;Sentinel2A MSI&quot;), 
    Reference = c(&quot;Zhu et al., 2022&quot;, &quot;Huang et al., 2021&quot;, &quot;Huang et al., 2021&quot;, 
    &quot;Wu et al., 2022&quot;, &quot;Yu et al., 2016&quot;, &quot;Xu et al., 2010&quot;, 
    &quot;Huang et al., 2023&quot;), In.situ = c(0.624, 2.15, 2.15, 0.778, 
    0.0895, 0.1305, 0.31), Remote.sensing = c(0.554325, 1.52, 
    2.3, 0.64, 0.10546, 0.11799, 0.41)), class = &quot;data.frame&quot;, row.names = c(NA, 
-7L))

huangapple
  • 本文由 发表于 2023年7月27日 22:08:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780584.html
匿名

发表评论

匿名网友

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

确定