我想自动调整图表中值的位置。请考虑以下事项:
如您所见,这些值与该行重叠。我该如何避免这种情况?当然,我可以手动更改所有值的位置,但调整所有值将非常困难。
我的代码是:
library(ggplot2)
library(scales)
df <- data.frame(years=c(1989, 1991, 1993, 1997, 2001, 2005, 2007, 2011, 2015),
freq=c(62.70, 43.20, 52.13, 47.93, 46.29, 40.57, 53.88, 48.92, 50.92))
df$years <- as.numeric(as.character(df$years))
point <- format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)
p <- (ggplot(df, aes(x=years, y=freq, label=point(round(freq,1)))) +
geom_line(size=.7, color="#999999") +
geom_point(size=3, color="black") +
geom_text(vjust=c(2, -1, -1.5*sign(diff(diff(df$freq))) + 0.5)) +
theme_bw() +
theme(panel.border=element_blank(), panel.grid.minor=element_blank(),
axis.text.y=element_blank(), axis.ticks=element_blank(), axis.line.y=element_blank()) +
scale_x_continuous("", breaks=df$years, minor_breaks=NULL) +
scale_y_continuous("", limits=c(0, 65),
breaks=seq(0, 65, 10), minor_breaks=NULL))
p