GGPLOT2: Adjust Font Size of Plot Title - r

I have This ggplot2 I made up to satisfy these conditions:
Remove default background that is hash colour to be plain.
Make (a) to be the plot title located within the plot area that is not close to the line (automatically).
Make $\phi = .8$ to be automatically at the head of the line (still within the plot area).
And sd = 1 to be automatically at the tail of the line.
The four(4) Borderlines to be present.
Gridlines to be a grey colour.
.
## simulate ARIMA(1, 0, 0)
set.seed(799837)
ts <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 10)
gplot <- ggplot(NULL, aes(y = ts, x = seq_along(ts))) +
geom_line(color = "#F2AA4CFF") +
geom_point(color = "#101820FF") +
annotate("text", x = mean(seq_along(ts)), y = max(ts) * 1.1, label = "(a)")+
annotate("text", x = min(seq_along(ts)), y = max(ts) * 1.1, label = 'paste(~phi~"=.8")', parse = TRUE )+
annotate("text", x= max(seq_along(ts)), y = ts[[max(seq_along(ts))]] * 1.1, label = "sd=1") +
xlab('Time') +
ylab('Series') +
theme_bw() +
theme(axis.text = element_text(size = 40, angle = 0, vjust = 0.0, hjust = 0.0), #y-axis label size
axis.title = element_text(size = 40), #x-axis label size
axis.title.x = element_text(angle = 0, hjust = 0.5, vjust = 0.5, size = 40), # x-axis title
axis.title.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5, size = 40), # y-axis title
plot.title = element_text(size = 40, margin = margin(t = 25, b = -20, l = 0, r = 0)),
panel.background = element_blank()) +
scale_x_continuous(breaks = seq(1,10,2)) +
scale_y_continuous(expand = c(0.0, 0.00))
gplot
I want the font of the plot title to increase. As you can see that despite setting the font of the plot title to 40 the font title refuse to increase. This question is a follow-up question from Remove Default Background Color and Make Title in Plot Zone

Daniel
If (a) is the 'title' it's not really the title, it's an annotation.
So to change it's size do it when you add the annotation.
annotate("text", x = mean(seq_along(ts)), y = max(ts) * 1.5, label = "(a)", size = 40)
You might also want to resize the other annotations.
annotate("text", x = mean(seq_along(ts)), y = max(ts) * 1.5, label = "(a)", size = 40) +
annotate("text", x = min(seq_along(ts)), y = max(ts) * 1.5, label = 'paste(~phi~"=.8")', parse = TRUE, size = 10)+
annotate("text", x= max(seq_along(ts)), y = ts[[max(seq_along(ts))]] * 1.5, label = "sd=1", size = 10)

Related

Why does the top line of ggplot disappear when using xlim, ylim and scale_continuous to define breaks

I am using this piece of code to create a series of 4 point plots. I need the plots to go from x1 to x2 and y1 to y2. The length of the axes is always 10 with a single major break halfway and minor breaks at each unit.
When I get the scale and the breaks right I sometimes (not always) lose the top line on the box surrounding the map
The parts of the data set that are involved are
tag: an identifier used to label the points
lx, ly: the coordinates of the plot which range from 0 -20 in the dataset but each map does only a quarter of the area. So x goes from 0 to 10 or 10 to 20 and y goes from 0 to 10 or 10 to 20.
I tried this piece of code. I expect a point plot surrounded by a box
n ranges from 1:4
x1 = c(0, 10, 10, 0)
x2 = c(10, 20, 20, 10)
y1 = c(0, 0, 10, 10)
y2 = c(10, 10, 20, 20)
theme_set(theme_bw())
ggplot(onemap, aes(x = onemap$lx, y = onemap$ly)) + geom_point(size =
.3) +
xlim(c(x1[n], x2[n])) + ylim(c(y1[n], y2[n])) +
# coord_cartesian(expand = FALSE)
# scale_x_continuous(expand = c(0, 0), limits = c(0, NA)) +
scale_y_continuous(breaks = seq(y1[n], y2[n], 5),
minor_breaks = seq(y1[n], y2[n], 1)) +
scale_x_continuous(breaks = seq(x1[n], x2[n], 5),
minor_breaks = seq(x1[n], x2[n], 1)) +
labs(
x = element_blank(),
y = element_blank(),
title = hd,
subtitle = subheads
) +
theme(plot.title.position = "plot") +
theme(
plot.title = element_text(
size = 14,
face = "bold",
margin = margin(8, 0, 8, 0)
),
plot.subtitle = element_text(size = 10),
plot.margin = margin(
t = 1,
r = 2,
b = 1.5,
l = 2,
unit = "cm"
),
axis.ticks = element_blank(),
axis.text = element_text(size = 6),
axis.text.y = element_text(angle = 90)
) +
theme(
panel.grid.major = element_line(color = "gray30", linewidth = .25),
panel.grid.minor = element_line(
color = "gray30",
linewidth = .25,
linetype = "dashed"
),
panel.border = element_blank()
) +
geom_text_repel(
aes(label = tag),
box.padding = 0.01,
size = 2,
xlim = c(-Inf, Inf),
ylim = c(-Inf, Inf),
max.overlaps = 45,
segment.size = 2,
segment.color = "grey"
)
I worked out the problem. I should not have used xlim and ylim in the main body of ggplot. I should have used the limits in scale_x_continuous and scale_y_continuous as below.
scale_y_continuous(breaks = seq(y1[n], y2[n], 5), minor_breaks = seq(y1[n], y2[n], 1),limits=c(y1[n], y2[n])) +
scale_x_continuous(breaks = seq(x1[n], x2[n], 5), minor_breaks = seq(x1[n], x2[n], 1),limits=c(x1[n], x2[n])) +

How can I create a graph like this with ggplot in r?

Is this even possible with ggplot?
(https://i.stack.imgur.com/wCyZN.png)
I tried geom_point(), but I don't think it can work. I also can't find the semi-circle shape.
One option would be to use ggforce::geom_arc_bar to draw the half circles for which I set sep=pi. The rest is a lot of fiddling to put the labels at the right positions.
library(ggplot2)
library(ggforce)
library(showtext)
#> Loading required package: sysfonts
#> Loading required package: showtextdb
showtext_auto()
font_add_google("Roboto Condensed", "roboto")
dat <- data.frame(
x = 1:5,
r = c(.143, .321, .176, .129, -.2)
)
col <- "grey75"
scale <- .75
fontsize <- 10
ggplot(dat) +
geom_arc_bar(
aes(
x0 = x, y0 = 0, r0 = 0, r = -scale * sign(r) * sqrt(abs(r)),
amount = 1
),
stat = "pie", sep = pi, fill = col
) +
geom_hline(yintercept = 0, color = col) +
geom_text(
aes(x = x, y = sign(r) * .1, label = scales::percent(r)),
color = "white", size = .8 + fontsize / .pt, family = "roboto"
) +
annotation_custom(
grob = grid::textGrob(
label = "Price difference %",
x = unit(-20, "pt"),
y = unit(.9, "npc"),
gp = grid::gpar(col = "white", fontsize = fontsize, fontfamily = "roboto"),
hjust = 0, vjust = 0
)
) +
annotation_custom(
grob = grid::textGrob(
label = c("% that U.S. is paying more", "% that U.S. is paying less"),
x = unit(-20, "pt"),
y = unit(.5, "npc") + unit(c(.37, -.25), "npc"),
gp = grid::gpar(col = col, fontsize = fontsize * .8, fontfamily = "roboto"),
hjust = 0, vjust = 1
)
) +
scale_y_continuous(expand = c(.2, 0, .2, 0)) +
scale_x_continuous(expand = c(0.01, 0.01)) +
coord_fixed(clip = "off") +
theme_void() +
theme(
plot.background = element_rect(fill = "black"),
plot.margin = margin(0, 11, 0, 11, "pt"),
axis.ticks.length.y.left = unit(20, "pt")
)

Drawing a 3 axis graph in ggplot2 with r

https://imgur.com/a/fYGgpu7
I am trying to recreate a graph such as these below in ggplot2. I have been looking for 3 axis and seem to be getting 3d visualizations which is clearly not what I need so my googlefu has let me down.
My data is very simple:
https://imgur.com/a/eM8tKAG
With the CTD column determining a different graph.
I guess my main issue is the scaling of the axis. My y axis is Depth. It is common to inverse this value as it places the surface of the water at the top.
It's considered suboptimal by many (myself included) to have even 2 axes on the same dimension of a plot. There are simply better ways of presenting the same information. It is possible to have a second axis in ggplot, but if you want a third you'll have to create it yourself:
ggplot(df, aes(Salinity, Depth)) +
geom_line(color = "green4") +
geom_line(aes(x = (Temperature - 8.25) * 15 + 29), color = "red3") +
geom_line(aes(x = Silicate * 0.3 + 24.5), col = "deepskyblue3") +
annotate(geom = "text", x = seq(15, 27, 3) * 0.3 + 24.5, y = -0.5,
label = seq(15, 27, 3), col = "deepskyblue3", size = 3.1) +
annotate(geom = "text", color = "deepskyblue3", x=30.87, y = -1.5,
label = "Silicates") +
geom_hline(yintercept = 0, color = "deepskyblue3") +
scale_x_continuous(sec.axis = sec_axis(name = "Temperature",
trans = function(x) {
(x - 28) / 15 + 8.25
})) +
coord_cartesian(ylim = c(-2, 20), clip = "off") +
theme_minimal() +
theme(plot.margin = margin(10, 10, 50, 10),
axis.text.x.top = element_text(colour = "red3"),
axis.title.x.top = element_text(colour = "red3"),
axis.ticks.x.top = element_line(color = "red3"),
axis.line.x.top = element_line(color = "red3"),
axis.text.x.bottom = element_text(colour = "green4"),
axis.title.x.bottom = element_text(colour = "green4"),
axis.ticks.x.bottom = element_line(color = "green4"),
axis.line.x.bottom = element_line(colour = "green4"),
axis.line.y.left = element_line())
Data transcribed from OP
df <- data.frame(CTD = c(1, 1, 1, 2, 2),
Depth = c(17.78, 3.89, 1.44, 13.23, 1.34),
Temperature = c(8.28, 8.31, 8.49, 8.25, 8.31),
Salinity = c(31.9, 31.39, 30.45, 32.61, 29.11),
Silicate = c (17.19643, 19.51786, 24.07143, 14.78571, 27.64286))

Adding labels to this ggridge plot

I can't see, to figure out how to add labels to this plot:
ggplot(input_cleaned, aes(x =DAYS_TO_FA, y = fct_rev(DATE_TEXT), group = fct_rev(DATE_TEXT))) +
geom_density_ridges2(stat="binline", bins = 75, scale = 0.95, draw_baseline = FALSE) +
labs(title = 'Monthly Plots of Time to First Nose Pickin', y='Month Tracked', x = 'Days to First Pickin Action') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,130,5)) +
geom_segment(aes(x=50, xend = 50, y=1,yend=5),
linetype = "dashed", size = 1.5,
color = "black") +
geom_segment(aes(x=75, xend = 75, y=5,yend=30),
linetype = "dashed", size = 1.5,
color = "black")
which produces:
I have tried this:
ggplot(input_cleaned, aes(x =DAYS_TO_FA, y = fct_rev(DATE_TEXT), group = fct_rev(DATE_TEXT))) +
geom_density_ridges2(stat="binline", bins = 75, scale = 0.95, draw_baseline = FALSE) +
geom_text(stat = "bin",
aes(y = fct_rev(input_cleaned$DATE_TEXT) + 0.95*(..count../max(..count..)), label = ifelse(..count..>0, ..count.., "")),
vjust = 1.4, size = 3, color = "white", binwidth = 1) +
labs(title = 'Monthly Plots of Time to First Nose Pickin', y='Month Tracked', x = 'Days to First Pickin Action') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,130,5)) +
geom_segment(aes(x=50, xend = 50, y=1,yend=5),
linetype = "dashed", size = 1.5,
color = "black") +
geom_segment(aes(x=75, xend = 75, y=5,yend=30),
linetype = "dashed", size = 1.5,
color = "black")
based on the example found here:
Visualization of Groups of Poisson random samples using ggridges
but I can't get it to work. Nothing changes.
I know it may not be a good idea for this graph, but I am interested in seeing how it looks and more or less learning how to apply it.

Exact Positioning of multiple plots in ggplot2 with grid.arrange

I'm trying to create a multiple plot with the same x-axis but different y-axes, because I have values for two groups with different ranges. As I want to control the values of the axes (respectively the y-axes shall reach from 2.000.000 to 4.000.000 and from 250.000 to 500.000), I don't get along with facet_grid with scales = "free".
So what I've tried is to create two plots (named "plots.treat" and "plot.control") and combine them with grid.arrange and arrangeGrob. My problem is, that I don't know how to control the exact position of the two plots, so that both y-axes are positioned on one vertical line. So in the example below the second plot's y-axis needs to be positioned a bit more to the right.
Here is the code:
# Load Packages
library(ggplot2)
library(grid)
library(gridExtra)
# Create Data
data.treat <- data.frame(seq(2005.5, 2015.5, 1), rep("SIFI", 11),
c(2230773, 2287162, 2326435, 2553602, 2829325, 3372657, 3512437,
3533884, 3519026, 3566553, 3527153))
colnames(data.treat) <- c("Jahr", "treatment",
"Aggregierte Depositen (in Tausend US$)")
data.control <- data.frame(seq(2005.5, 2015.5, 1), rep("Nicht-SIFI", 11),
c(324582, 345245, 364592, 360006, 363677, 384674, 369007,
343893, 333370, 318409, 313853))
colnames(data.control) <- c("Jahr", "treatment",
"Aggregierte Depositen (in Tausend US$)")
# Create Plot for data.treat
plot.treat <- ggplot() +
geom_line(data = data.treat,
aes(x = `Jahr`,
y = `Aggregierte Depositen (in Tausend US$)`),
size = 1,
linetype = "dashed") +
geom_point(data = data.treat,
aes(x = `Jahr`,
y = `Aggregierte Depositen (in Tausend US$)`),
fill = "white",
size = 2,
shape = 24) +
scale_x_continuous(breaks = seq(2005, 2015.5, 1),
minor_breaks = seq(2005, 2015.5, 0.5),
limits = c(2005, 2015.8),
expand = c(0.01, 0.01)) +
scale_y_continuous(breaks = seq(2000000, 4000000, 500000),
minor_breaks = seq(2000000, 4000000, 250000),
labels = c("2.000.000", "2.500.000", "3.000.000",
"3.500.000", "4.000.000"),
limits = c(2000000, 4000000),
expand = c(0, 0.01)) +
theme(text = element_text(family = "Times"),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line.x = element_line(color="black", size = 0.6),
axis.line.y = element_line(color="black", size = 0.6),
legend.position = "none") +
geom_segment(aes(x = c(2008.7068),
y = c(2000000),
xend = c(2008.7068),
yend = c(3750000)),
linetype = "dotted") +
annotate(geom = "text", x = 2008.7068, y = 3875000, label = "Lehman\nBrothers + TARP",
colour = "black", size = 3, family = "Times") +
geom_segment(aes(x = c(2010.5507),
y = c(2000000),
xend = c(2010.5507),
yend = c(3750000)),
linetype = "dotted") +
annotate(geom = "text", x = 2010.5507, y = 3875000, label = "Dodd-Frank-\nAct",
colour = "black", size = 3, family = "Times") +
geom_rect(aes(xmin = 2007.6027, xmax = 2009.5, ymin = -Inf, ymax = Inf),
fill="dark grey", alpha = 0.2)
# Create Plot for data.control
plot.control <- ggplot() +
geom_line(data = data.control,
aes(x = `Jahr`,
y = `Aggregierte Depositen (in Tausend US$)`),
size = 1,
linetype = "solid") +
geom_point(data = data.control,
aes(x = `Jahr`,
y = `Aggregierte Depositen (in Tausend US$)`),
fill = "white",
size = 2,
shape = 21) +
scale_x_continuous(breaks = seq(2005, 2015.5, 1), # x-Achse
minor_breaks = seq(2005, 2015.5, 0.5),
limits = c(2005, 2015.8),
expand = c(0.01, 0.01)) +
scale_y_continuous(breaks = seq(250000, 500000, 50000),
minor_breaks = seq(250000, 500000, 25000),
labels = c("250.000", "300.000", "350.000", "400.000",
"450.000", "500.000"),
limits = c(250000, 500000),
expand = c(0, 0.01)) +
theme(text = element_text(family = "Times"),
axis.title.x = element_blank(), # Achse
axis.title.y = element_blank(), # Achse
axis.line.x = element_line(color="black", size = 0.6),
axis.line.y = element_line(color="black", size = 0.6),
legend.position = "none") +
geom_segment(aes(x = c(2008.7068),
y = c(250000),
xend = c(2008.7068),
yend = c(468750)),
linetype = "dotted") +
annotate(geom = "text", x = 2008.7068, y = 484375, label = "Lehman\nBrothers + TARP",
colour = "black", size = 3, family = "Times") +
geom_segment(aes(x = c(2010.5507),
y = c(250000),
xend = c(2010.5507),
yend = c(468750)),
linetype = "dotted") +
annotate(geom = "text", x = 2010.5507, y = 484375, label = "Dodd-Frank-\nAct",
colour = "black", size = 3, family = "Times") +
geom_rect(aes(xmin = 2007.6027, xmax = 2009.5, ymin = -Inf, ymax = Inf),
fill="dark grey", alpha = 0.2)
# Combine both Plots with grid.arrange
grid.arrange(arrangeGrob(plot.treat, plot.control,
ncol = 1,
left = textGrob("Aggregierte Depositen (in Tausend US$)",
rot = 90,
vjust = 1,
gp = gpar(fontfamily = "Times",
size = 12,
colout = "black",
fontface = "bold")),
bottom = textGrob("Jahr",
vjust = 0.1,
hjust = 0.2,
gp = gpar(fontfamily = "Times",
size = 12,
colout = "black",
fontface = "bold"))))
Do:
install.packages("cowplot")
but do not library(cowplot) as it'll mess up your theme work.
Then, do:
grid.arrange(
arrangeGrob(cowplot::plot_grid(plot.treat, plot.control, align = "v", ncol=1),
ncol = 1,
left = textGrob("Aggregierte Depositen (in Tausend US$)",
rot = 90,
vjust = 1,
gp = gpar(fontfamily = "Times",
size = 12,
colout = "black",
fontface = "bold")),
bottom = textGrob("Jahr",
vjust = 0.1,
hjust = 0.2,
gp = gpar(fontfamily = "Times",
size = 12,
colout = "black",
fontface = "bold"))))

Resources