这个问题已经有答案了:
Create climate diagram in R with temperature and precipitation(2个答案)
5天前关闭。
这篇文章是编辑并提交审查5天前.
我试图调整这个简单的code来可视化2001-2010年的月降水量,温度和相对湿度数据。代表数据温度和湿度的线在图中不可见。它们似乎被绘制出来,但它们并没有像预期的那样显示出来。我想faceted
绘制year
。
以下是可重复的示例:
df <- structure(list(YEAR = c(2000, 2001, 2001, 2001, 2001, 2001, 2001,
2001, 2001, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002, 2002,
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 2003,
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2004, 2004, 2004,
2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2005, 2005,
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2006,
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006,
2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007,
2007, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008,
2008, 2008, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
2009, 2009, 2009, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010,
2010, 2010, 2010, 2010), month = c("December", "April", "August",
"December", "February", "January", "July", "June", "March", "May",
"November", "October", "September", "April", "August", "December",
"February", "January", "July", "June", "March", "May", "November",
"October", "September", "April", "August", "December", "February",
"January", "July", "June", "March", "May", "November", "October",
"September", "April", "August", "December", "February", "January",
"July", "June", "March", "May", "November", "October", "September",
"April", "August", "December", "February", "January", "July",
"June", "March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September"), total_rain = c(0,
31.62, 82.73, 68.57, 53.39, 58.26, 167.69, 99.49, 114.37, 128.83,
20.5, 21.92, 61.9, 39.91, 72.38, 109.6, 28.85, 82.93, 104.89,
59.59, 123.88, 77.08, 119.68, 127.47, 122.63, 172.74, 163.4,
84.12, 142.47, 50.37, 174.77, 165.84, 129.24, 126.63, 117.04,
49.48, 106.39, 93.99, 97.12, 78.74, 75.4, 45.45, 101.28, 146.53,
58.48, 96.85, 104.12, 64.47, 259.29, 91.63, 85.85, 81.31, 57.48,
73.13, 161.34, 99.85, 100.79, 50.32, 87.82, 113.9, 14.94, 97.52,
120.2, 72.52, 35.83, 88.76, 104.12, 175.53, 34.18, 60.07, 107.2,
104.32, 114.34, 83.92, 46.56, 91.12, 56.16, 69.57, 97.58, 107.81,
99.23, 38.87, 23.25, 134.55, 59.15, 108.3, 138.68, 95.95, 72.98,
40, 105.43, 76.5, 92.1, 67.17, 59.6, 43.73, 102.05, 86.69, 114.03,
169.59, 53.03, 90.54, 107.6, 146.18, 96.85, 191.26, 119.76, 87.34,
106.33, 55.26, 120.61, 62.02, 80.53, 130.04, 121.64, 84.1, 111.81,
117.08, 72.93, 65.58, 100.37), mean_temp = c(-7.08, 13.5, 23.43,
5.19, 4.63, 0.46, 22.68, 22.03, 4.77, 18.07, 10.51, 12.2, 18.06,
14.25, 24.37, 0.43, 3.02, 2.76, 25.45, 23.46, 7.52, 17.34, 6.09,
13.87, 20.98, 12.14, 23.11, 0.79, 0.97, -1.22, 22.44, 20.08,
8.11, 16.61, 9.74, 12.2, 17.98, 12.41, 22.13, 2.44, 1.42, -0.31,
23.53, 21.56, 8.11, 19.8, 8.83, 14.33, 19.13, 12.22, 24.6, -0.1,
3.02, 2.77, 24.54, 21.64, 5.24, 15.51, 8.66, 14.48, 21.52, 14.33,
24.54, 4.86, 1.98, 4.42, 24.08, 21.83, 7.2, 16.55, 8.22, 11.57,
17.95, 11.32, 26.86, 4.75, -0.2, 2.91, 23.3, 22.59, 9.64, 17.91,
6.53, 16.35, 21.38, 12.54, 22.94, 3.57, 3.91, 0.65, 24.01, 23.91,
7.33, 16.35, 5.31, 12.2, 19.46, 12.5, 23.03, 0.5, 2.32, 0.06,
21.68, 21.69, 6.67, 17.3, 8.55, 12.48, 19.08, 13.99, 24.52, -2.72,
-2.63, -1.5, 25.45, 23.99, 7.02, 18.73, 7.27, 14.01, 21.1), mean_rh = c(86.4,
72.96, 72.19, 73.56, 75.66, 75.61, 70.86, 70.88, 73.23, 69.4,
64.71, 60.92, 66.56, 71.78, 63.03, 86.24, 70.36, 78.04, 64.6,
62.9, 76.59, 67.54, 82.01, 82.58, 70.91, 84.24, 83.63, 81.27,
86.27, 80.1, 84.13, 81.88, 84.91, 84.9, 77.39, 78.23, 79.94,
75.2, 72.94, 79.33, 80.15, 80.06, 73.7, 79, 78.94, 78.68, 81.42,
84.18, 79.35, 78.21, 70.42, 82.36, 82.22, 84.23, 77.34, 76.14,
80.7, 75.11, 69.03, 72.83, 61.95, 73.48, 68.09, 78.53, 76.51,
82.82, 71.16, 68.9, 73.7, 69.8, 76.67, 73.81, 76.98, 73.18, 59.32,
81.1, 75.2, 80.22, 65.39, 65.82, 78.02, 70.13, 73.05, 69.43,
61.26, 77.79, 66.35, 82.25, 78.95, 77.56, 64.5, 62.35, 77.1,
73.89, 72.58, 70.21, 75.86, 76.53, 75.8, 85.05, 78.31, 79.92,
76.9, 81.67, 82.01, 83.01, 82.54, 78.1, 76.27, 76.19, 73.32,
78.53, 90.08, 82.42, 65.86, 76.54, 81.49, 81.75, 74.36, 65.27,
63.09)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -121L), groups = structure(list(YEAR = c(2000,
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010),
.rows = structure(list(1L, 2:13, 14:25, 26:37, 38:49, 50:61,
62:73, 74:85, 86:97, 98:109, 110:121), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -11L), .drop = TRUE))
字符串
使用的代码
df$month <- factor(df$month, levels = month.name)
ggplot(df, aes(x = month)) +
geom_bar(aes(y = total_rain/4, fill = "Precipitation"), stat = "identity") +
geom_line(aes(y = mean_temp, color = "Temperature")) +
geom_point(aes(y = mean_temp)) +
geom_line(aes(y = mean_rh, color = "Relative Humidity"), linetype = "dashed") +
geom_point(aes(y = mean_rh), shape = 17) +
scale_x_discrete(limits = month.name) +
scale_y_continuous(
name = "Temperature (°C)",
sec.axis = sec_axis(~.*4, name = "Precipitation (mm)")
) +
scale_color_manual(
name = "",
values = c("Temperature" = "black", "Relative Humidity" = "blue")
) +
scale_fill_manual(name = "", values = "orange") +
theme(
text = element_text(size = 15),
axis.text.x = element_text(angle = 90, hjust = 1),
legend.position = "bottom"
)
型
这里是输出
x1c 0d1x的数据
我想要的输出在这里,除了我想添加mean_rh
。
的
1条答案
按热度按时间xfb7svmp1#
EDIT - OP澄清了他们想要一个分面图。为了使线连接起来,即使x轴是一个因子(ggplot的默认值是将其作为观察值分开的标志),我们可以添加
group = 1
(或任何其他常量)来告诉ggplot 2将各个月视为一个单一的系列。字符串
x1c 0d1x的数据
Origin回答:
型
的