我想用matplotlib制作xy数据的多个hexbin密度图,类似于这个:http://matplotlib.org/1.4.0/examples/pylab_examples/hexbin_demo.html
但我想将每个六边形的数量除以一个给定的数字(我的密度图中的最高峰值),这样我所有的阴影图都会有相同的颜色,并且所有图的颜色条都是[0,1]范围.
有人能告诉我一个有用的例子吗?
谢谢你的期待,
亚诺什
有没有办法让R中的density()函数使用计数与概率?
例如,在使用直方图函数检查密度分布时,我有两个选择hist
:
hist(x,freq=F) #"graphic is a representation of frequencies, the counts component of the result"
hist(x,freq=T) #"probability densities, component density, are plotted (so that the histogram has a total area of one)"
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以使用该density
功能执行类似的操作?
在我的具体示例中,我有许多直径不同的树木。(我会注意到,我将数据保持为连续的大小比例,而不是将其分为离散的大小类)。当我将density
函数与该数据一起使用时(即plot(density(dat$D,na.rm=T,from=0))
),它为我提供了每种尺寸的概率(当然是平滑的)的密度估计。我对将这些数据报告为茎/面积与概率的关系更感兴趣,因此我更喜欢密度估计值来使用计数。
想法?
更新:
这是一些真实的示例数据:
dat <- c(6.6, 7.1, 8.4, 27.4, 11.9, 18.8, 8.9, 25.4, 8.9, 8.6, 11.4, 19.3, 7.6, 42.2, 20.8, 25.1, 38.1, 42.2, 5.2, 34.3, 42.7, 34, 37.3, 45.5, 39.4, 25.1, 30.7, 23.1, 43.4, 19.6, 30.5, 23.9, 10.7, …
Run Code Online (Sandbox Code Playgroud) r histogram frequency-distribution kernel-density density-plot
我正在尝试使用ggplot2 R库在密度分布上绘制某些数据的中值。我想将中间值作为文本打印在密度图的顶部。
您将看到一个示例的意思(使用“钻石”默认数据框):
我正在打印三个项目:密度图本身,一条垂直线,显示每个切割的中位数价格,以及带有该值的文本标签。但是,正如您所看到的,中位数价格在“ y”轴上重叠(这种美感在geom_text()函数中是必需的)。
有没有办法为每个中间价格动态分配一个“ y”值,以便在不同的高度打印它们?例如,每个“切口”的最大密度值。
到目前为止,我已经知道了
# input dataframe
dia <- diamonds
# calculate mean values of each numerical variable:
library(plyr)
dia_me <- ddply(dia, .(cut), numcolwise(median))
ggplot(dia, aes(x=price, y=..density.., color = cut, fill = cut), legend=TRUE) +
labs(title="diamond price per cut") +
geom_density(alpha = 0.2) +
geom_vline(data=dia_me, aes(xintercept=price, colour=cut),
linetype="dashed", size=0.5) +
scale_x_log10() +
geom_text(data = dia_me, aes(label = price, y=1, x=price))
Run Code Online (Sandbox Code Playgroud)
(我为geom_text函数中的y美感分配了一个常量值,因为它是强制性的)
我已经绘制了一个分布,我想对> 95% 的区域进行着色。但是,当我尝试使用此处记录的不同技术时:ggplot2 按组密度曲线下的阴影区域它不起作用,因为我的数据集的长度不同。
AGG[,1]=seq(1:1000)
AGG[,2]=rnorm(1000,mean=150,sd=10)
Z<-data.frame(AGG)
library(ggplot2)
ggplot(Z,aes(x=Z[,2]))+stat_density(geom="line",colour="lightblue",size=1.1)+xlim(0,350)+ylim(0,0.05)+geom_vline(xintercept=quantile(Z[,2],prob=0.95),colour="red")+geom_text(aes(x=quantile(Z[,2],prob=0.95)),label="VaR 95%",y=0.0225, colour="red")
#I want to add a shaded area right of the VaR in this chart
Run Code Online (Sandbox Code Playgroud) 我试图找出为什么沿 x 轴出现一条紫色线,该线与我的图例中的“Prypchan,Lida”颜色相同。我查看了数据,没有发现任何问题。
ggplot(LosDoc_Ex, aes(x = LOS)) +
geom_density(aes(colour = AttMD)) +
theme(legend.position = "bottom") +
xlab("Length of Stay") +
ylab("Distribution") +
labs(title = "LOS Analysis * ",
caption = "*exluding Residential and WSH",
color = "Attending MD: ")
Run Code Online (Sandbox Code Playgroud)
我需要将密度线与 geom_histogram 的高度对齐,并在 y 轴而不是密度上保留计数值。
我有这两个版本:
# Creating dataframe
library(ggplot2)
values <- c(rep(0,2), rep(2,3), rep(3,3), rep(4,3), 5, rep(6,2), 8, 9, rep(11,2))
data_to_plot <- as.data.frame(values)
Run Code Online (Sandbox Code Playgroud)
# Option 1 ( y scale shows frequency, but geom_density line and geom_histogram are not matching )
ggplot(data_to_plot, aes(x = values)) +
geom_histogram(aes(y = ..count..), binwidth = 1, colour= "black", fill = "white") +
geom_density(aes(y=..count..), fill="blue", alpha = .2)+
scale_x_continuous(breaks = seq(0, max(data_to_plot$values), 1))
Run Code Online (Sandbox Code Playgroud)
y 刻度显示频率,但 geom_密度线和 geom_histogram 不匹配
# Option 2 (geom_density line and geom_histogram …
Run Code Online (Sandbox Code Playgroud) 我在R中有一个数据如下:
bag_id location_type event_ts
2 155 sorter 2012-01-02 17:06:05
3 305 arrival 2012-01-01 07:20:16
1 155 transfer 2012-01-02 15:57:54
4 692 arrival 2012-03-29 09:47:52
10 748 transfer 2012-01-08 17:26:02
11 748 sorter 2012-01-08 17:30:02
12 993 arrival 2012-01-23 08:58:54
13 1019 arrival 2012-01-09 07:17:02
14 1019 sorter 2012-01-09 07:33:15
15 1154 transfer 2012-01-12 21:07:50
Run Code Online (Sandbox Code Playgroud)
class(event_ts)是哪里POSIXct
.
我想在不同的时间找到每个位置的袋子密度.
我使用了命令geom_density(ggplot2)
,我可以很好地绘制它.我想知道density(base)
和这个命令之间是否有任何区别.我的意思是他们正在使用的方法或他们正在使用的默认带宽等有任何区别.
我需要将密度添加到我的数据框中.如果我使用过该函数density(base)
,我知道如何使用该函数approxfun
将这些值添加到我的数据框中,但是我想知道它在使用时是否相同geom_density(ggplot2)
.
我试图在下面的情节背景上有2个"阴影".这些阴影应分别代表橙色和蓝色点的密度.是否有意义?
以下是要改进的ggplot:
这是df
我用来创建这个图的代码和数据(矩阵):
PC1 PC2 aa
A_akallopisos 0.043272525 0.0151023307 2
A_akindynos -0.020707141 -0.0158198405 1
A_allardi -0.020277664 -0.0221016281 2
A_barberi -0.023165596 0.0389906701 2
A_bicinctus -0.025354572 -0.0059122384 2
A_chrysogaster 0.012608835 -0.0339330213 2
A_chrysopterus -0.022402365 -0.0092476009 1
A_clarkii -0.014474658 -0.0127024469 1
A_ephippium -0.016859412 0.0320034231 2
A_frenatus -0.024190876 0.0238499714 2
A_latezonatus -0.010718845 -0.0289904165 1
A_latifasciatus -0.005645811 -0.0183202248 2
A_mccullochi -0.031664307 -0.0096059126 2
A_melanopus -0.026915545 0.0308399009 2
A_nigripes 0.023420045 0.0293801537 2
A_ocellaris 0.052042539 0.0126144250 2
A_omanensis -0.020387101 0.0010944998 2
A_pacificus 0.042406273 -0.0260308092 2 …
Run Code Online (Sandbox Code Playgroud) 导出为PDF后,我不断获得这些白线.它们在R中不可见,但一旦导出就会显示.这似乎也是一个特定于mac的问题.导出到tiff时不会出现问题.
数据:
> dput(head(newdemodf1,10))
structure(list(x = c(21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L), y = c(27L, 26L, 27L, 28L, 29L, 30L, 31L, 34L, 35L,
36L), totaltime = c(0.0499999523162842, 0.0499999523162842, 0.379999876022339,
0.0500004291534424, 0.0299999713897705, 0.109999895095825, 0.0499999523162842,
0.0299999713897705, 0.0500001907348633, 0.0299999713897705)), .Names = c("x",
"y", "totaltime"), row.names = c(NA, 10L), class = "data.frame")
library(ggplot2)
library(RColorBrewer)
ggplot(newdemodf1) +
stat_density2d(aes(x=x, y=y, z=totaltime, fill = ..density..),
geom="tile", contour = FALSE) +
scale_fill_gradientn(colours=cols)
Run Code Online (Sandbox Code Playgroud)
然后我导出到PDF导入adobe illustrator.但是,我得到的情节如下:
如何删除白线?这是否涉及平滑颜色?或以某种方式更换瓷砖?缺少x,y组合?任何帮助赞赏.
我有一个如下所示的数据框:
DF<-as.data.frame(t(replicate(150, sample(seq(100, 1000),15,replace=T))),rownames=T)
Run Code Online (Sandbox Code Playgroud)
我想将各行绘制为密度,以便得到具有多个密度曲线的图.我知道我可以一行一行地做到这一点:
plot(density(DF[,1]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,2]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,3]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,4]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,5]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,6]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,7]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,8]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,9]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,10]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,11]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,12]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,13]),col = adjustcolor('black', …
Run Code Online (Sandbox Code Playgroud)