I want to make a plot from raster data using levelplot. How to add a header which contains logo.png and title, like showed on this picture?
Here is my data: SST
Here is my basic code to produce this map:
r<-crop(raster(flname, varname="sst"), extent(90, 144, -20, 25))
png('SST.png', height = 2000, width = 2500, res = 300)
print(levelplot(r, col.regions = sst, at=seq(20, 34, 0.1),
yscale.components=yscale.raster.subticks,
xscale.components=xscale.raster.subticks,
margin=FALSE, ylab='Latitude', xlab='Longitude',
main=paste0(flname,' (deg-C)')))
dev.off()
You can use package magick to compose images:
library(rasterVis)
library(magick)
header <- image_read("~/Desktop/headerWithLogo.png")
fig <- image_graph(width = 600, height = 600, res=96)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
levelplot(r, margin=F, main ="Test \n")
dev.off()
out <- image_composite(fig, header, offset = "+50-5")
print(out)
Related
I have few separate plots in one R script that I'd like to save to png files. I run the script at the terminal and out of the 4 plots, only the last one was created as png file.
How to fix it so it will create and save all 4 plots as png files?
library(data.table)
library("ggplot2")
setwd("C:\\Users\\Desktop\\R\\figure_1A-D")
final_df_older <- fread("C:\\Users\\Desktop\\R\\older.csv"),
select=c(3,4))
png("Figure 1a - older_samples.png", width = 500, height = 400)
figure_1_wo_new <- ggplot(data = final_df_older, aes(x=final_df_older$`quntity`, y=final_df_older$`count`))
dev.off()
list.files(pattern = "png")
getwd()
final_df_all <- fread("C:\\Users\\Desktop\\R\\all.csv"),
select=c(3,4))
png("Figure 1b - all)samples.png", width = 500, height = 400)
figure_1_all <- ggplot(data = final_df_all, aes(x=final_df_all$`quntity`, y=final_df_all$`count`))
dev.off()
list.files(pattern = "png")
getwd()
final_df_new <- fread("C:\\Users\\Desktop\\R\\new.csv"),
select=c(3,4))
png("Figure 1c - new_samples.png", width = 500, height = 400)
figure_1_new_only <- ggplot(data = final_df_new, aes(x=final_df_new$`quntity`, y=final_df_new$`count`))
dev.off()
list.files(pattern = "png")
getwd()
library("gridExtra")
library("grid")
png("Figure 1d - merged_plot.png", width = 1500, height = 500)
merged_plot <- grid.arrange(figure_1_wo_new, figure_1_all,figure_1_new_only, ncol=3,
top = textGrob("Merged plot",
gp=gpar(fontsize=30,font=3,cex=1,col="darksalmon"),vjust=0.4))
dev.off()
list.files(pattern = "png")
getwd()
Does anyone know how to convert a simple plot into png_format without saving the picture...? In other words, I look for the most straight forward and fastest way to convert a simple plot into pixels, if it is not already (see code below)...
EDIT: Suggestion given by #hrbrmstr implemented...
## SolutionOne
library(png)
testONE <- system.time({
## Save plot as .png
tmp <- tempfile()
png(tmp, width = 800, height = 600, res = 72)
plot(1:10, pch = 19, col = "yellowgreen", cex = 20)
dev.off()
## Read .png
asPixels <- readPNG(tmp)
## Information needed (e.g. RGB)
dim(asPixels)
pixONE <- asPixels[300, 400, 1:3]
})
## SolutionTwo
library(magick)
testTWO <- system.time({
## Produce image using graphics device
fig <- image_graph()
plot(1:10, pch = 19, col = "yellowgreen", cex = 20)
## Information needed
pixTWO <- image_data(fig)[1:3, 400, 300]
dev.off()
})
testONE # elapsed time: 0.064
testTWO # elapsed time: 0.164
Thanks for any hint...
When creating a PNG file using writeGDAL, a georeferencing file is created (.aux.xml) along with the PNG file. Is there a way to prevent this from happening?
The following code creates the files as explained above.
library(raster)
library(rgdal)
r <- raster(xmn=742273.5, xmx=742702.5, ymn=6812515.5, ymx=6812995.5, ncols=144, nrows=161)
r <- setValues(r, 1:ncell(r))
rSpdf <- as(r, 'SpatialPixelsDataFrame')
rSpdf$colors <- as.numeric(cut(rSpdf$layer, breaks = 10))
writeGDAL(rSpdf[, 'colors'], 'test.png', drivername = 'PNG', type = 'Byte', mvFlag = 0, colorTables = list(colorRampPalette(c('black', 'white'))(11)))
By setting rgdal::setCPLConfigOption("GDAL_PAM_ENABLED", "FALSE") the .aux.xml file is not created.
Thank you Val for pointing me to the post.
library(raster)
library(rgdal)
rgdal::setCPLConfigOption("GDAL_PAM_ENABLED", "FALSE")
r <- raster(xmn=742273.5, xmx=742702.5, ymn=6812515.5, ymx=6812995.5, ncols=144, nrows=161)
r <- setValues(r, 1:ncell(r))
rSpdf <- as(r, 'SpatialPixelsDataFrame')
rSpdf$colors <- as.numeric(cut(rSpdf$layer, breaks = 10))
writeGDAL(rSpdf[, 'colors'], 'test.png', drivername = 'PNG', type = 'Byte', mvFlag = 0, colorTables = list(colorRampPalette(c('black', 'white'))(11)))
I called highchart with height parameter to make a taller chart successfully in RStudio:
chart <- highchart(type = "stock", height=800, ...)
When in Shiny, the chart shown up but ignore the height parameter. Is there any way to fix it ?
If you are using shiny you can set the dimension in the highchartOutput() function:
# ui.R
...
highchartOutput("my_hc_id", width = 450, height = 300)
...
Other way using the hc_size function compared with the #pork-chop answer:
library(quantmod)
library(highcharter)
x <- getSymbols("GOOG", auto.assign = FALSE)
b <- hchart(x)
b <- hc_size(b, width = 450, height = 300)
b
Try this:
library(quantmod)
library(highcharter)
x <- getSymbols("GOOG", auto.assign = FALSE)
a <- hchart(x)
a$width <- 450
a$height <- 300
a
I've got a large (8GB) raster file that I'd like to plot in R. I try to do the below, but I run out of memory. I'd like some input and ideas on what to do please? Thanks, James.
library(raster)
library(rasterVis)
# ---- setting the colours ------------#
my.at <- c( 0,0.5, 1, 2.5, 5, 10, 15, 20, 25, 30,35,40,45,50, 100, 260)
colours<-c("paleturquoise","lightcyan", "brown","burlywood","bisque2","darkorange2","lightpink",
"darkred","gold1","mediumorchid1","hotpink2", "darkblue","darkblue", "violetred4","violetred4")
pdf("plot_1b.pdf", width=10, height=20)
par(bty='n')
par(mar=c(0, 0,0,0))
plot(raster("2015-06-23_001403_21674_63649.grd"),
xlim=c(-1200000, -650000),
ylim=c(-260000,550000),
maxpixels=445500000000,
breaks=my.at,
col = colours,
lab.breaks=my.at,
legend=FALSE,
axes=FALSE)
dev.off()