如何在GGPLOT可视化中添加徽标? 我目前正在处理GGPLOT列图表,我正在尝试将徽标添加到右下角。这是图表的代码: DF

问题描述 投票:0回答:2
i遵循了我在网上找到的本教程,但是由于某种原因,它不会让我调整徽标的大小,无论我在image_scale函数上输入什么,它看起来都太小了。

img <- image_read("Logo.png") img <- image_scale(img,"200") img <- image_scale(img, "x200") g <- rasterGrob(img) size = unit(4, "cm") heights = unit.c(unit(1, "npc") - size,size) widths = unit.c(unit(1, "npc") - size, size) lo = grid.layout(2, 2, widths = widths, heights = heights) grid.show.layout(lo) grid.newpage() pushViewport(viewport(layout = lo)) pushViewport(viewport(layout.pos.row=1:1, layout.pos.col = 1:2)) print(p, newpage=FALSE) popViewport() pushViewport(viewport(layout.pos.row=2:2, layout.pos.col = 2:2)) print(grid.draw(g), newpage=FALSE) popViewport() g = grid.grab() grid.newpage() grid.draw(g) rm(list=ls())

我找到了另一个教程,并且在尝试此操作后,它根本没有显示任何内容。
mypng <- readPNG('Logo.png') print(mypng) logocomp <- p + annotation_raster(mypng, ymin = 4.5,ymax= 5,xmin = 30,xmax = 35)

您可以使用

cowplot

软件包轻松地将图像添加到用
ggplot
制成的任何图中。我将R徽标用作需要添加到图中的图像(使用
r ggplot2
2个回答
6
投票
软件包来读取它)。使用

cowplot

的一个优点是,您可以轻松地指定图和图像的大小和位置。
library(cowplot) library(magick) img <- image_read("Logo.png") # Set the canvas where you are going to draw the plot and the image ggdraw() + # Draw the plot in the canvas setting the x and y positions, which go from 0,0 # (lower left corner) to 1,1 (upper right corner) and set the width and height of # the plot. It's advisable that x + width = 1 and y + height = 1, to avoid clipping # the plot draw_plot(p,x = 0, y = 0.15, width = 1, height = 0.85) + # Draw image in the canvas using the same concept as for the plot. Might need to # play with the x, y, width and height values to obtain the desired result draw_image(img,x = 0.85, y = 0.02, width = 0.15, height = 0.15)

    

trone使用
grid.raster

,类似: Plot with imagegrid::grid.raster(img, x = 0.15, y = 0.05, width = unit(0.5, 'inches'))


0
投票

y

定义图像的位置。
调整
unit()

中的数字以调整图。
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.