我想为我的数据绘制直方图。我有两个问题。
首先,如何为每个中断值分隔条形(频率)。换句话说,我在对数刻度中为X轴设置了中断,我想仅为这些中断绘制图形....我不想要继续直方图条(下一个彼此),我想要它们之间的间隙.. ..
其次,我想知道如何将条件应用于休息时间。例如,我有break = c(0.1,0.2,0.5,1,2,5,10,30,40),如何添加一个中断作为一个类似break = 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得到了这个图,它自动计算一系列区间的直方图。我本想在ggplot中完成所有事情......
任何建议!!!
答案 0 :(得分: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))
给出了这个结果:
答案 1 :(得分:1)
这使用你原来的休息时间。我只是手动计算了数量。
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之间的更广泛的情节