How to make geom_text plot within the canvas's bounds
ggplot 2.0.0 introduced new options for hjust
and vjust
for geom_text()
that may help with clipping, especially "inward"
. We could do:
ggplot(data=df, aes(x=n.wheels, y=utility, label=word)) +
geom_text(vjust="inward",hjust="inward")
ggplot - Keep a variable length text inside canvas
I ended up finding a quite simple way of doing that using the parameter hjust=1
.
Also, I kept the x
value for geom_text
with its maximum value, that is, max(df1$x)
.
Below, I'm plotting three texts in which all of them are aligned to the right:
require(ggplot2)
df1 = data.frame(x=1:100, y=rnorm(100))
p = ggplot(df1, aes(x=x, y=y)) + geom_point()
p = p + geom_hline(yintercept = c(-3, -2, 2))
variableLabelSize = "Variable length size text"
p = p + geom_text(x=max(df1$x), y=2, label=variableLabelSize, hjust=1) # added hjust=1
variableLabelSize = "Short text"
p = p + geom_text(x=max(df1$x), y=-3, label=variableLabelSize, hjust=1) # added hjust=1
variableLabelSize = "Very very very very very very long text"
p = p + geom_text(x=max(df1$x), y=-2, label=variableLabelSize, hjust=1) # added hjust=1
p
how to avoid geom_text from being cut off
library(tidyverse)
data <- structure(list(Date = structure(c(18779, 18809, 18840, 18871,
18901, 18932, 18779, 18809, 18840, 18871, 18901, 18932, 18779,
18809, 18840, 18871, 18901, 18932), class = "Date"), Key = c("Category 1",
"Category 1", "Category 1", "Category 1", "Category 1", "Category 1",
"Category 2", "Category 2", "Category 2", "Category 2", "Category 2",
"Category 2", "Category 3", "Category 3", "Category 3", "Category 3",
"Category 3", "Category 3"), Value = c(2.8, 3, 2, 2.7, 2.7, 2.8,
1, 2, -1.2, 0.3, 0.6, 0.8, 4, 3.7, 4.4, 4.4, 4.2, 4.2)), row.names = c(NA,
-18L), class = c("tbl_df", "tbl", "data.frame"))
fvjust = function(data) data %>%
mutate(vjust = ifelse(Value>mean(Value),2, -2))
df = data %>% group_by(Key) %>%
group_modify(~fvjust(.x))
df %>% ggplot(aes(Date, Value, col = Key)) +
geom_line(size = 2) +
facet_wrap(~Key, scales = 'free_y', nrow = 3) +
guides(col='none', linetype='none') +
geom_text(aes(label = paste0(sprintf('%0.1f', round(Value, digits = 2)), '%')), size = 5,
vjust = df$vjust)
How to fit custom long annotations geom_text inside plot area for a Donuts plot?
If I understand correctly, you wish to shrink the donut relative to the entire plot area, so that there's more space to fit long annotations? I got the following to work based on the sample data. You may wish to tweak the parameters for your actual use case:
library(dplyr); library(stringr)
ggplot(df %>%
mutate(label2 = str_wrap(label2, width = 10)), #change width to adjust width of annotations
aes(x="", y=n, fill=group)) +
geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) +
expand_limits(x = c(2, 4)) + #change x-axis range limits here
# no change to theme
theme(axis.title=element_blank(),axis.text=element_blank(),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid=element_blank(),
axis.ticks.length=unit(0,"cm"),axis.ticks.margin=unit(0,"cm"),
legend.position="none",panel.spacing=unit(0,"lines"),
plot.margin=unit(c(0,0,0,0),"lines"),complete=TRUE) +
geom_text(aes_string(label="label2",x="3",y="ypos",hjust="hjust")) +
coord_polar("y", start=0) +
scale_x_discrete()
how to increase date range of a plot to make room for geom_text at the end of series?
Use :
scale_x_date(limits = c(as.Date("2021-03-01"),max(rtpcr_test_daily_pct$Updated.On) + 15))
complete code -
library(tidyverse)
rtpcr_test_daily_pct %>%
filter(!is.na(pct_rtpcr),
pct_rtpcr > 0 ) %>%
ggplot(aes(x = Updated.On,
y = pct_rtpcr,
col = State)
) +
geom_line(size = 1) +
geom_text(data = rtpcr_test_daily_pct %>%
filter(Updated.On == max(Updated.On)-1),
aes(label = State,
x = Updated.On ,
y = pct_rtpcr ),
vjust = -1,
size = 3) +
scale_y_continuous(labels = percent,
breaks = seq(.1,1, by = .1)) +
expand_limits(y = .1 ) + #
scale_x_date(limits = c(as.Date("2021-03-01"),max(rtpcr_test_daily_pct$Updated.On) + 15)) +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "% RTPCR testing Between Karnataka & Delhi- Mar'21 onwards") +
coord_equal(ratio = 70)
Related Topics
Adding Space Between Bars in Ggplot2
Subtract a Column in a Dataframe from Many Columns in R
Drop-Down Checkbox Input in Shiny
Grouped Barplot in R with Error Bars
Apply a Function to Every Row of a Matrix or a Data Frame
How to Plot with a Png as Background
Ggplot2 Pie and Donut Chart on Same Plot
How to Initialize Empty Data Frame (Lot of Columns at the Same Time) in R
Using a Pre-Defined Color Palette in Ggplot
Tidyverse Pivot_Longer Several Sets of Columns, But Avoid Intermediate Mutate_Wider Steps
Automatically Delete Files/Folders
Alignment of Numbers on the Individual Bars
Shiny: Differencebetween Observeevent and Eventreactive
Efficiently Sum Across Multiple Columns in R
Why Is Using Update on a Lm Inside a Grouped Data.Table Losing Its Model Data