Related
I have written the following code to show four plots
Scores <- as.factor(sampleXYPCA$PC1)
p1 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC1)) +
geom_point( ) + scale_color_gradient(low="blue", high="red") +
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) + labs( x ="x (m) ", y = "y (m)") +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank(),
legend.position="right", legend.direction="vertical")
Scores <- as.factor(sampleXYPCA$PC2)
p2 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC2)) +
geom_point( ) + scale_color_gradient(low="blue", high="red") +
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) + labs( x ="x (m) ", y = "y (m)") +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
Scores <- as.factor(sampleXYPCA$PC3)
p3 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC3)) +
geom_point( ) + scale_color_gradient(low="blue", high="red") +
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) + labs( x ="x (m) ", y = "y (m)") +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
Scores <- as.factor(sampleXYPCA$PC4)
p4 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC4)) +
geom_point( ) + scale_color_gradient(low="blue", high="red") +
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) + labs( x ="x (m) ", y = "y (m)") +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
figure <- ggarrange(p1, p2,p3,p4 + font("x.text", size = 10),
ncol = 2, nrow = 2)
show(figure)
I have two issues that I am trying to fix:
I want to remove the values at x-axis at the last plot (PC4), as in the previous plots.
I want to set the same scale at the colour bar for all plots (from -3,3)
For convenience, I copy the first lines of the dataframe (sampleXYPCA) that I am using:
X_UTM_ Y_UTM_ PC1 PC2 PC3 PC4
1 6501395 1885718 -1.37289727 2.320717816 0.93434761 1.24571643
2 6500888 1885073 -1.22111900 4.021127182 1.89434320 1.26801802
3 6500939 1885241 -0.58212873 3.301443355 -1.79458946 0.63329006
4 6500965 1884644 -1.13872381 4.521231473 2.43925215 0.53962882
5 6501608 1884654 -0.24075643 5.871225725 0.69257238 0.89294843
6 6501407 1883939 -0.15938861 3.965081981 1.40970861 -0.77825417
7 6501581 1883630 -0.59187192 2.904278269 0.40655574 -1.66513966
Using facet_wrap and adding an aerial basemap for visualisation (personal prefence when plotting spatial data):
#sample data as dput
dt <- structure(list(x = c(6501395, 6500888, 6500939, 6500965, 6501608,
6501407, 6501581), y = c(1885718, 1885073, 1885241, 1884644,
1884654, 1883939, 1883630), pca1 = c(-1.37289727, -1.221119,
-0.58212873, -1.13872381, -0.24075643, -0.15938861, -0.59187192
), pca2 = c(2.320717816, 4.021127182, 3.301443355, 4.521231473,
5.871225725, 3.965081981, 2.904278269), pca3 = c(0.93434761,
1.8943432, -1.79458946, 2.43925215, 0.69257238, 1.40970861, 0.40655574
), pca4 = c(1.24571643, 1.26801802, 0.63329006, 0.53962882, 0.89294843,
-0.77825417, -1.66513966)), class = "data.frame", row.names = c(NA,
-7L))
#load libraries
library(sf)
library(tidyr)
library(ggplot2)
library(ggspatial)
library(tmaptools)
#pivot_longer on PCA
dt <- pivot_longer(dt, cols = c("pca1", "pca2", "pca3", "pca4"), names_to = "PCA", values_to = "Score")
#convert to sf object (assumed that you use espg:32629, change to whatever you use as the coordinate system)
dt <- st_as_sf(dt, coords = c("x", "y"), crs = st_crs(32629))
#load a basemap
basemap <- read_osm(dt, type = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}", zoom = 15, ext = 1.2)
#plot
ggplot() + layer_spatial(basemap) + geom_sf(data = dt, aes(col = Score), size = 3) + facet_wrap(~PCA) + labs( x ="x (m) ", y = "y (m)") +
theme_bw() + theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank()) +
scale_x_continuous(expand = c(0.01,0.01)) +
scale_y_continuous(expand = c(0.01,0.01)) +
scale_color_gradient(low="blue", high="red")
Using some dummy data to illustrate a possible solution, this may help.
The issue in the OP's question seems to be with the call to ggarrange. Check out the documentation with ?ggarrange
library(ggpubr)
library(ggplot2)
p1 <-
ggplot(mtcars, aes(mpg, wt))+
geom_point() +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
figure <-
ggarrange(p1, p1, p1, p1,
font.label = list(size = 10),
ncol = 2,
nrow = 2)
show(figure)
Created on 2021-12-10 by the reprex package (v2.0.1)
I need to bold the equations in stat_poly_eq() in ggplot2. How do I do this along with atop function?
Also, is there anyway to specify the coordinates of the stat_poly_eq?
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(
formula = my.formula,
aes(label = paste("atop(", ..eq.label.., ",", ..rr.label.., ")")),
label.y = 0.9,
parse = TRUE,
size = 2.5
#, col = "black"
)+
facet_grid(.~z, scales = "free") +
theme_classic()
I don't know if you can; you can underline the equation, e.g.
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(
formula = x ~ y,
aes(label = paste("atop(underline(", ..eq.label.., "),", ..rr.label.., ")")),
label.y = 0.9,
parse = TRUE,
size = 2.5
#, col = "black"
)+
facet_grid(.~z, scales = "free") +
theme_classic()
However, everything I tried (e.g. bold(), bolditalic()) didn't work when combined with atop(). Perhaps it's related to this statement in the docs:
Note that bold, italic and bolditalic do not apply to symbols, and
hence not to the Greek symbols such as mu which are displayed in the
symbol font. They also do not apply to numeric constants.
Even if you "bold" everything with e.g. theme_classic(base_family = "Arial Bold"), the equation and R2 value aren't changed.
--
In terms of positioning the labels, you can move them around with label.x and label.y, e.g. (label.y = 1 puts the label at the top of the plot):
library(tidyverse)
library(ggpmisc)
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(
formula = x ~ y,
aes(label = paste("atop(underline(", ..eq.label.., "),", ..rr.label.., ")")),
label.y = 1,
label.x = 0.2,
parse = TRUE,
size = 2.5
#, col = "black"
)+
facet_grid(.~z, scales = "free") +
theme_classic()
I am creating several plots in order to create frames for a gif. It is supposed to show growing points over time. (see plot 1 and 2 - the values increase). Using size aesthetic is problematic, because the scaling is done for each plot individually.
I tried to set breaks with scale_size_area() to provide a sequence of absolute values, in order to scale on 'all values' rather than only the values present in each plot. (no success).
Plot 3 shows how the points should be scaled, but this scaling should be achieved in each plot.
library(tidyverse)
df1 <- data.frame(x = letters[1:5], y = 1:5, size2 = 21:25)
ggplot(df1, aes(x, y, size = y)) +
geom_point() +
scale_size_area(breaks = seq(0,25,1))
ggplot(df1, aes(x, y, size = size2)) +
geom_point() +
scale_size_area(breaks = seq(0,25,1))
df2 <- data.frame(x = letters[1:5], y = 1:5, size2 = 21:25) %>% gather(key, value, y:size2)
ggplot(df2, aes(x, value, size = value)) +
geom_point() +
scale_size_area(breaks = seq(0,25,1))
Created on 2019-05-12 by the reprex package (v0.2.1)
Pass lower and upper bound to limits argument in scale_size_area function:
ggplot(df1, aes(x, y, size = y)) +
geom_point() +
labs(
title = "Y on y-axis",
size = NULL
) +
scale_size_area(limits = c(0, 25))
ggplot(df1, aes(x, y, size = size2 )) +
geom_point() +
labs(
title = "size2 on y-axis",
size = NULL
) +
scale_size_area(limits = c(0, 25))
How about this?
library("ggplot2")
df1 <- data.frame(x = letters[1:5],
y = 1:5)
ggplot(data = df1,
aes(x = x,
y = y,
size = y)) +
geom_point() +
scale_size_area(breaks = seq(1,25,1),
limits = c(1, 25))
I want to add an annotation outside the plotting area in a faceted ggplot. I can get the annotation that I want, but it's repeated for each facet. How can I get this annotation to appear only once?
E.g., to annotate "XX" once in the top left hand corner I can use:
library("ggplot2")
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
facet_grid(.~cyl ) +
annotate("text", x = -20, y = 36, label = "XX") +
coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")
However this actually annotates it to the top left of each facet.
How can I get this to only appear once?
You can put a single tag label on a graph using tag in labs().
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
facet_grid(.~cyl ) +
labs(tag = "XX") +
coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")
This defaults to "top left", though, which may not be what you want. You can move it around with the theme element plot.tag.position, either as coordinates (between 0 and 1 to be in plot space) or as a string like "topright".
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
facet_grid(.~cyl ) +
labs(tag = "XX") +
coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off") +
theme(plot.tag.position = c(.01, .95))
It's in fact very easy, just have a vector of labels, where the ones you don't want to plot are the empty string "".
library("ggplot2")
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
annotate("text", x = -20, y = 36, label = c("XX", "", "")) +
facet_grid(.~cyl ) +
coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")
Alternatively, the package cowplot has the handy annotation function draw_label(). When used in combination with ggdraw(), can annotate anywhere on the canvas/sheet with the coordinates ranging from 0 to 1 (relative to the entire canvas). The function cowplot::draw_label() uses ggplot2::annotation_custom() under the hood.
library(ggplot2)
library(cowplot)
#> Warning: package 'cowplot' was built under R version 3.5.2
#>
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#>
#> ggsave
# Revert to default theme; see https://stackoverflow.com/a/41096936/5193830
theme_set(theme_grey())
p <- ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
facet_grid(. ~ cyl)
ggdraw(p) + draw_label("XX", x = 0.02, y = 0.97)
Created on 2019-01-14 by the reprex package (v0.2.1)
With geom_text:
dummy <- data.frame(cyl = c(4), l = c("XX"), stringsAsFactors = F)
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_text(data=dummy, aes(label=l), x = -20, y = 36) +
facet_grid(.~cyl ) +
coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")
How to show the point (x=0, y=1500) with a text label next to it on the following histogram?
ggplot(ds_visits, aes(x = patientsInService)) +
geom_histogram(stat = "count", col = "black", fill = "white") +
theme_bw() +
labs(x = "Patients in service", y = "Cases") +
scale_x_discrete(limits = seq(0, 5, 1))
You have to create dummy data.frame for point data:
pointData <- data.frame(X = 0, Y = 1500)
Plot it with with two additional gems (geom_point and geom_text):
ggplot(ds_visits, aes(patientsInService)) +
geom_histogram(stat = "count", col = "black", fill = "white") +
geom_point(data = pointData, aes(X , Y)) +
geom_text(data = pointData, aes(X + 1 , Y + 10, label = "My Text"))
In geom_text I'm changing coordinates a little bit not to overlap text with point.