I'm using webshot to save a png file from HTML :
p <- plot_ly(plotly::wind, r = ~r, t = ~t) %>% add_area(color = ~nms)%>%
layout(radialaxis = list(ticksuffix = "%"), orientation = 270)
saveWidget(as.widget(p), "temp.html")
webshot("temp.html",file = "temp.png",cliprect = "viewport")
This is giving me a plot like this. It returns a small image in the upper left corner. What should I do to have a full-size image in a center?
Related
I'm writing a function to save a plot to the R temp directory, then display it in the viewer pane. (Based in part on the last paragraph of this question.) If I save the plot as an .svg, it saves and shows up in the viewer pane at just the right size, based on the current pane dimensions:
library(ggplot2)
library(grDevices)
plt <- ggplot(iris) +
geom_point(aes(Sepal.Width, Sepal.Length))
dev.size()
# 5.250000 2.927083
plt_path <- tempfile(fileext = ".svg")
ggsave(plt_path, plt)
# Saving 5.25 x 2.93 in image
viewer <- getOption("viewer")
viewer(plt_path)
If I save it as a .png, however, the displayed image is way too big. (Note that ggsave() still reports saving it to the correct size.)
plt_path <- tempfile(fileext = ".png")
ggsave(plt_path, plt)
# Saving 5.25 x 2.93 in image
viewer <- getOption("viewer")
viewer(plt_path)
The issue persists using type = "cairo", and even if I manually set the ggsave dimensions:
dims <- dev.size(units = "in")
plt_path <- tempfile(fileext = ".png")
ggsave(plt_path, plt, width = dims[[1]], height = dims[[2]], units = "in")
How can I save and display the .png image to match the viewer pane dimensions?
The resolution needs to match that of your device. Default dpi is 300 in ggsave, but monitors are typically 96 dpi:
dims <- dev.size(units = "in")
plt_path <- tempfile(fileext = ".png")
ggsave(plt_path, plt, width = dims[[1]], height = dims[[2]], units = "in",
dpi = 96)
viewer <- getOption("viewer")
viewer(plt_path)
I was trying to embed an image on an echart plot. The image is coming perfectly if I provide a link of the image, but while I am trying to call the same image from my directory, the image is not coming in the plot.
I was tring the example provided in this website and the code I am using is as below.
library( echarts4r)
cars %>%
e_charts(speed) %>%
e_scatter(dist) %>%
e_image_g(
right = 20,
top = 20,
z = -999,
style = list(
image = "~/Rlogo.png",
width = 150,
height = 150,
opacity = .6
)
)
So how can I capture this Rlogo image from my local directory to the plot?
I want to plot a animated 3D scatterplot and save it as gif. I followed the code provided by the R Graph Gallery example: https://www.r-graph-gallery.com/3-r-animated-cube.html.
library(rgl)
library(magick)
options(rgl.printRglwidget = TRUE)
# Let's use the iris dataset
# iris
# This is ugly
colors <- c("royalblue1", "darkcyan", "oldlace")
iris$color <- colors[ as.numeric( as.factor(iris$Species) ) ]
# Static chart
plot3d( iris[,1], iris[,2], iris[,3], col = iris$color, type = "s", radius = .2 )
# We can indicate the axis and the rotation velocity
play3d( spin3d( axis = c(0, 0, 1), rpm = 20,dev = cur3d()),startTime = 0, duration = 10 )
# Save like gif
movie3d(
movie="3dAnimatedScatterplot",
spin3d( axis = c(0, 0, 1), rpm = 20,dev = cur3d()),
startTime = 0,
duration = 10,
dir = ".",
type = "gif",
clean = T,
fps=10,
convert=T
)
plot3d was successed output a 3d scatter plot.
Static 3d scatter plot
But the final output: 3dAnimatedScatterplot.gif,just a black image
3dAnimatedScatterplot.gif
when I set clean=F, all frame images are black. So, I guess the play3d() was not working.
Can anyone provide any help to me ? Thanks a lot !
Most likely snapshot3d isn't working for you. Try it with the option webshot = FALSE instead of the default webshot = TRUE. That uses a different mechanism for saving the image.
Is it possible to insert external images into a Powerpoint with officer without changing the image dimensions?
This is the code I have now where x is an rpptx object:
library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
t_file <- tempfile(pattern = "logo_", fileext = ".png")
download.file(image_url, destfile = t_file)
x <- x %>%
ph_with(value = external_img(t_file), location = ph_location(left = 9.4, top = 4.6))
This will change the dimensions of the image to 4x3 inches (the default for ph_location()).
I know you can use external R packages to get image metadata then pass this to the function so the actual image dimensions are used, but I don't want to add any more external R package dependencies.
You can always specify the image dimensions within your external_img function. I'm assuming you know what dimensions you want, so say your dimensions are width = 9, height = 5 it would look something like this:
library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
t_file <- tempfile(pattern = "logo_", fileext = ".png")
download.file(image_url, destfile = t_file)
x <- x %>%
#specify height and width
ph_with(value = external_img(t_file, width = 9, height = 5), location = ph_location(left = 9.4, top = 4.6))
Hope that is helpful!
I am using magick library in R. I want to add watermark on some pictures.
I used image_annotatefunction as below.
img <- image_read("C:\\Users\\Maydin\\Desktop\\manzara.png")
image_annotate(img, "my watermark", gravity = "northwest", location = "+200+275",
degrees = -30, size =50, font = NULL, color = "transparent",
strokecolor = "gray90", boxcolor = NULL)
At the end, the output looks like this;
However, what I want to have is something like this ,
Is that doable in magick in R?
For instance, this
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
library(magick)
img <- image_read(tf)
library(extrafont)
truetype_path <- paste0("#", subset(fonttable(), FullName=="Matura MT Script Capitals", fontfile)[1,])
image_annotate(img, "my watermark", gravity = "northwest", location = "+70+220",
degrees = -30, size = 80, font = truetype_path, color = "#FFFFFF66",
strokecolor = NULL, boxcolor = NULL)
gives this image:
I.e., choose a nice font like maybe Matura MT Script Capitals, tell image_annotate where to find it on your harddrive, adjust the opacity in the color argument - et voila. The font does not drop a shadow or show a relief, but maybe you can emulate this by plotting the text two times, the dark shadow one with a little offset to the other light one.
#lukA nicely demonstrates a solution using extrafonts package with magick package, but it looks like you can refer to the font by name within image_annotate() without the clever lookup of the full path. Use extrafonts::fonttable() to find the name.
library(extrafonts)
library(magick)
#download original example file
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
img <- image_read(tf)
#Use stroke to create an outline of the text with 50% alpha
magick::image_annotate(img, "Preview", location = "+100+175", degrees = -30, size=75, weight=700, font = "MonotypeCorsiva" , color = "transparent",
strokecolor = "#00000050", boxcolor = NULL)