geom_image 显示为填充的几何图形而不是图像

问题描述 投票:0回答:1

我正在尝试对 Google 广告中的图像资源性能进行分类,并希望使用 geom_image 将实际图像添加到绘图中,以便我们知道哪一个效果最好。我之前已将图像添加到图中并且它有效,因此我使用相同的基本代码布局。但我的问题是,geom_images 只是作为填充几何图形返回,其颜色与我指定的颜色值相同。

plot with filled geoms instead of images

下面是我使用过的代码。为了表达目的,我将实际图像更改为球的图像。当我像这样运行它时,问题仍然存在。我怀疑问题在于我如何在

aes()
中表达事物,但我不确定应该如何改变它。 理想情况下,填充的几何图形都是球的图像。

# Packages
library(tidyverse)
library(ggimage)
library(ggchicklet)

# Create sample data
df <- data.frame(
  Asset = c("running_wide", "biomechanicsMan_wide", "running_square", "mandelbrot_wide", 
            "mandelbrot_square", "lines_wide", "systems_square", "systems_wide", "lines_square"),
  status = rep("Enabled", 9),
  Asset_type = c("Image", "Image", "Square image", "Image", "Square image", "Image", "Square image", "Image", "Square image"),
  Performance = c("Best", "Best", "Best", "Best", "Low", "Best", "Good", "Best", "Good"),
  Asset_source = rep("Advertiser", 9),
  val = rep(10, 9),
  font_size = c(10, 10, 10, 10, 10, 10, 10, 10, 10),
  asset_name = rep("ball", 9),
  path = rep("https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Basketball.png/220px-Basketball.png", 9)
)



ggplot(df, aes(x = reorder(Asset, font_size), y = val, fill = Performance, colour = Performance)) + 
  geom_chicklet(width = 0.9, alpha = 0.3, size = 1) + 
  scale_fill_manual(values = c("Best" = "#D6AF36", 
                               "Good" = "#A7A7AD",
                               "Low" = "#A77044"), name = NULL) + 
  scale_colour_manual(values = c("Best" = "#D6AF36", 
                                 "Good" = "#A7A7AD",
                                 "Low" = "#A77044"), name = NULL) +   
  geom_image(aes(x = reorder(Asset, font_size), y = 1, image = path), size = 0.08) +
  coord_flip() +
  theme_void() + 
  theme(
    legend.title = element_text(size = 16),
    legend.text=element_text(size=13)
  )
r ggplot2 ggimage
1个回答
0
投票

在您的代码中,您用颜色填充图像。只需将

aes(fill = Performance, colour = Performance)
ggplot()
移至
geom_chicklet()
行:

ggplot(df, aes(x = reorder(Asset, font_size), y = val)) + 
  geom_chicklet(aes(fill = Performance, colour = Performance), width = 0.9, alpha = 0.3, size = 1) + 
  scale_fill_manual(values = c("Best" = "#D6AF36", 
                               "Good" = "#A7A7AD",
                               "Low" = "#A77044"), name = NULL) + 
  scale_colour_manual(values = c("Best" = "#D6AF36", 
                                 "Good" = "#A7A7AD",
                                 "Low" = "#A77044"), name = NULL) +   
  geom_image(aes(x = reorder(Asset, font_size), y = 1, image = path), size = 0.08) +
  coord_flip() +
  theme_void() + 
  theme(
    legend.title = element_text(size = 16),
    legend.text=element_text(size=13)
  )

© www.soinside.com 2019 - 2024. All rights reserved.