使用R中的multhist创建具有多个数据序列的直方图 [英] Creating a histogram with multiple data series using multhist in R

查看:478
本文介绍了使用R中的multhist创建具有多个数据序列的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一个图上创建一个包含多个数据序列的直方图。我能找到的最好的方法是 multhist()。我想要一个类似于 hist()的风格,而 ggplot()也可以用于执行此任务,图形样式不是我想要的。



以下是一些示例数据:

  df < - 结构(列表(年份= c(2011L,2011L,2011L,2011L,2011L,2011L,
2011L,2011L,2011L,2011L,2011L,2011L,2011L,2011L,2011L,2011L,
2011L,2011L,2011L 2011L,2011L,2011L,2011L,2011L,2012L,
2012L,2012L,2012L,2012L,2012L,2012L,2012L,2012L,2012L,2012L,
2012L,2012L,2012L, ,2012L,2012L,2012L,
2012L,2012L,2012L),count = c(187L,199L,560L,1000L,850L,
400L,534L,911L,390L,1008L,1173L,1222L, 810L,950L,752L,
1125L,468L,710L,290L,670L,855L,614L,1300L,950L,670L,
888L,490L,557L,741L,700L,954L,378L,512L, 780L,951L,398L,
1544L,903L,769L,1399L,1021L,1235L,1009L,1222L,255L)),.Names = c(year,
count),class = data.frame,row.names = c(NA,-45L))

这里是我已经使用过的代码:

  require(plotrix)
d2011< -df $ count [df $ year ==2011]
d2012< -df $ count [df $ year ==2012]
year <-list(d2011,d2012)
mh< - multhist(year,xlab =Count,ylab =Frequency,main =,cex.axis = 1,col = c(暗灰色,浅灰色),breaks = seq(0,1600 (=200))
box(bty =l,col =black)
legend.text< -c(2011,2012)
legend (1),legend = legend.text,col = c(深灰色,浅灰色),pch = 15,bty =n,cex = 0.8)

这为我提供了一个'barplot style'多直方图,但是我遇到了更改两个图形参数的问题。


  1. 我想让情节看起来更像直方图,更不像一个barplot,所以首先我想删除(或减少)列之间的空间。我尝试过使用 space = NULL ,但这个命令似乎不适用于multhist。

  2. 想要更改x轴,以便轴上的刻度线出现在曲线图上的小节之间,轴文本与刻度线对齐,而不是位于小节点中点。我已经尝试过使用 axis(side = 1,...),但是由于multhist使用列表对象来创建绘图,所以这些命令似乎不起作用。

任何建议将不胜感激。对其他有用的可以绘制多个数据集的直方图的图形包的建议也会受到欢迎。

解决方案阅读 barplot 的文档,了解如何指定零空间:

  multhist(year,xlab =Count,ylab =Frequency,main =,
(x,y)= 1,col = c(暗灰色,浅灰色),
breaks = seq(0,1600,by = 200),
space = c(0,0) ,旁边= TRUE)


以下是ggplot2和 theme_bw 的示例。 :

  library(ggplot2)

ggplot(df,aes(x = count,group = year ,fill = as.factor(year)))+
geom_histogram(position =identity,alpha = 0.5,breaks = seq(0,1600,by = 200),right = TRUE)+
scale_fill_discrete(name =Year)+
theme_bw(base_size = 20)+
xlab(values)



或者如果你真的希望它像来自 multhist (这不容易解释)的情节:

  ggplot(df,aes(x = count,group = year,fill = as.factor(year)))+ 
geom_histogram(position =dodge,breaks = seq(0,1600,by = 200),right = TRUE)+
scale_fill_discrete(name =Year)+
theme_bw(base_size = 20)+
xlab(values) +
scale_x_continuous(break = seq(100,1500,by = 200))

< img src =https://i.stack.imgur.com/o17bi.pngalt =在这里输入图片描述>


I want to create a histogram with multiple data series on the same plot. The best method that I can find to do this is multhist(). I would like a plot in a style similar to hist(), and while ggplot() can also be used to perform this task, the graphics style is not what I want.

Here is some example data:

df <- structure(list(year = c(2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L), count = c(187L, 199L, 560L, 1000L, 850L, 
400L, 534L, 911L, 390L, 1008L, 1173L, 1222L, 810L, 950L, 752L, 
1125L, 468L, 710L, 290L, 670L, 855L, 614L, 1300L, 950L, 670L, 
888L, 490L, 557L, 741L, 700L, 954L, 378L, 512L, 780L, 951L, 398L, 
1544L, 903L, 769L, 1399L, 1021L, 1235L, 1009L, 1222L, 255L)), .Names = c("year", 
"count"), class = "data.frame", row.names = c(NA, -45L))

And here is the code that I have used so far:

require(plotrix)
d2011<-df$count[df$year=="2011"]
d2012<-df$count[df$year=="2012"]
year<-list(d2011,d2012)
mh <- multhist(year, xlab="Count", ylab="Frequency", main="", cex.axis=1, col=c("dark gray", "light gray"), breaks=seq(0,1600, by=200))
box(bty="l", col="black")
legend.text<-c("2011","2012")
legend(locator(1), legend=legend.text, col=c("dark gray", "light gray"), pch=15, bty="n", cex=0.8)

This provides me with a 'barplot style' multi histogram, but I am having issues changing two graph parameters.

  1. I would like the plot to look more like a histogram and less like a barplot, so firstly I want to remove (or reduce) the space between the columns. I have tried using space = NULL, but this command does not appear to work with multhist

  2. I would like to change the x-axis so that axis tick marks are present in between bars on the plot and axis text is aligned with tick marks rather than positioned at the bar midpoint. I have tried using axis(side=1, …), but as multhist uses list objects to create plots these commands don’t appear to work.

Any suggestions would be greatly appreciated. Suggestions for other useful graphics packages that can plot histograms with multiple datasets would also be welcomed.

解决方案

Read the documentation of barplot to understand how to specify zero space:

multhist(year, xlab="Count", ylab="Frequency", main="", 
         cex.axis=1, col=c("dark gray", "light gray"), 
         breaks=seq(0,1600, by=200),
         space=c(0,0), beside=TRUE)

Here is an example with ggplot2 and theme_bw:

library(ggplot2)

ggplot(df, aes(x=count,group=year,fill=as.factor(year))) + 
  geom_histogram(position="identity", alpha=0.5, breaks=seq(0,1600, by=200),right=TRUE) +
  scale_fill_discrete(name="Year") +
  theme_bw(base_size=20) +
  xlab("values")

Or if you really want it like the plot from multhist (which is not as easy to interpret):

ggplot(df, aes(x=count,group=year,fill=as.factor(year))) + 
  geom_histogram(position="dodge", breaks=seq(0,1600, by=200),right=TRUE) +
  scale_fill_discrete(name="Year") +
  theme_bw(base_size=20) +
  xlab("values") +
  scale_x_continuous(breaks=seq(100,1500, by=200))

这篇关于使用R中的multhist创建具有多个数据序列的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆