通过 geom_tile ggplot R 的热图 - 正确组织每月因子的 y 轴水平
Heatmap via geom_tile ggplot R - Organize y axis levels of monthly factor correctly
我正在尝试制作日期热图。我把它转换成一个例子你可以copy/paste到R看看。
除了 x 轴上的月份没有按顺序排列外,第一次尝试绘图时工作正常。我尝试通过添加级别来订购它们。在下图中,顺序是正确的,但数据没有移动。 2009 年 2 月和 2009 年 8 月显示相同的数据。2009 年 8 月是正确的,但是当我尝试修复水平时,数据没有移动。如何才能让 X 轴按顺序标记并同时使数据正确?
library(tidyverse)
year_data <- c("2009", rep("2010",7), rep("2011",10),rep("2012",10))
month_data <- c("Aug", "Aug", "Feb", "Jan", "Jul", "May", "Nov", "Oct", "Aug",
"Dec", "Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct", "Sep",
"Apr", "Aug", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Oct", "Sep")
number_data <- c(3, 12, 6, 3, 15, 6, 9, 6, 30, 24, 3, 24, 12, 12, 6, 39, 33, 39,
33, 51, 45, 54, 42, 30, 36, 45, 15, 36)
reprex_data <- data.frame(year_data, month_data, number_data) %>%
as_tibble() %>%
rename("year" = 1,
"month" = 2,
"n" = 3) %>%
mutate(month = as.factor(month))
# This plot works, but y axis is out of order
reprex_data %>%
ggplot(aes(year, month)) +
geom_tile(aes(fill = n)) +
scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
labs(title = "Plot before attempting to fix levels")
# Attempt to reorganize them. While it works, the data that should be Aug, 2009 is plotted as Feb, 2009
levels(reprex_data$month) <- (month.abb)
reprex_data %>%
ggplot(aes(year, month)) +
geom_tile(aes(fill = n)) +
scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
labs(title = "Plot after attempting to fix levels")
你可以试试-
library(tidyverse)
reprex_data <- data.frame(year_data, month_data, number_data) %>%
as_tibble() %>%
rename("year" = 1,
"month" = 2,
"n" = 3) %>%
mutate(month = factor(month, month.abb))
然后使用剧情代码-
reprex_data %>%
ggplot(aes(year, month)) +
geom_tile(aes(fill = n)) +
scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
labs(title = "Fixed Plot")
我正在尝试制作日期热图。我把它转换成一个例子你可以copy/paste到R看看。
除了 x 轴上的月份没有按顺序排列外,第一次尝试绘图时工作正常。我尝试通过添加级别来订购它们。在下图中,顺序是正确的,但数据没有移动。 2009 年 2 月和 2009 年 8 月显示相同的数据。2009 年 8 月是正确的,但是当我尝试修复水平时,数据没有移动。如何才能让 X 轴按顺序标记并同时使数据正确?
library(tidyverse)
year_data <- c("2009", rep("2010",7), rep("2011",10),rep("2012",10))
month_data <- c("Aug", "Aug", "Feb", "Jan", "Jul", "May", "Nov", "Oct", "Aug",
"Dec", "Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct", "Sep",
"Apr", "Aug", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Oct", "Sep")
number_data <- c(3, 12, 6, 3, 15, 6, 9, 6, 30, 24, 3, 24, 12, 12, 6, 39, 33, 39,
33, 51, 45, 54, 42, 30, 36, 45, 15, 36)
reprex_data <- data.frame(year_data, month_data, number_data) %>%
as_tibble() %>%
rename("year" = 1,
"month" = 2,
"n" = 3) %>%
mutate(month = as.factor(month))
# This plot works, but y axis is out of order
reprex_data %>%
ggplot(aes(year, month)) +
geom_tile(aes(fill = n)) +
scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
labs(title = "Plot before attempting to fix levels")
# Attempt to reorganize them. While it works, the data that should be Aug, 2009 is plotted as Feb, 2009
levels(reprex_data$month) <- (month.abb)
reprex_data %>%
ggplot(aes(year, month)) +
geom_tile(aes(fill = n)) +
scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
labs(title = "Plot after attempting to fix levels")
你可以试试-
library(tidyverse)
reprex_data <- data.frame(year_data, month_data, number_data) %>%
as_tibble() %>%
rename("year" = 1,
"month" = 2,
"n" = 3) %>%
mutate(month = factor(month, month.abb))
然后使用剧情代码-
reprex_data %>%
ggplot(aes(year, month)) +
geom_tile(aes(fill = n)) +
scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) +
labs(title = "Fixed Plot")