如何根据时钟值对齐geom_text
How to align geom_text according to clock values
希望有人能帮我解决这个问题。我尝试了几种解决方案但没有成功。
我想复制下图 #1。因此,我将变量时钟(值从 1 到 12)添加到我的数据框中,以将其用作对齐 geom_text.
的参数
我要复制的图:
我的身材:
按照我的绘图代码:
p1 <- ggplot(data, aes(x=tax, y=employment)) + geom_point(colour = ifelse(style == 1 | style == 2,"black","grey")) + geom_smooth(method=lm, se=FALSE)
p1 + coord_cartesian(xlim = c(0, 0.8), ylim = c(0.5,0.9), expand = FALSE) + labs(x = "1 - participation taxrate", y="Employment rate") + geom_text(aes(family = "Times New Roman", label=ifelse(style == 1 | style == 2 ,Country,''))) + theme_bw()
如果您指定了时钟值,您可以轻松计算出方向,从而计算出您想要在每个方向上微调的量:
angle <- clock/12*2*pi
radius <- 0.01
ng <- data.frame(x = radius * sin(angle),
y = radius * cos(angle))
然后你可以只添加position = position_nudge(x = ng$x, y = ng$y)
到geom_text
语句。下面我还删除了您要复制的图中未出现的网格线。
p1 + coord_cartesian(xlim = c(0, 0.8), ylim = c(0.5,0.9), expand = FALSE) +
labs(x = "1 - participation taxrate", y="Employment rate") +
geom_text(aes(family = "Times New Roman", label=ifelse(style == 1 | style == 2 ,Country,'')),
position = position_nudge(x = ng$x, y = ng$y)) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
希望有人能帮我解决这个问题。我尝试了几种解决方案但没有成功。
我想复制下图 #1。因此,我将变量时钟(值从 1 到 12)添加到我的数据框中,以将其用作对齐 geom_text.
的参数我要复制的图:
我的身材:
按照我的绘图代码:
p1 <- ggplot(data, aes(x=tax, y=employment)) + geom_point(colour = ifelse(style == 1 | style == 2,"black","grey")) + geom_smooth(method=lm, se=FALSE)
p1 + coord_cartesian(xlim = c(0, 0.8), ylim = c(0.5,0.9), expand = FALSE) + labs(x = "1 - participation taxrate", y="Employment rate") + geom_text(aes(family = "Times New Roman", label=ifelse(style == 1 | style == 2 ,Country,''))) + theme_bw()
如果您指定了时钟值,您可以轻松计算出方向,从而计算出您想要在每个方向上微调的量:
angle <- clock/12*2*pi
radius <- 0.01
ng <- data.frame(x = radius * sin(angle),
y = radius * cos(angle))
然后你可以只添加position = position_nudge(x = ng$x, y = ng$y)
到geom_text
语句。下面我还删除了您要复制的图中未出现的网格线。
p1 + coord_cartesian(xlim = c(0, 0.8), ylim = c(0.5,0.9), expand = FALSE) +
labs(x = "1 - participation taxrate", y="Employment rate") +
geom_text(aes(family = "Times New Roman", label=ifelse(style == 1 | style == 2 ,Country,'')),
position = position_nudge(x = ng$x, y = ng$y)) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())