r - 每个月并排显示四个变量的条形图(1 月至 12 月)
问题描述
我是 R 的初学者,想将我从 1 月到 12 月的两年降雨量和太阳辐射数据并排绘制条形图(附加数据)。
要绘制的数据:
我正在尝试绘制第一行(1 月),但出现此错误
-0.01 * 高度中的错误:二元运算符的非数字参数
如何处理?以及使用哪个脚本来绘制我的数据?
问候,
解决方案
这是一个例子
library(tidyverse)
set.seed(123456)
df <- data.frame(Month = month.abb,
R_2014 = runif(n = 12, min = 0, max = 195),
R_2015 = runif(n = 12, min = 0, max = 295),
S_2014 = runif(n = 12, min = 3, max = 10),
S_2015 = runif(n = 12, min = 4, max = 10))
df
#> Month R_2014 R_2015 S_2014 S_2015
#> 1 Jan 155.56794 267.06645 6.344445 9.714178
#> 2 Feb 146.94519 259.85035 7.903533 9.229704
#> 3 Mar 76.29486 293.18178 9.159223 8.272923
#> 4 Apr 66.60356 264.30712 9.144556 7.632427
#> 5 May 70.45235 259.19979 8.977157 5.352593
#> 6 Jun 38.67722 58.29370 4.161913 8.437571
#> 7 Jul 104.29730 98.82311 6.660781 9.373255
#> 8 Aug 18.82262 229.27586 9.083897 5.766779
#> 9 Sep 192.63015 47.08010 4.618097 7.092115
#> 10 Oct 32.67605 23.79035 3.833566 6.607897
#> 11 Nov 155.60788 39.13185 8.767659 7.450991
#> 12 Dec 115.78983 50.71209 3.561939 8.445736
# convert from wide to long format
# separate columns to get variable and year
df_long <- df %>%
gather(key, value, -Month) %>%
separate(key, into = c("variable", "Year"), "_") %>%
mutate(Month = factor(Month, levels = month.abb))
head(df_long)
#> Month variable Year value
#> 1 Jan R 2014 155.56794
#> 2 Feb R 2014 146.94519
#> 3 Mar R 2014 76.29486
#> 4 Apr R 2014 66.60356
#> 5 May R 2014 70.45235
#> 6 Jun R 2014 38.67722
# facet by year
plt1 <- ggplot(df_long, aes(x = Month, y = value, fill = variable)) +
geom_col(position = "dodge") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
facet_wrap(~ Year)
plt1
# facet by variable
plt2 <- ggplot(df_long, aes(x = Month, y = value, fill = Year)) +
geom_col(position = "dodge") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
facet_wrap( ~ variable, scales = "free_y")
plt2
由reprex 包(v0.2.0)于 2018 年 6 月 1 日创建。
推荐阅读
- database - 如何摆脱 Delphi 中的“ADOQuery Field name not found”错误?(使用 MS Access 和 ADOQuery)
- python - 优化多输出梯度提升的学习率和估计器数量
- pytorch - PyTorch:应用不同形状的遮罩
- angular - Angular 请求中的冲突并从服务器获取响应
- python-3.x - 尝试将差异散列与 ImageHash 库一起使用的 Numpy 错误
- arrays - 将复杂的 JSON 解析为 List Flutter
- typescript - 在具有额外维度的 ChartJS 上对多行上的 y 轴标签进行分组
- python - 在python中,如何在这样的单行中一次输入( L , R ) - L=10 T=20
- node.js - 'req.user' 显示在获取请求但不发布请求
- python - 如何停止递归并返回答案?