gpt4 book ai didi

r - 在密度图下方添加箱线图

转载 作者:行者123 更新时间:2023-12-04 15:29:19 57 4
gpt4 key购买 nike

我是 ggplot 的新手,我正在尝试创建此图:

enter image description here

但实际上,我只是被困在这里:

enter image description here

这是我的代码:

ggplot(diamonds)  + 
aes(x = carat, group = cut) +
geom_line(stat = "density", size = 1) +
theme_grey() +
facet_wrap(~cut, nrow = 5, strip.position = "right") +
geom_boxplot(aes())

有人知道我接下来可以做什么吗?

最佳答案

编辑:从 ggplot2 3.3.0 开始,这可以在 ggplot2 中完成,无需任何扩展包。
下包的news , 在新功能下:

All geoms and stats that had a direction (i.e. where the x and y axeshad different interpretation), can now freely choose their direction,instead of relying on coord_flip(). The direction is deduced fromthe aesthetic mapping, but can also be specified directly with the neworientation argument (@thomasp85, #3506).


以下内容现在可以直接工作(将原始答案中对 geom_boxploth/ stat_boxploth 的所有引用替换为 geom_boxplot/ stat_boxplot :
library(ggplot2)

ggplot(diamonds, aes(x = carat, y = -0.5)) +

# horizontal boxplots & density plots
geom_boxplot(aes(fill = cut)) +
geom_density(aes(x = carat), inherit.aes = FALSE) +

# vertical lines at Q1 / Q2 / Q3
stat_boxplot(geom = "vline", aes(xintercept = ..xlower..)) +
stat_boxplot(geom = "vline", aes(xintercept = ..xmiddle..)) +
stat_boxplot(geom = "vline", aes(xintercept = ..xupper..)) +

facet_grid(cut ~ .) +
scale_fill_discrete()

原答案
这可以通过水平箱线图轻松完成 geom_boxploth()/ stat_boxploth() ,在 ggstance 中找到包裹:
library(ggstance)

ggplot(diamonds, aes(x = carat, y = -0.5)) +

# horizontal box plot
geom_boxploth(aes(fill = cut)) +

# normal density plot
geom_density(aes(x = carat), inherit.aes = FALSE) +

# vertical lines at Q1 / Q2 / Q3
stat_boxploth(geom = "vline", aes(xintercept = ..xlower..)) +
stat_boxploth(geom = "vline", aes(xintercept = ..xmiddle..)) +
stat_boxploth(geom = "vline", aes(xintercept = ..xupper..)) +

facet_grid(cut ~ .) +

# reproduce original chart's color scale (o/w ordered factors will result
# in viridis scale by default, using the current version of ggplot2)
scale_fill_discrete()
plot
如果你因为某种原因被限制在 ggplot2 包中,它仍然可以完成,但它会不那么直接,因为 geom_boxplot()geom_density()往不同的方向走。
备选方案 1 : 计​​算箱线图的坐标,并在将结果传递给 ggplot() 之前手动翻转它们.以正常方式添加密度层:
library(dplyr)
library(tidyr)

p.box <- ggplot(diamonds, aes(x = cut, y = carat)) + geom_boxplot()
p.box.data <- layer_data(p.box) %>%
select(x, ymin, lower, middle, upper, ymax, outliers) %>%
mutate(cut = factor(x, labels = levels(diamonds$cut), ordered = TRUE)) %>%
select(-x)

ggplot(p.box.data) +

# manually plot flipped boxplot
geom_segment(aes(x = ymin, xend = ymax, y = -0.5, yend = -0.5)) +
geom_rect(aes(xmin = lower, xmax = upper, ymin = -0.75, ymax = -0.25, fill = cut),
color = "black") +
geom_point(data = . %>% unnest(outliers),
aes(x = outliers, y = -0.5)) +

# vertical lines at Q1 / Q2 / Q3
geom_vline(data = . %>% select(cut, lower, middle, upper) %>% gather(key, value, -cut),
aes(xintercept = value)) +

# density plot
geom_density(data = diamonds, aes(x = carat)) +

facet_grid(cut ~ .) +
labs(x = "carat") +
scale_fill_discrete()
备选方案 2 : 计​​算密度图的坐标,并在将结果传递给 ggplot() 之前手动翻转它们.以正常方式添加箱线图图层。翻转整个图表:
p.density <- ggplot(diamonds, aes(x = carat, group = cut)) + geom_density()    
p.density.data <- layer_data(p.density) %>%
select(x, y, group) %>%
mutate(cut = factor(group, labels = levels(diamonds$cut), ordered = TRUE)) %>%
select(-group)
p.density.data <- p.density.data %>%
rbind(p.density.data %>%
group_by(cut) %>%
filter(x == min(x)) %>%
mutate(y = 0) %>%
ungroup())

ggplot(diamonds, aes(x = -0.5, y = carat)) +

# manually flipped density plot
geom_polygon(data = p.density.data, aes(x = y, y = x),
fill = NA, color = "black") +

# box plot
geom_boxplot(aes(fill = cut, group = cut)) +

# vertical lines at Q1 / Q2 / Q3
stat_boxplot(geom = "hline", aes(yintercept = ..lower..)) +
stat_boxplot(geom = "hline", aes(yintercept = ..middle..)) +
stat_boxplot(geom = "hline", aes(yintercept = ..upper..)) +

facet_grid(cut ~ .) +
scale_fill_discrete() +
coord_flip()

关于r - 在密度图下方添加箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54393927/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com