2

希望有人可以帮助我解决这个问题。我尝试了几种解决方案,但均未成功。

我想复制下图#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()
4

1 回答 1

1

如果您已指定时钟值,则可以轻松计算方向,从而计算您想要在每个方向微移的量:

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())
于 2016-10-17T11:17:23.237 回答