英文:
How to add a sum of all bins to the graph using ggplot2?
问题
这是我试图复现的图表 -
正如您所见,每个直方图后面都有一个灰色直方图,似乎是所有直方图的总和。这是我做的:
df <- read_csv("https://raw.githubusercontent.com/agricolamz/r_on_line_course_data/master/data.japanese.vocal.tract.length.csv")
df %>%
subset(select = -c(age, body.height, mean)) ->
df.mod
colnames(df.mod)[which(names(df.mod) == "a.vtl")] <- "a"
colnames(df.mod)[which(names(df.mod) == "o.vtl")] <- "o"
colnames(df.mod)[which(names(df.mod) == "i.vtl")] <- "i"
colnames(df.mod)[which(names(df.mod) == "e.vtl")] <- "e"
colnames(df.mod)[which(names(df.mod) == "u.vtl")] <- "u"
df.mod %>%
gather(key = vowel, value = value, a:u) -> df.mod
df.mod %>%
ggplot(aes(value, fill = vowel)) +
facet_wrap(~ vowel) +
geom_histogram(bins = nclass.FD(df.mod$value), show.legend = FALSE)
它产生了这种类型的直方图 -
我如何添加那个总值的灰色直方图?
英文:
Here's the graph I'm trying to reproduce -
As you can see, each histogram has a grey histogram behind it, which appears to be a sum of all. Here's what I did:
df <- read_csv("https://raw.githubusercontent.com/agricolamz/r_on_line_course_data/master/data.japanese.vocal.tract.length.csv")
df %>%
subset(select = -c(age, body.height, mean)) ->
df.mod
colnames(df.mod)[which(names(df.mod) == "a.vtl")] <- "a"
colnames(df.mod)[which(names(df.mod) == "o.vtl")] <- "o"
colnames(df.mod)[which(names(df.mod) == "i.vtl")] <- "i"
colnames(df.mod)[which(names(df.mod) == "e.vtl")] <- "e"
colnames(df.mod)[which(names(df.mod) == "u.vtl")] <- "u"
df.mod %>%
gather(key = vowel, value = value, a:u) -> df.mod
df.mod %>%
ggplot(aes(value, fill = vowel)) +
facet_wrap(~ vowel) +
geom_histogram(bins = nclass.FD(df.mod$value), show.legend = FALSE)
It produces this kind of histogram -
How can I add that grey histogram of total values?
答案1
得分: 0
你可以先将所有数据的直方图添加到一个单独的图层中。请注意,您还可以使用 rename_with
大幅简化您的代码:
library(tidyverse)
df <- "https://raw.githubusercontent.com/agricolamz/r_on_line_course_data/" %>%
paste0("master/data.japanese.vocal.tract.length.csv") %>%
read_csv() %>%
subset(select = -c(age, body.height, mean)) %>%
rename_with(~sub('\\..*$', '', .x)) %>%
gather(key = vowel, value = value, a:u)
ggplot(df, aes(value)) +
geom_histogram(data = df[-2], bins = nclass.FD(df$value), fill = 'gray') +
geom_histogram(aes(fill = vowel), bins = nclass.FD(df$value)) +
facet_wrap(~ vowel) +
theme(legend.position = 'none') +
labs(x = 'length of the vocal tract',
caption = 'data from [Hatano et. al 2012]')
英文:
You can add the histogram of all the data in a separate layer first. Note you can also simplify your code substantially using rename_with
:
library(tidyverse)
df <- "https://raw.githubusercontent.com/agricolamz/r_on_line_course_data/" %>%
paste0("master/data.japanese.vocal.tract.length.csv") %>%
read_csv() %>%
subset(select = -c(age, body.height, mean)) %>%
rename_with(~sub('\\..*$', '', .x)) %>%
gather(key = vowel, value = value, a:u)
ggplot(df, aes(value)) +
geom_histogram(data = df[-2], bins = nclass.FD(df$value), fill = 'gray') +
geom_histogram(aes(fill = vowel), bins = nclass.FD(df$value)) +
facet_wrap(~ vowel) +
theme(legend.position = 'none') +
labs(x = 'length of the vocal tract',
caption = 'data from [Hatano et. al 2012]')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论