I'm currently working on a ggplot column chart and I'm trying to add a logo to the bottom right. This is the code to the chart:
df <- data.frame(Names = c("2001", "2004", "2008", "2012", "2018"),
Value = c(47053, 68117, 171535, 241214, 234365))
p <- ggplot(df, aes(x = Names, y = Value)) +
geom_col(fill = "#DB4D43") + theme_classic() +
geom_text(aes(label = Value, y = Value + 0.05),
position = position_dodge(0.9),
vjust = 0)
I followed this tutorial I found online, but for some reason, it won't let me adjust the size of the logo and it ends up looking too small no matter what I type on the image_scale function.
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())
I found another tutorial and, after trying this, it doesn't show anything at all when I run it.
mypng <- readPNG('Logo.png')
print(mypng)
logocomp <- p + annotation_raster(mypng, ymin = 4.5,ymax= 5,xmin = 30,xmax = 35)
You could use the cowplot package to easily add an image to any plot made with ggplot. I used the R logo as the image that needs to be added to the plot (using magick package to read it). One advantage of using cowplot is that you can easily specify the size and position of both the plot and the image.
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)
Try using grid.raster, something like:
grid::grid.raster(img, x = 0.15, y = 0.05, width = unit(0.5, 'inches'))
x and y to define location of the image.
Adjust the number in unit() to resize the plot.
Related
How could I produce a legend that is not a rectangle with ggplot?
For example, how could I get round(ish) corners instead of these sharp corners?
Everybody knows them, but here is one example:
ggplot(baseball, aes(year, r)) +
geom_point(aes(col=g))
Would something similar to this: Rounded corners in ggplot2? maybe work?
The particular strategy mentioned in the linked post is likely going to fail because the colourbar isn't parameterised as a rectangle but rather as a raster. The alternative strategy then is to draw white, inverted quarter circles near the corners.
One way of doing that is to define your own class of colour bar, and add some code that draws these white corners. The code below should work for vertical colour bars.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
library(grid)
# Custom constructor, just tags on another class
my_round_colourbar <- function(...) {
guide <- guide_colourbar()
class(guide) <- c("my_round_colourbar", class(guide))
guide
}
# Customised drawing code that adds the corners
guide_gengrob.my_round_colourbar <- function(...) {
# Use the usual drawing method
grob <- NextMethod()
# Find the bar from the normal legend
is_bar <- grep("bar", grob$layout$name)[[1]]
bar <- grob$grobs[[is_bar]]
# Measure the bar
width <- convertUnit(bar$width, "cm", valueOnly = TRUE)
height <- convertUnit(bar$height, "cm", valueOnly = TRUE)
minor <- min(width, height)
# Draw 'caps': 2 inverted corner pieces
t <- seq(0, pi, length.out = 180)
cap <- data.frame(
x = c((cos(t) / 2), -0.5, 0.5) * minor,
y = c((-sin(t) / 2) + 0.5, 0, 0) * minor
)
# Draw a polygon for each cap
bottom <- polygonGrob(x = unit(cap$x, "cm") + bar$x,
y = unit(cap$y, "cm") + bar$y -
unit(height * 0.5, "cm"),
gp = gpar(fill = "white",col = NA),
default.units = "cm")
top <- polygonGrob(x = unit(cap$x, "cm") + bar$x,
y = unit(-cap$y, "cm") + bar$y + unit(height * 0.5, "cm"),
gp = gpar(fill = "white", col = NA),
default.units = "cm")
# Add polygon to bar
bar <- grobTree(bar, top, bottom)
grob$grobs[[is_bar]] <- bar
return(grob)
}
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(colour = drat)) +
guides(colour = my_round_colourbar())
Created on 2021-07-05 by the reprex package (v1.0.0)
I really like the aesthetics of The Economist magazine and I use the theme_economist often. However, I am curious as to how they create the red lines in the top left in a lot of their charts. See image below and where I circled.
This question is a mix of "how to annotate outside the plot area" and "how to annotate in npc coordinates". Therefore, I offer two options.
Both unfortunately require a bit of trial and error in order to correctly place the segment. For option 1, it is the y coordinate which we have to "guess", and for option 2 it's x!
In order to make y slightly less guess work, I tried an approach to position is relative to the default axis breaks. using the fabulous information from this answer. This is of course not necessary, one can also simply trial and error.
For option 2, I modified a function from user Allan Cameron's answer here. He mentions a way to figure out x and y, I guess one could use the title, and then place the annotation based on that.
library(ggplot2)
p <-
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
ggtitle("lorem ipsum") +
theme(plot.margin = margin(t = 1.5, unit = "lines")) # this is always necessary
# OPTION 1
# semi-programmatic approach to figure out y coordinates
y_defaultticks <- with(mtcars, labeling::extended(range(wt)[1], range(wt)[2], m = 5))
y_default <- y_defaultticks[2] - y_defaultticks[1]
y_seg <- max(mtcars$wt) + 0.75 * y_default
p +
annotate(geom = "segment", x = - Inf, xend = 12, y = y_seg, yend = y_seg,
color = "red", size = 5) +
coord_cartesian(clip = "off", ylim = c(NA, max(mtcars$wt)),
xlim = c(min(mtcars$mpg), NA))
# OPTION 2
annotate_npc <- function(x, y, height, width, ...) {
grid::grid.draw(grid::rectGrob(
x = unit(x, "npc"), y = unit(y, "npc"), height = unit(height, "npc"), width = unit(width, "npc"),
gp = grid::gpar(...)
))
}
p
annotate_npc(x = 0.07, y = 1, height = 0.05, width = 0.05, fill = "red", col = NA)
Created on 2021-01-02 by the reprex package (v0.3.0)
I was running the example here, and noticed that the horizontal arrow connecting the Total to the Ineligible boxGrobs doesn't always touch the left-edge of the the Ineligible boxGrob.
It seems to depend on the width of the viewing window in RStudio. This does not seem to be the case for the vertical arrow, which always seem to perfectly connect to the top of the correct boxGrob.
Is there a way to force the arrow to touch the side of the box and not go any further? I am trying to save the output to a pdf, and by default it seems to use a wider plotting window so all of my horizontal arrows don't align with the correct boxes.
Narrow plotting window:
Wide plotting window:
I have tried manually creating a viewport with a wider area, but that didn't change anything in the pdf:
Code:
library(grid)
library(Gmisc)
vp <- grid::viewport(x = 10, y = 10, clip = 'on', xscale = c(0, 10),
yscale = c(0, 10), default.units = 'inch')
grid::pushViewport(vp)
leftx <- .25
midx <- .5
rightx <- .75
width <- .4
gp <- gpar(fill = "lightgrey")
# add box/connectors to the plot
(total <- boxGrob("Total\n N = NNN",
x=midx, y=.9, box_gp = gp, width = width))
(rando <- boxGrob("Randomized\n N = NNN",
x=midx, y=.75, box_gp = gp, width = width))
connectGrob(total, rando, "v")
(inel <- boxGrob("Ineligible\n N = NNN",
x=rightx, y=.825, box_gp = gp, width = .25, height = .05))
connectGrob(total, inel, "-")
For the time being, this problem can be solved using absolute unit.
Example code:
(inel <- boxGrob("Ineligible\n N = NNN",
x=rightx, y=.825, box_gp = gp, width = unit(2, "inch"), height = .05))
I have a grid composed of several ggplots and want to add an x axis, where axis ticks and annotations are added between the plots. I could not came up with a better solution than to create a custom plot for the axis and adding it below with arrangeGrob. But they do not align with the plots (I draw arrows where the numbers should be). Also there is a large white space below which I don't want.
I will also need an analogue for the y-axis.
library(ggplot2)
library(gridExtra)
library(ggpubr)
library(grid)
# Create a grid with several ggplots
p <-
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
theme_transparent() +
theme(plot.background = element_rect(color = "black"))
main.plot <- arrangeGrob(p, p, p, p, p, p, p, p, ncol = 4, nrow = 2)
# grid.draw(main.plot)
# Now add an x axis to the main plot
x.breaks <- c(0, 1, 2.5, 8, 10)
p.axis <- ggplot() +
ylim(-0.1, 0) +
xlim(1, length(x.breaks)) +
ggpubr::theme_transparent()
for (i in seq_along(x.breaks)) {
p.axis <- p.axis +
geom_text(aes_(x = i, y = -0.01, label = as.character(x.breaks[i])), color = "red")
}
# p.axis
final.plot <- arrangeGrob(main.plot, p.axis, nrow = 2)
grid.draw(final.plot)
Any help appreciated.
Note: In the code below, I assume each plot in your grid has equal width / height, & used equally spaced label positions. If that's not the case, you'll have to adjust the positions yourself.
Adding x-axis to main.plot:
library(gtable)
# create additional row below main plot
# height may vary, depending on your actual plot dimensions
main.plot.x <- gtable_add_rows(main.plot, heights = unit(20, "points"))
# optional: check results to verify position of the new row
dev.off(); gtable_show_layout(main.plot.x)
# create x-axis labels as a text grob
x.axis.grob <- textGrob(label = x.breaks,
x = unit(seq(0, 1, length.out = length(x.breaks)), "npc"),
y = unit(0.75, "npc"),
just = "top")
# insert text grob
main.plot.x <- gtable_add_grob(main.plot.x,
x.axis.grob,
t = nrow(main.plot.x),
l = 1,
r = ncol(main.plot.x),
clip = "off")
# check results
dev.off(); grid.draw(main.plot.x)
You can do the same for the y-axis:
# create additional col
main.plot.xy <- gtable_add_cols(main.plot.x, widths = unit(20, "points"), pos = 0)
# create y-axis labels as a text grob
y.breaks <- c("a", "b", "c") # placeholder, since this wasn't specified in the question
y.axis.grob <- textGrob(label = y.breaks,
x = unit(0.75, "npc"),
y = unit(seq(0, 1, length.out = length(y.breaks)), "npc"),
just = "right")
# add text grob into main plot's gtable
main.plot.xy <- gtable_add_grob(main.plot.xy,
y.axis.grob,
t = 1,
l = 1,
b = nrow(main.plot.xy) - 1,
clip = "off")
# check results
dev.off(); grid.draw(main.plot.xy)
(Note that the above order of x-axis followed by y-axis should not be switched blindly. If you are adding rows / columns, it's good habit to use gtable_show_layout() frequently to check the latest gtable object dimensions, & ensure that you are inserting new grobs into the right cells.)
Finally, let's add some buffer on all sides, so that the labels & plot borders don't get cut off:
final.plot <- gtable_add_padding(main.plot.xy,
padding = unit(20, "points"))
dev.off(); grid.draw(final.plot)
I have the below figure shows cdf. I am wondering how can I zoom in to show better the difference between four lines in the left upper part of the figure.
You can use coord_cartesian to zoom in. I don't know what you mean by having the zoom part and the whole in the same figure. If you want to have them side by side, you can use the multiplot function found in the Cookbook for R page. For example:
df <- data.frame(x = c(rnorm(100, 0, 3), rnorm(100, 0, 10)),
g = gl(2, 100))
p <- ggplot(df, aes(x, colour = g)) + stat_ecdf()
p1 <- p
p2 <- p + coord_cartesian(ylim = c(.75, 1))
multiplot(p1, p2)
Edit
Based on #Paul Lemmens' comment, you can use grid's viewport function in the following way:
pdf("~/Desktop/foo.pdf", width = 6, height = 6)
subvp <- viewport(width = .4, height = .4, x = .75, y = .25)
p1
print(p2, vp = subvp)
dev.off()
which gives the following output - adjust the details for your specific example: