英文:
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")
<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't need to flip co-ordinates - you can simply put the values on the x axis:
``` r
library(tidyverse)
d1 %>%
mutate(Satellite = factor(Satellite, rev(unique(Satellite))),
Reference = factor(Reference, unique(Reference))) %>%
pivot_longer(`In-situ`:`Remote sensing`) %>%
mutate(name = factor(name, unique(name))) %>%
ggplot(aes(value, Reference, fill = name)) +
geom_col(position = position_dodge(), width = 0.7) +
facet_grid(Satellite~., scales = 'free_y', space = 'free_y', switch = 'y') +
scale_fill_manual(NULL, values = c('#4473c5', '#ec7f2d')) +
theme_minimal(base_size = 16) +
theme(text = element_text(family = 'serif'),
strip.placement = 'outside',
strip.background = element_rect(color = 'gray', fill = NA),
strip.text = element_text(size = 10, face = 2),
panel.grid.major.y = element_blank(),
panel.spacing.y = unit(0, 'mm'),
legend.position = 'bottom')
答案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
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 %>%
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")
which yields,
DATA
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论