【问题标题】:ggplot2:histogram with conditionggplot2:带条件的直方图
【发布时间】:2014-04-10 22:40:00
【问题描述】:

我想为我的数据绘制直方图。我正在努力解决两个问题。

首先,如何为每个中断值分隔条(频率)。换句话说,我在对数刻度中为 X 轴设置了中断,我只想为这些中断绘制图表.. 我不想继续直方图条(彼此相邻),我想要它们之间的间隙.. ..

其次,我想知道如何将条件应用于中断。例如我有breaks=c(0.1,0.2,0.5,1,2,5,10,30,40),如何添加一个break作为条件,比如breaks=c(0.1,0.2,0.5,1,2 ,5,10,30,40, "任何值 > 40")。

这是我的数据:

structure(list(Time = c(0.08618, 0.086591, 0.086752, 0.18448, 
0.093463, 0.092634, 0.087419, 0.087307, 0.085734, 0.085272, 0.18448, 
0.085154, 0.085021, 0.084936, 0.091301, 0.177737, 0.18448, 0.089677, 
0.084906, 0.08614, 0.194328, 0.10183, 0.086494, 0.088581, 0.089195, 
0.089914, 0.090335, 0.086295, 0.086589, 0.10714, 0.265871, 0.315305, 
0.251465, 0.167559, 0.828143, 0.19883, 0.16173, 0.297092, 0.199025, 
0.196639, 0.20123, 0.206766, 0.205378, 0.490892, 0.226212, 11.197049, 
3.215287, 0.201566, 8.732194, 1.890716, 0.589986, 15.215162, 
0.196188, 0.219697, 9.816025, 0.290359, 0.233825, 3.230766, 4.605698, 
0.804751, 0.41611, 0.51733, 9.318433, 0.812274, 0.41187, 9.843202, 
0.607423, 0.823639, 932, 0.243041, 0.309908, 929, 0.70039, 0.706538, 
9.848918, 0.427812, 2.213476, 923, 3.428199, 921, 6.247575, 1.007718, 
918, 0.628396, 0.156748, 800, 914, 900, 890, 850, 650)), .Names = "Time", row.names = c(NA, 
-91L), class = "data.frame")

这是我的代码:

 ggplot(DF, aes(x =Time))+
 geom_histogram(bin=0.1,position = "dodge", colour = "black", fill = "white")+
 scale_x_log10(breaks=c(0.1,0.2,0.5,1,2,5,10,20,30,40),expand=c(0.005,0.1))+
 scale_y_continuous(expand=c(0.04,0.3))

下面是我得到的...

更新:我想得到类似的东西:

我知道这是条形图...但是,我从 excel 中得到了这个图,它会自动计算一系列 bin 的直方图。我希望在 ggplot 中完成所有工作...
有什么建议!!!

【问题讨论】:

  • 如果你想要间隔,你需要使用 geom_bar,这需要你手动计算 bin 和计数。
  • 我在 Excel 中得到了我想要的图表,希望在 ggplot2 中得到它! Excel 会进行手动计数。
  • 如果你发布一张 Excel 图表的图像,我可以看看我是否可以使用 ggplot 重新创建它。
  • 基本上,如果您使用 excel,它允许您设置 bin 范围并计算每个 bin 的频率。在 ggplot 中绘制 excel 的结果很简单。我希望在 ggplot 中完成这一切,而不是从 excel 中获取结果并在 ggplot 中进行条形图。我想别无选择!!除非有办法设置我不知道的垃圾箱范围!
  • 如果您只是发布您想要创建的情节的图像会容易得多。

标签: r ggplot2 histogram


【解决方案1】:

据我所知,ggplot2 中的直方图条之间不能有间隙。

对于你的第二个问题,这段代码:

ggplot(df, aes(x = Time))+
  geom_histogram(binwidth = 0.1, colour = "black", fill = "white")+
  scale_x_log10(breaks = c(0.1,0.2,0.5,1,2,5,10,20,30,40,100),
                labels = c("0.1","0.2","0.5","1","2","5","10","20","30","40","> 100"),
                expand = c(0.005,0.1))+
  scale_y_continuous(expand = c(0.04,0.3))

给出这个结果:

【讨论】:

  • 希望在 ggplot2 中获得它(在 Excel 中完成),感谢第二部分。您是否知道标签以外的另一种方式。我希望最后两个小节位于“>100”标签上。
【解决方案2】:

这使用您原来的休息时间。我只是手动计算了计数。

brks<-c(0.1,0.2,0.5,1,2,5,10,30,40,"more")

count<-rep(1,10)
count[1]<-length(DF[which(DF$Time<=0.1),])
count[2]<-length(DF[which(DF$Time>0.1 & DF$Time<=0.2),])
count[3]<-length(DF[which(DF$Time>0.2 & DF$Time<=0.5),])
count[4]<-length(DF[which(DF$Time>0.5 & DF$Time<=1),])
count[5]<-length(DF[which(DF$Time>1 & DF$Time<=2),])
count[6]<-length(DF[which(DF$Time>2 & DF$Time<=5),])
count[7]<-length(DF[which(DF$Time>5 & DF$Time<=10),])
count[8]<-length(DF[which(DF$Time>10 & DF$Time<=30),])
count[9]<-length(DF[which(DF$Time>30 & DF$Time<=40),])
count[10]<-length(DF[which(DF$Time>40),])

data<-data.frame("breaks"=brks,"count"=count)

ggplot(data,aes(x=breaks,y=count))+
  geom_bar(stat="identity")+
  scale_x_discrete(limits=c(0.1,0.2,0.5,1,2,5,10,30,40,"more"))

编辑:这是您第一次尝试的所有选项的情节:

ggplot(data,aes(x=breaks,y=count))+
  geom_bar(stat="identity",colour = "black",fill = "white")+
  scale_x_log10(breaks=c(0.1,0.2,0.5,1,2,5,10,30,40,600),
                labels = c("0.1","0.2","0.5","1","2","5","10","30","40","> 600"),
                expand=c(0.005,0.1))+
  scale_y_continuous(expand=c(0.04,0.3))

EDIT2:更宽的情节将距离置于 30 到 40 之间

【讨论】:

  • 完美..但是为什么X轴没有排序(即1,10,2,30,40,5)..我们是否需要使用'reorder'之类的!!跨度>
  • 是的,我在发布后注意到了这一点。我对其进行了编辑并修复了它。如果您愿意,我也可以在对数刻度上执行此操作,并使用您在原始帖子中在 ggplot 中设置的其他选项。
  • 非常感谢....是否可以控制中断标签之间的空间(即30-40)。
  • 您可以使绘图更宽,但以任何其他方式对其进行更改将不再具有对数 x 轴。见编辑更宽的情节。
  • 您也可以将最后一个柱设置为小于 100 的值。我将其设置为> 600的原因是为了准确性。该计数中的值都是 650 或更大。
猜你喜欢
  • 2017-04-26
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-23
相关资源
最近更新 更多