【问题标题】:How to make a chart with multi histograms如何制作具有多个直方图的图表
【发布时间】:2016-01-16 10:53:29
【问题描述】:

在 R 中制作包含多个直方图的图表时有些混乱(尝试使用 ggplot2)。我需要的例子是在Excel图表中生成的(见链接):

初始数据集如下:

attribute   DEV_share   current_qty_share
1           0,04999641  0,115217086
2           0,050001729 0,076647464
3           0,04999641  0,074048054
4           0,050001729 0,071905297
5           0,049999069 0,067865674
6           0,049999069 0,059962063
7           0,049999069 0,054130954
8           0,049999069 0,052725868
9           0,049999069 0,047421666
10          0,049999069 0,036040466
11          0,049999069 0,033370802
12          0,049999069 0,029085289
13          0,049999069 0,027047913
14          0,04999641  0,034354363
15          0,050001729 0,036567374
16          0,049999069 0,039728818
17          0,049999069 0,042222847
18          0,049999069 0,036532247
19          0,049999069 0,02511592
20          0,050017684 0,040009836

对于每个属性 (#1,2,3...) 对应两个变量('DEV_share' - 蓝色和 'current_qty_share' - 绿色、黄色、红色。

两条虚线表示截止值:第一个截止值对应第 17 个属性,第二个截止值对应第 10 个属性。 截止点是区分“current_qty_share”变量的每个属性的颜色的地标:17-20 - 绿色,10-16 - 黄色,1-9 - 红色。 'DEV_share' 的所有属性的蓝色都是相同的。

X 轴包含属性值,Y 轴包含 'DEV_share' 和 'current_qty_share' 的值。

如果您能提供一个提示,例如如何在 Excel 模板中制作图表(请参阅链接),将不胜感激。

【问题讨论】:

  • 这个docs.ggplot2.org/dev/geom_bar.html 应该可以帮助你开始;顺便说一句,它不是直方图,而是您拥有的条形图
  • @MLavoie:感谢您这么快的回复。我查看了链接,想知道如何将我的份额值放置在 Y 轴上(在所有示例中,仅显示与直方图中频率等效的“计数”)。

标签: r ggplot2 histogram


【解决方案1】:

这里有一些东西可以帮助您入门。首先,我必须用“。”替换你所有的“,”。在您的数据框中。如果要显示两列,则需要将数据框从宽转换为长。我还添加了一种为条形图着色的方法,您可以随意更改。

library(ggplot2)
library(reshape2)
data_wide <- read.table(text="
attribute   DEV_share   current_qty_share
1           0.04999641  0.115217086
2           0.050001729 0.076647464
3           0.04999641  0.074048054
4           0.050001729 0.071905297
5           0.049999069 0.067865674
6           0.049999069 0.059962063
7           0.049999069 0.054130954
8           0.049999069 0.052725868
9           0.049999069 0.047421666
10          0.049999069 0.036040466
11          0.049999069 0.033370802
12          0.049999069 0.029085289
13          0.049999069 0.027047913
14          0.04999641  0.034354363
15          0.050001729 0.036567374
16          0.049999069 0.039728818
17          0.049999069 0.042222847
18          0.049999069 0.036532247
19          0.049999069 0.02511592
20          0.050017684 0.040009836",
  header = TRUE)

data_long <- melt(data_wide, id.vars=c("attribute"))
data_long$Color <- 
  with(data_long,
       ifelse(variable == "current_qty_share", "darkblue", 
         ifelse(variable == "DEV_share" & attribute >=1 & attribute <8, 
                "darkred", 
                ifelse(variable == "DEV_share" & attribute >=8 & attribute <=17, 
                       "green",      
                       "yellow")))

ggplot(data=data_long, aes(x=as.factor(attribute), y=value, group=variable, fill=Color)) + 
geom_bar(stat='identity', position='dodge') + 
theme_bw() + 
geom_vline(xintercept=10, linetype = "longdash") + 
geom_vline(xintercept=17, linetype = "longdash") + 
xlab("attribute") + 
ylab("value") + 
scale_fill_manual(values=c("darkblue", "darkred", "green", "yellow"))

【讨论】:

  • @MLavoie:非常感谢。你的评论真的很棒! :-) 这出乎我的意料 :-)
  • @Axeman:非常感谢您的评论。好吧,有时我这样做了,但是“,”不是用“。”代替的。所以我更喜欢通过 Stringr 包显式地进行替换。
  • @Dimon,如果这是你想要的,别忘了接受答案!
猜你喜欢
  • 2021-08-17
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 2016-10-05
  • 2014-12-24
  • 2020-07-17
  • 2022-01-10
相关资源
最近更新 更多