Reputation: 857
I plot the estimates "0.548" and "0.604" using the following code:
df %>%
mutate(label = replace(round(est, 3), x != -6 & x != 12, '')) %>%
ggplot(aes(x,est, label = label)) +
geom_text(hjust = 1.2, size=5) +
geom_line(, size= 1.5) +
coord_cartesian(xlim = c(-8, 14))+
theme_classic()+
theme()
But my aim is to move the 0.604 position to the right side of the line while keeping the 0.548 to its left. Is there any way to do it?
Here is the data:
structure(list(x = c(-6, -3, 0, 3, 6, 9, 12), group = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = "Working class", class = "factor"),
est = c(0.547944792197329, 0.55727970599782, 0.566614619798308,
0.575949533598797, 0.585284447399287, 0.594619361199776,
0.603954275000266)), row.names = c(NA, -7L), groups = structure(list(
x = c(-6, -3, 0, 3, 6, 9, 12), .rows = structure(list(1L,
2L, 3L, 4L, 5L, 6L, 7L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 7L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Upvotes: 1
Views: 46
Reputation: 12699
There's bound to be a more elegant way of doing this, here is one option.
library(dplyr)
library(ggplot2)
ggplot(df, aes(x, est)) +
geom_text(data = filter(df, est == max(df$est)), aes(x, est, label = round(est, 3)), hjust = -0.2, size=5) +
geom_text(data = filter(df, est == min(df$est)), aes(x, est, label = round(est, 3)), hjust = 1.2, size=5) +
geom_line(, size= 1.5) +
coord_cartesian(xlim = c(-8, 14))+
theme_classic()+
theme()
Created on 2021-03-31 by the reprex package (v1.0.0)
Upvotes: 1