如何在ggplot中使用图像作为点?
有没有什么方法可以使用 ggplot2 中的特定小图像作为散点图中的点。理想情况下,我想根据变量调整图像大小。
这是一个例子:
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(aes(size = qsec, shape = factor(cyl)))
所以我基本上想知道是否有办法提供特定图像作为形状?
Is there some way to use a specific small image as a point in a scatterplot with ggplot2. Ideally I will want to resize the images based on an variable.
Here's an example:
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(aes(size = qsec, shape = factor(cyl)))
So I basically want to know if there is a way to supply a specific image as the shape?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个名为 ggimage 的库可以做到这一点。请参阅此处的简介插图
您只需在
data.frame
中添加一列,其中包含图像的地址,该图像可以存储在网络上或本地计算机上,然后您可以使用geom_image()
:ps。请注意,
ggimage
取决于 EBImage 。因此,要安装 gginamge,我必须这样做:There is a library called
ggimage
to do that. See an intro vignette hereYou just have to add a column to your
data.frame
with the address of the images, which can be stored on the web or locally on your computer and then you can use thegeom_image()
:ps. Note that
ggimage
depends on EBImage. So to installgginamge
I had to do this:这是一个用于显示光栅图像而不是点的极简几何图形,
(来自 ggflags 包的输出)
Here's a minimalist geom to display raster images instead of points,
(output from the ggflags package)
首先,这是您的答案:
为了向您展示如何更好地使用小部件来表示数据差异,我请您参考 R 图库中的 chernoff 面孔。:
(来源:free.fr)
所有站点上提供了生成此示例的代码。
或者,查看 ggplot 的 stat_spoke 以获得简单的小部件:
(来源:had.co.nz)
grImport 提供了一种将简单 PDF 图像导入到您的绘图用作点。
现在对你的例子进行批评。
这不是散点图。它本质上是有序数据点的流动列表,其中颜色用于指示文本变量之一,并且使用无信息和冗余的小部件来构建数据,但在大小或形状方面不提供视觉反馈。
这不是一个好的图表,因为它完全无法回答所提出的问题“支付更多费用是否会带来更好的结果”,并且让读者很难自己得出该结论(以及其他必要的图表)。
此外,作者还浪费了 x、y 轴——这些轴本来可以很好地用于通过输出和结果来定位元素,以提供物有所值的视觉理解。相反,他们选择按照人均成本与平均毕业率的比率对图标进行排序,这有点有用,但没有回答所述问题,并且无法直接直观地比较大学之间的相对比率,或者成本与价值之间的关系。
正如我所说,在我看来,这是一个糟糕的图表,如果你复制它,你的读者不会得到很好的服务。
First, here's your answer:
To show you how to use how you might better use widgets to represent data differentiation, I refer you to the example of chernoff faces at the R graph gallery.:
(source: free.fr)
All the code to generate this example is available at the site.
Alternatively, look ggplot's stat_spoke for a simple widget:
(source: had.co.nz)
grImport provides a mechanism to import simple PDF images into your plot for use as points.
Now follows a critique of your example.
This is not a scatterplot. It's essentially a flowed list of ordered data points where colour is used to indicate one of the text variables, and an uninformative and redundant widget has been used to frame the data but otherwise provides no visual feedback in terms of size or shape.
It is not a good graph, because it completely fails to answer the stated question "Does Paying More Lead To Better Results", and leaves the reader to struggle draw that conclusion (and that other graph, as necessary) by themselves.
In addition, the authors have wasted the x, y axes - which could have been well used to position elements by outgoing and results, to provide a visual understanding of value-for-money. Instead they have opted to order the icons by the ratio of per head cost to average graduation rate, which is sort of useful, but doesn't answer the stated question, and fails to allow a direct visual comparison of relative ratio between colleges, or the relationship between cost and value.
As I say, in my opinion, this is a bad graph, and your readers would not be well served by having you replicate it.