相关疑难解决方法(0)

将数据叠加到背景图像上

我最近发现使用Tableau Public使用背景图像并在其上映射数据是多么容易.这是他们网站上的流程.如您所见,它非常简单,您只需告诉软件您要使用的图像以及如何定义坐标.

这个过程在R中是否直截了当?什么是最好的方法?

r background-image

15
推荐指数
3
解决办法
2万
查看次数

在一个ggplot上使用两个刻度颜色渐变

我想将散点图上的点的颜色比例渐变与图表上某些文本的颜色比例渐变结合起来.我可以单独执行它们,如下面的示例所示,但我似乎无法将它们放在一起......有没有办法做到这一点?

这是我想要组合的两种类型图(p和p1)的示例代码

l <- data.frame(prev=rnorm(1266), 
            aft=rnorm(1266), 
            day=as.factor(wday(sample(c(2:6),1266,replace=TRUE),abbr=TRUE, label=TRUE)), 
            month=as.factor(month(Sys.Date()+months(sample(0:11,1266,replace=TRUE)),abbr=TRUE, label=TRUE)), 
            ind=c(1:1266))
cors <- ddply(l, c("month", "day"), summarise, cor = round(cor(prev, aft), 3))


# below the text gains the colour gradient
p <- ggplot(l, aes(x=prev, y=aft)) + 
    geom_point() + 
    scale_colour_gradient(low = "red", high="blue")+ 
    facet_grid(day~month, scales="free_x")+
    geom_text(data=cors,aes(label=paste("r= ",cor,sep=""), size=abs(cor), colour=cor), x=Inf, y=Inf, vjust=1, hjust=1, show_guide=FALSE)+
    geom_hline(aes(yintercept=0))+
    geom_smooth(method="loess")
p

# below the points gain the colour gradient
p1 <- ggplot(l, aes(x=prev, y=aft)) + 
    geom_point(aes(colour=ind)) + 
    scale_colour_gradient("gray")+ 
    facet_grid(day~month, scales="free_x")+
    geom_text(data=cors,aes(label=paste("r= ",cor,sep=""), size=abs(cor), colour=cor), x=Inf, …
Run Code Online (Sandbox Code Playgroud)

gradient r colors scale ggplot2

8
推荐指数
1
解决办法
2952
查看次数

R ggplot背景图像未显示

问题

我想在地图上画一些箭头。通过一些谷歌搜索,我了解到annotation_customwithrasterGrob可以做到这一点:

library(ggplot2)
library(png)
library(grid)

img = readPNG('floorplan.png')
g = rasterGrob(img, interpolate = TRUE)

data = read.csv('est_err_structured.csv', header = FALSE)
x1 = data$V1
y1 = data$V2
x2 = data$V3
y2 = data$V4

p = ggplot() +
geom_segment(data = data, mapping = aes(x = x1, y = y1, xend = x2, yend = y2),
            arrow = arrow(length = unit(0.2, 'cm'))) +
annotation_custom(g, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) +
xlab('x (m)') …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
1
解决办法
535
查看次数

尝试将图像添加到极坐标图会给出“错误:annotation_custom 仅适用于笛卡尔坐标”

我已经尝试按照已经给出答案将图像添加到绘图中,但是它们在使用时不起作用coord_polar()

# install.packages("RCurl", dependencies = TRUE)
library(RCurl)
myurl <- "http://vignette2.wikia.nocookie.net/micronations/images/5/50/German_flag.png"

# install.packages("png", dependencies = TRUE)
library(png)
img <-  readPNG(getURLContent(myurl))

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)

ger <- grid::rasterGrob(img, interpolate=TRUE)

## works, adds the image to the plot
ggplot(mtcars, aes(x=mpg, y= cyl)) + geom_line() + annotation_custom(ger, 10, 15, 5)

## doesn't work
ggplot(mtcars, aes(x=mpg, y= cyl)) + geom_line() + annotation_custom(ger) + coord_polar()
> Error: annotation_custom only works with Cartesian coordinates
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望能够将标志图像定位在极坐标图的中心,并沿着 y 轴定位另一个图像。

无论如何要添加图像?它可以原样,无需转换。

我正在使用ggplot2 …

r ggplot2

3
推荐指数
1
解决办法
2373
查看次数

热图以图像为背景

我正在尝试使用R创建热图。我正在尝试使用ggplot2。我的实际数据帧要大得多,但在这里我只包含一小部分

x <- c(502.9, 512.1, 716.6, 759.7, 776.1, 776.5, 736.1, 271.3, 304.7, 279.9, 263.8, 726.6, 767.6, 778.8, 779.2, 263.6, 291.8, 472.6, 499.9, 684.9) 
y <- c(374.6, 367.4, 378.1, 373.7, 381.4, 395.7, 412.1, 399.2, 364.6, 382.1, 409.1, 410.4, 411.1, 429.4, 477.4, 468.6, 406.5, 343.2, 356.9, 365.2)
a <- data.frame(x,y)
ggplot(a, aes(x = x, y =y))  + stat_density2d(geom = "tile", aes(fill = ..density..), contour = FALSE) + scale_fill_gradient (low= "green", high="red") + geom_point()
Run Code Online (Sandbox Code Playgroud)

我想获得与此图像相似的东西,该区域越红,该区域中的点越多。

在此处输入图片说明

如您所见,当我尝试这样做时,我的背景是绿色的。如何更改代码以获取热图,如图所示?如何放置图片作为背景?

谢谢!

r heatmap ggplot2

2
推荐指数
1
解决办法
1674
查看次数

标签 统计

r ×5

ggplot2 ×4

background-image ×1

colors ×1

gradient ×1

heatmap ×1

scale ×1