0

我想使用 ggimage 包将从网页(LeBron_James 分配在下面)中提取的图像添加到 ggplot 中。如何将它添加到下面的 ggplot rscript?

GGplot_mean_performance <- FirstPick_Wage_Performance %>% 
  ggplot(aes(x=Player, y=mean_performance, label=mean_performance))+ 
  geom_point(stat="identity", size=3)  +
  geom_hline(aes(yintercept = mean(mean_performance)), color = "blue", size = 3) + 
  geom_segment(aes(y = mean(mean_performance),             
                   x = Player, 
                   yend = mean_performance, 
                   xend = Player,)) +
  geom_text(color="red", size=4) +
  labs(title="Lollipop Chart of Player's mean performance for the first two years as draft pick \ncompared to mean performance of the population", 
       subtitle="Blake Griffin is the best performing 1st Pick and Anthony Bennett is the worst performing 1st Pick",
       caption="Source: https://www.basketball-reference.com & https://basketball.realgm.com") + 
  ylim(20, 80) +
  coord_flip() +
  theme(legend.position = "none")

Lebron_James <- image_read2("https://nba-players.herokuapp.com/players/james/lebron")
4

2 回答 2

1

您没有提供任何数据,也没有指定要放置图像的位置或方式,因此我无法重现您的情节。但是,您应该能够使用cowplot 做您想做的事情。例如:

library(ggimage)
library(cowplot)
Lebron_James <- image_read2("https://nba-players.herokuapp.com/players/james/lebron")


df <- data.frame(a=seq(1,10), b=seq(1,10))

p <- ggplot(df)+
  geom_line(aes(a, b)) +
  theme_cowplot(12)

# place image under plot
a <- ggdraw() + 
  draw_image(Lebron_James, scale = 0.5) +
  draw_plot(p) 

# place image above plot
b <- ggdraw(p) + 
  draw_image(Lebron_James, x = 1, y = 1, hjust = 1, vjust = 1, width = 0.13, height = 0.2)

plot_grid(a,b)

您可以将图像放在绘图下方(在这种情况下,您需要选择一个牛图主题以不覆盖图像),或者您可以将其放在绘图上方,如第二个示例所示,并调整坐标以匹配您想要的结果。从您的示例中,这可能会起作用:

ggdraw(GGplot_mean_performance) +
   draw_image(Lebron_James, x=1, y=1, hjust = 1, vjust = 1)
于 2020-05-26T02:08:39.760 回答
1

这将绘制图像而不是点

library("ggplot2")
library("ggimage")


d <- data.frame(x = 1:2,
                y = 1:2,
                image = c("https://nba-players.herokuapp.com/players/james/lebron",
                          "https://nba-players.herokuapp.com/players/james/lebron"))

ggplot(d, aes(x, y)) + 
  geom_image(aes(image=image), size=.2) +
  scale_x_continuous(limits = c(0, 3)) +
  scale_y_continuous(limits = c(0,3))
于 2020-05-26T04:18:32.930 回答