如何使用ggplot2将所有箱子的总和添加到图表中?

huangapple go评论71阅读模式
英文:

How to add a sum of all bins to the graph using ggplot2?

问题

这是我试图复现的图表 -
如何使用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) 
  

它产生了这种类型的直方图 -

如何使用ggplot2将所有箱子的总和添加到图表中?

我如何添加那个总值的灰色直方图?

英文:

Here's the graph I'm trying to reproduce -
如何使用ggplot2将所有箱子的总和添加到图表中?

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 -

如何使用ggplot2将所有箱子的总和添加到图表中?

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]')

如何使用ggplot2将所有箱子的总和添加到图表中?

英文:

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 &lt;- &quot;https://raw.githubusercontent.com/agricolamz/r_on_line_course_data/&quot; %&gt;%
  paste0(&quot;master/data.japanese.vocal.tract.length.csv&quot;) %&gt;%
  read_csv() %&gt;%
  subset(select = -c(age, body.height, mean)) %&gt;% 
  rename_with(~sub(&#39;\\..*$&#39;, &#39;&#39;, .x)) %&gt;%
  gather(key = vowel, value = value, a:u) 

ggplot(df, aes(value)) + 
  geom_histogram(data = df[-2], bins = nclass.FD(df$value), fill = &#39;gray&#39;) +
  geom_histogram(aes(fill = vowel), bins = nclass.FD(df$value)) + 
  facet_wrap(~ vowel) +
  theme(legend.position = &#39;none&#39;) +
  labs(x = &#39;length of the vocal tract&#39;,
       caption = &#39;data from [Hatano et. al 2012]&#39;)

如何使用ggplot2将所有箱子的总和添加到图表中?

huangapple
  • 本文由 发表于 2023年5月29日 18:14:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定