当我使用geom_text时,如何阻止boxplot向右移动?

时间:2018-05-01 19:17:52

标签: r ggplot2 boxplot

我正在使用boxplot创建一个ggplot,一切正常,直到我使用geom_text向水平线添加标签。

这是我在没有geom_text的情况下使用的代码以及相应的输出:

ggplot(data = x_frame, aes(x = '', y = x_frame)) +
  scale_y_log10(labels = comma, name='Transaction (GBP)') +
  geom_boxplot() +
  ggtitle('Box & Whisker')+
  geom_hline(aes(yintercept = SE, colour = 'Tukey'), linetype='dashed') +
  geom_hline(aes(yintercept = mean(x),colour = 'Mean'),linetype='dashed') +
  geom_hline(aes(yintercept = mean_std,colour = 'Mean & 1Std'),linetype='dashed')+
  scale_colour_manual(name = 'Thresholds', values=c(Tukey='red', Mean='blue', 'Mean & 1Std'='darkred'))+
  guides(colour = guide_legend(override.aes = list(shape = c('Tukey','Mean','Mean & 1Std'), size = 3)))

Without<code>geom_text</code>

以下是使用geom_text和输出后的代码:

ggplot(data = x_frame, aes(x = '', y = x_frame)) +
  scale_y_log10(labels = comma, name='Transaction (GBP)') +
  geom_boxplot() +
  ggtitle('Box & Whisker')+
  geom_hline(aes(yintercept = SE, colour = 'Tukey'), linetype='dashed') +
  geom_hline(aes(yintercept = mean(x),colour = 'Mean'),linetype='dashed') +
  geom_hline(aes(yintercept = mean_std,colour = 'Mean & 1Std'),linetype='dashed')+
  scale_colour_manual(name = 'Thresholds', values=c(Tukey='red', Mean='blue', 'Mean & 1Std'='darkred'))+
  geom_text(aes(y=SE, label=prettyNum(round(SE),big.mark=","), x=0.1), colour='red', hjust=-0.1 , vjust = 1) +
  geom_text(aes(y=mean(x), label=prettyNum(round(mean(x)),big.mark=","), x=0.1), colour='blue', hjust=-0.1 , vjust = -0.5 ) +
  geom_text(aes(y=mean_std, label=prettyNum(round(mean_std),big.mark=","), x=0.1), colour='darkred', hjust=-0.1 , vjust = -0.5) +
  guides(colour = guide_legend(override.aes = list(shape = c('Tukey','Mean','Mean & 1Std'), size = 3)))

with <code>geom_text</code>

对不起,我不能发布数据,但只是它是一个高标准偏差的数字向量(因此,为什么我使用日志转换)。

非常感谢

0 个答案:

没有答案