首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将图像添加到ggplot

如何将图像添加到ggplot
EN

Stack Overflow用户
提问于 2020-05-26 00:48:27
回答 2查看 161关注 0票数 0

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

代码语言:javascript
复制
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")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-26 12:18:33

这将打印图像而不是点

代码语言:javascript
复制
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))
票数 1
EN

Stack Overflow用户

发布于 2020-05-26 10:08:40

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

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
ggdraw(GGplot_mean_performance) +
   draw_image(Lebron_James, x=1, y=1, hjust = 1, vjust = 1)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62006992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档