在R中沿多个轴绘制列。

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

plot columns along multiple axes in R

问题

我尝试复制下面图片中的图表:

在R中沿多个轴绘制列。

所有数据都在这个文件中:https://drive.google.com/file/d/1PbxTrX2NekN4i-2REInKcROdVnxX1xJr/view?usp=sharing

数据看起来是这样的:

在R中沿多个轴绘制列。

我没有走得太远。我知道我只需要使用 geom_col(),但如何获得每个月每十年一个图,都像图片中那样排列呢?我找不到相似的示例。

ggplot(
  data=sealeveldecade,aes(x=month)) + #
  geom_col(show.legend = FALSE) +
  geom_col(aes(y = mediansealevelmonth))+
  geom_col(aes(y = minsealevelmonth))+
  labs(x = "月份", y = "由于RH2000的海平面(厘米)",title = "1960年-2009年每月的海平面中位数和最大值", subtitle = "每天每24小时的测量数据")
英文:

I am trying to replicate the plot as in the image below:

在R中沿多个轴绘制列。

All data is in this file: https://drive.google.com/file/d/1PbxTrX2NekN4i-2REInKcROdVnxX1xJr/view?usp=sharing

data looks like this:

在R中沿多个轴绘制列。

I didn't get far at all. I know i will just geom_col(), but how do i get one plot per month per decade, all arranged like in the image? I can't find an example that looks alike neither.

ggplot(
  data=sealeveldecade,aes(x=month)) + #
  geom_col(show.legend = FALSE) +
  geom_col(aes(y = mediansealevelmonth))+
  geom_col(aes(y = minsealevelmonth))+
  labs(x = "Month", y = "Sea level (cm) due to RH2000",title = "Median and maximum sea level per month during 19602009", subtitle = "Based on measurements every 24 hour per day")

答案1

得分: 1

您可以使用您的decades变量创建分面:

已编辑以按月份顺序排列

library(dplyr)
library(ggplot2)

df %>%
  mutate(month = factor(month, levels = month.abb)) %>%
  arrange(month) %>%
  ggplot(aes(month)) +
  geom_col(aes(y = mediansealevelmonth), fill = 'red') +
  geom_col(aes(y = minsealevelmonth), width = 0.6) +
  facet_wrap(~decades, ncol = 1) +
  labs(x = "月份", y = "由于RH2000而导致的海平面(厘米)", title = "1960年至2009年每月的中位数和最大海平面",
       subtitle = "基于每天每24小时的测量")

在R中沿多个轴绘制列。

创建于2023年3月31日,使用reprex v2.0.2

英文:

You can create facets with your decades variables:

Edited to arrange months chronologically

library(dplyr)

library(ggplot2)


df %>%
  mutate(month = factor(month, levels = month.abb)) %>%
  arrange(month) %>%
  ggplot(aes(month)) +
  geom_col(aes(y = mediansealevelmonth), fill = 'red') +
  geom_col(aes(y = minsealevelmonth), width = 0.6) +
  facet_wrap(~decades, ncol = 1) +
  labs(x = "Month", y = "Sea level (cm) due to RH2000",title = "Median and maximum sea level per month during 19602009", subtitle = "Based on measurements every 24 hour per day")

在R中沿多个轴绘制列。<!-- -->

<sup>Created on 2023-03-31 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月31日 20:57:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898810.html
匿名

发表评论

匿名网友

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

确定