我正试图在背景图像上绘制一些数据.问题是两个层最终都使用相同的比例.不幸的是,这是有问题的.
一个例子.
我想在这张图片上绘制一些数据.

对.所以我在ggplot中绘制它就像这样.
img <- readJPEG("image.jpg")
image <- apply(img, 1:2, function(v) rgb(v[1], v[2], v[3]))
image <- melt(image)
ggplot(image, aes(row, -column, fill=fill)) + geom_tile() + scale_fill_identity()
Run Code Online (Sandbox Code Playgroud)
它运作良好.所以,让我们在顶部添加一些数据.
df <- data.frame(x=sample(1:64, 1000, replace=T),
y=sample(1:64, 1000, replace=T))
ggplot(df, aes(x,y)) + stat_bin2d()
Run Code Online (Sandbox Code Playgroud)
绘制样本数据,我得到了这个.

所以我只想在渐变图像上分层这个数据图.
ggplot(image, aes(row, -column, fill=fill)) + geom_tile() +
scale_fill_identity() + geom_point(data=df2, aes(x=x, y=-y))
Run Code Online (Sandbox Code Playgroud)
但它最终会像这样

尝试指定第二个填充比例会引发错误.我看到这说不能做,但我希望有一个解决方法或我忽略的东西.
cnt = 100
df <- data.frame(x = c(rnorm(cnt, mean = 3), rnorm(cnt, mean = 0)),
y = rnorm(2 * cnt), g = rep(0:1, each = cnt))
ggplot(df, aes(x, y, color = as.factor(g))) +
stat_density2d(aes(fill = ..level..), alpha = 0.3, geom = "polygon")
Run Code Online (Sandbox Code Playgroud)
这样就可以根据因子创建一个填充的等高线图.

我想为每个轮廓使用不同的填充比例,使得g = 0具有红色填充,而g = 1具有蓝色填充.这是可能的,如果是的话,怎么样?
我正在尝试创建一个随机生成具有某些特定属性的值的数据集:
我已经成功地生成了接近我想要的数据,但速度非常慢。我怀疑它很慢,因为 while 循环。
simSession <- function(sessionid = 1) {
s <- data.frame(sessionid = sessionid, userid = seq(1:12))
total <- sample(48:72, 1)
mu = total / 4
sigma = 3
s$x <- as.integer(rnorm(mean=mu, sd=sigma, n=nrow(s)))
while(sum(s$x) > total) {
# i <- sample(nrow(s), 1)
i <- sample(rep(s$userid, s$x), 1)
if(s[i, ]$x > 1) {
s[i, ]$x <- s[i, ]$x - 1
} else {
s[i, ]$x = 1
} …Run Code Online (Sandbox Code Playgroud) 给定一个数据集,其中值可以位于数据帧的任何一列中:
df <- data.frame(h1=c('a', 'b', 'c', 'a', 'a', 'b', 'c'), h2=c('b', 'c', 'd', 'b', 'c', 'd', 'b'), h3=c('c', 'd', 'e', 'e', 'e', 'd', 'c'))
Run Code Online (Sandbox Code Playgroud)
如何获取指定哪些行包含目标值的逻辑向量?在这种情况下,搜索'b',我想要一个行(1,2,4,6,7)的逻辑向量为TRUE.
实际数据集更大,更复杂,所以我试图避免for循环.
谢谢
编辑:
这似乎有效.
>apply(df, 1, function(x) {'b' %in% as.vector(t(x))}) -> i
> i
[1] TRUE TRUE FALSE TRUE FALSE TRUE TRUE
Run Code Online (Sandbox Code Playgroud) 我有一组严重偏斜的数据.这在执行stat_bin2d绘图时会产生问题.结果是大多数图形是深蓝色,只有几个点是不同的颜色.我想让图表使用整个颜色范围更多一点.
问题的一个例子来自ggplot文档方向.
ggplot(diamonds, aes(carat, price)) + stat_bin2d()
Run Code Online (Sandbox Code Playgroud)
结果图只有少数几个不是深蓝色的位置.
如何调整颜色范围的映射以显示更多细节?我知道我可以设置限制,但这并不完全符合法案,因为它使任何超出限制的东西都是灰色的.
ggplot(diamonds, aes(carat, price)) + stat_bin2d() + scale_fill_gradient(limits=c(1, 100))
Run Code Online (Sandbox Code Playgroud)
像这样的东西,它们的颜色也是灰色的.
使用这个 JSFiddle作为我的例子,我的目的是让点击轴标签(第 1 组、第 2 组、第 3 组)将打开数据中指定的相应链接。
我试图将数据重新绑定到 .ticks 选择,但这仅适用于第一个轴标签。由于仅将名称传递给轴映射,因此我不确定如何保留将 on( 'click'... ) 事件添加到标签的关系。
提前感谢您的时间和帮助。
var data = [{
"Items": [{
"Value": 3327,
"Name": {
"Value": "A"
},
}, {
"Value": 794,
"Name": {
"Value": "B"
},
}, {
"Value": 1999,
"Name": {
"Value": "C"
},
}, ],
"Type": "Group",
"Name": {
"Value": "Group 1"
},
"Link": "www.espn.com",
}, {
"Items": [{
"Value": 3317,
"Name": {
"Value": "A"
},
}, {
"Value": 787,
"Name": {
"Value": "B" …Run Code Online (Sandbox Code Playgroud) 因此,expand.grid返回传递的向量的所有组合的df.
df <- expand.grid(1:3, 1:3)
df <- expand.grid(1:3, 1:3, 1:3)
Run Code Online (Sandbox Code Playgroud)
我想要的是一个带有1个参数(向量数)并返回适当数据帧的通用函数.
combinations <- function(n) {
return(expand.grid(0, 1, ... n))
}
Run Code Online (Sandbox Code Playgroud)
这样
combinations(2) returns(expand.grid(1:3, 1:3))
combinations(3) returns(expand.grid(1:3, 1:3, 1:3))
combinations(4) returns(expand.grid(1:3, 1:3, 1:3, 1:3))
Run Code Online (Sandbox Code Playgroud)
等等
这个例子说明了我遇到的问题.例1和2是完美的.没有问题.在示例3中,格式分崩离析,我不确定原因.我如何使示例3看起来像其他示例,但允许我将list-group-item网格化.