双 x 轴直方图(图的顶部和底部)显示两个不同城市的温度分布
Dual x-axis histogram (top and bottom of plot) showing distributions of temperature for two different cities
我正在尝试创建一个具有 2 个 x 轴的图。我想要在这幅图中有两个直方图,一个在城市 A 的顶部(上下颠倒),一个在城市 B 的底部。我希望 x 轴以度为单位显示温度。
我尝试了几种不同的入门方法,但都失败了。本人能力有限,主要是修改网上复制的代码。
city<-c(A, B, A, B, A, B)
temp<-c(20, 25, 30, 35, 30, 40)
data<-data.frame(city, temp)
g.top <- ggplot(data, aes(x = temp)) +
theme_bw() +
theme(plot.margin = unit(c(1,5,-30,6),units="points"),
axis.title.y = element_text(vjust =0.25)) +
geom_histogram()
g.bottom <- ggplot(data, aes(x = temp)) +
theme_bw() +
theme(plot.margin = unit(c(0,5,1,1),units="points")) +
geom_histogram()
grid.arrange(g.top,g.bottom, heights = c(1/5, 4/5))
p <- ggplot(data, aes(x = temp, y=city)) + geom_histogram(aes(y=variable))
当我 运行 第一个代码块时,它给了我两个单独的堆叠直方图并说:
stat_bin() using bins = 30. Pick better value with binwidth.
但无论如何它都没有给我第二个 x 轴。
第二个代码给我一个错误,说我不能以 y 审美使用 statbin,这对我来说很有意义,但我现在没有想法。
我真的很沮丧,非常非常感谢任何建议
这不一定是到达那里的最简单方法,但可以控制整个过程。我将其作为数据重塑练习来处理。首先我得到每个城市的每个温度的计数,然后计算出这些计数之间有多少 space,然后绘制一个正常堆叠 geom_col
.
# Get the counts
library(tidyverse)
my_data_transform <- data %>%
count(temp, city, name = "count")
# Take that, and append after it rows that measure the "space between" each city's counts at each temperature.
my_data_transform2 <- bind_rows(
my_data_transform,
my_data_transform %>%
count(temp, wt = count, name = "count") %>%
mutate(city = " ",
count = 15 - count) # Adjust to taste
) %>%
# For plotting, I want the "space between" rows to be plotted in between the two cities.
# This is accomplished by making the city an ordered factor and putting the blanks 2nd.
mutate(city = city %>% fct_relevel(" ", after = 1))
# Plot as geom_col, specifying clear bars for the middle ones.
ggplot(my_data_transform2, aes(temp, count, fill = city)) +
geom_col() +
scale_fill_manual(values = c("black", NA, "black")) +
scale_y_continuous(labels = NULL, name = "") +
theme_minimal()
分别绘制直方图,每个城市一个:
- 顶部 y-axis 翻转,x-axis 标签在顶部。
- 下图一如往常
- 设置 x-axis 对 both 图设置相同的限制,因此当我们使用 cowplot:plotgrid[= 垂直对齐 x-axis 时27=] x-axis 将对齐。
参见下面的示例:
library(ggplot2)
library(cowplot)
#bigger example data
set.seed(1); data <- data.frame(city = sample(LETTERS[1:2], 100, replace = TRUE),
temp = sample(1:50, 100, replace = TRUE))
g.top <- ggplot(data[ data$city == "A", ], aes(x = temp)) +
geom_histogram() +
scale_x_continuous(limits = c(10, 50), position = "top") +
scale_y_reverse() +
theme_minimal()
g.bottom <- ggplot(data[ data$city == "B", ], aes(x = temp)) +
geom_histogram() +
scale_x_continuous(limits = c(10, 50)) +
theme_minimal()
plot_grid(g.top, g.bottom,
labels = c("City A", "City B"), nrow = 2, align = 'hv')
我正在尝试创建一个具有 2 个 x 轴的图。我想要在这幅图中有两个直方图,一个在城市 A 的顶部(上下颠倒),一个在城市 B 的底部。我希望 x 轴以度为单位显示温度。
我尝试了几种不同的入门方法,但都失败了。本人能力有限,主要是修改网上复制的代码。
city<-c(A, B, A, B, A, B)
temp<-c(20, 25, 30, 35, 30, 40)
data<-data.frame(city, temp)
g.top <- ggplot(data, aes(x = temp)) +
theme_bw() +
theme(plot.margin = unit(c(1,5,-30,6),units="points"),
axis.title.y = element_text(vjust =0.25)) +
geom_histogram()
g.bottom <- ggplot(data, aes(x = temp)) +
theme_bw() +
theme(plot.margin = unit(c(0,5,1,1),units="points")) +
geom_histogram()
grid.arrange(g.top,g.bottom, heights = c(1/5, 4/5))
p <- ggplot(data, aes(x = temp, y=city)) + geom_histogram(aes(y=variable))
当我 运行 第一个代码块时,它给了我两个单独的堆叠直方图并说:
stat_bin() using bins = 30. Pick better value with binwidth.
但无论如何它都没有给我第二个 x 轴。
第二个代码给我一个错误,说我不能以 y 审美使用 statbin,这对我来说很有意义,但我现在没有想法。
我真的很沮丧,非常非常感谢任何建议
这不一定是到达那里的最简单方法,但可以控制整个过程。我将其作为数据重塑练习来处理。首先我得到每个城市的每个温度的计数,然后计算出这些计数之间有多少 space,然后绘制一个正常堆叠 geom_col
.
# Get the counts
library(tidyverse)
my_data_transform <- data %>%
count(temp, city, name = "count")
# Take that, and append after it rows that measure the "space between" each city's counts at each temperature.
my_data_transform2 <- bind_rows(
my_data_transform,
my_data_transform %>%
count(temp, wt = count, name = "count") %>%
mutate(city = " ",
count = 15 - count) # Adjust to taste
) %>%
# For plotting, I want the "space between" rows to be plotted in between the two cities.
# This is accomplished by making the city an ordered factor and putting the blanks 2nd.
mutate(city = city %>% fct_relevel(" ", after = 1))
# Plot as geom_col, specifying clear bars for the middle ones.
ggplot(my_data_transform2, aes(temp, count, fill = city)) +
geom_col() +
scale_fill_manual(values = c("black", NA, "black")) +
scale_y_continuous(labels = NULL, name = "") +
theme_minimal()
分别绘制直方图,每个城市一个:
- 顶部 y-axis 翻转,x-axis 标签在顶部。
- 下图一如往常
- 设置 x-axis 对 both 图设置相同的限制,因此当我们使用 cowplot:plotgrid[= 垂直对齐 x-axis 时27=] x-axis 将对齐。
参见下面的示例:
library(ggplot2)
library(cowplot)
#bigger example data
set.seed(1); data <- data.frame(city = sample(LETTERS[1:2], 100, replace = TRUE),
temp = sample(1:50, 100, replace = TRUE))
g.top <- ggplot(data[ data$city == "A", ], aes(x = temp)) +
geom_histogram() +
scale_x_continuous(limits = c(10, 50), position = "top") +
scale_y_reverse() +
theme_minimal()
g.bottom <- ggplot(data[ data$city == "B", ], aes(x = temp)) +
geom_histogram() +
scale_x_continuous(limits = c(10, 50)) +
theme_minimal()
plot_grid(g.top, g.bottom,
labels = c("City A", "City B"), nrow = 2, align = 'hv')