gri*_*fin 37 r ggplot2 ggproto
有没有办法在ggplot2的散点图中使用特定的小图像作为点.理想情况下,我会想要根据变量调整图像大小.
这是一个例子:
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(aes(size = qsec, shape = factor(cyl)))
Run Code Online (Sandbox Code Playgroud)
所以我基本上想知道是否有办法提供特定图像作为形状?
bap*_*ste 23
这是一个显示光栅图像而不是点的极简主义geom,
library(ggplot2)
library(grid)
## replace by a named list with matrices to be displayed
## by rasterGrob
.flaglist <- list("ar" = matrix(c("blue", "white", "blue"), 1),
"fr" = matrix(c("blue", "white", "red"), 1))
flagGrob <- function(x, y, country, size=1, alpha=1){
grob(x=x, y=y, country=country, size=size, cl = "flag")
}
drawDetails.flag <- function(x, recording=FALSE){
for(ii in seq_along(x$country)){
grid.raster(x$x[ii], x$y[ii],
width = x$size[ii]*unit(1,"mm"), height = x$size[ii]*unit(0.5,"mm"),
image = .flaglist[[x$country[[ii]]]], interpolate=FALSE)
}
}
scale_country <- function(..., guide = "legend") {
sc <- discrete_scale("country", "identity", scales::identity_pal(), ..., guide = guide)
sc$super <- ScaleDiscreteIdentity
class(sc) <- class(ScaleDiscreteIdentity)
sc
}
GeomFlag <- ggproto("GeomFlag", Geom,
required_aes = c("x", "y", "country"),
default_aes = aes(size = 5, country="fr"),
draw_key = function (data, params, size)
{
flagGrob(0.5,0.5, country=data$country, size=data$size)
},
draw_group = function(data, panel_scales, coord) {
coords <- coord$transform(data, panel_scales)
flagGrob(coords$x, coords$y, coords$country, coords$size)
}
)
geom_flag <- function(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...) {
layer(
geom = GeomFlag, mapping = mapping, data = data, stat = stat,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...)
)
}
set.seed(1234)
d <- data.frame(x=rnorm(10), y=rnorm(10),
country=sample(c("ar","fr"), 10, TRUE),
stringsAsFactors = FALSE)
ggplot(d, aes(x=x, y=y, country=country, size=x)) +
geom_flag() +
scale_country()
Run Code Online (Sandbox Code Playgroud)
(从ggflags包输出)
raf*_*ira 19
有一个叫做这个的图书馆ggimage
.在这里看一个介绍小插图
您只需要data.frame
在图像地址中添加一列,可以存储在Web上或本地计算机上,然后您可以使用geom_image()
:
library("ggplot2")
library("ggimage")
# create a df
set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
y = rnorm(10),
image = sample(c("https://www.r-project.org/logo/Rlogo.png",
"https://jeroenooms.github.io/images/frink.png"),
size=10, replace = TRUE)
)
# plot2
ggplot(d, aes(x, y)) + geom_image(aes(image=image), size=.05)
Run Code Online (Sandbox Code Playgroud)
PS.请注意,这ggimage
取决于EBImage.所以要安装gginamge
我必须这样做:
# install EBImage
source("https://bioconductor.org/biocLite.R")
biocLite("EBImage")
# install ggimage
install.packages("ggimage")
Run Code Online (Sandbox Code Playgroud)
首先,这是你的答案:
为了向您展示如何更好地使用小部件来表示数据差异,我向您推荐R 图库中的chernoff 面的示例:
(来源:free.fr)
生成此示例的所有代码均可在该站点获得。
或者,查看 ggplot 的stat_spoke以获得简单的小部件:
(来源:had.co.nz)
grImport提供了一种将简单 PDF 图像导入绘图中以用作点的机制。
现在对你的例子进行批评。
这不是散点图。它本质上是有序数据点的流动列表,其中颜色用于指示文本变量之一,并且使用无信息且冗余的小部件来构建数据,但在大小或形状方面不提供视觉反馈。
这不是一个好的图表,因为它完全无法回答所提出的问题“支付更多费用是否会带来更好的结果”,并且让读者很难自己得出该结论(以及其他必要的图表)。
此外,作者还浪费了 x、y 轴——这些轴本来可以很好地用于通过输出和结果来定位元素,以提供物有所值的视觉理解。相反,他们选择按照人均成本与平均毕业率的比率对图标进行排序,这有点有用,但没有回答所述问题,并且无法直接直观地比较大学之间的相对比率,或者成本与价值之间的关系。
正如我所说,在我看来,这是一个糟糕的图表,如果你复制它,你的读者不会得到很好的服务。