简体   繁体   English

如何使用position_nudge将x轴标签与ggplot2中条的中心对齐

[英]How to use position_nudge to align x-axis labels with center of bars in ggplot2

In my original plot, the bar nearest to the y-axis was overlapping with the y-axis. 在我的原始图中,最靠近y轴的条与y轴重叠。 I used position_nudge which did shift the bar, but now I cannot align the x-axis labels with the newly positioned bars. 我使用position_nudge确实移动了条形,但是现在我无法将x轴标签与新放置的条形对齐。 Picture of misaligned x-axis labels x轴标签未对齐的图片

# data frame
AV_sum <- data.frame(condition = c( "Braced", "Unbraced"), 
                     AvgVelocity = c(-1, -3), 
                     se = c( 0.5, 0.3)
                     )

p <- ggplot(AV_sum, aes(x =condition, y = abs(AvgVelocity), fill = condition)) + 
       ggtitle("Average Velocity Between Conditions")  + 
       geom_bar(position=position_nudge(0.5), colour = "black", stat="identity") + 
       geom_errorbar(aes(ymin=abs(AvgVelocity), ymax = abs(AvgVelocity)+se),
         width = 0.1,  colour = "black", position = position_nudge(0.5)) + 
       theme_classic() + xlab("Condition") + 
       ylab("Average Velocity") + 
       theme(plot.title = element_text(colour = 
         "black", size = 18, face = "bold", hjust = 0.5), legend.position= 
         "none", axis.text = element_text(colour = "black", size= 12, face = 
         "bold"), axis.title = element_text(size = 14, face = "bold"))

p +  
  scale_fill_manual(values = c( "white", "black")) + 
  coord_cartesian(ylim = c(0, 5), expand = FALSE) + 
  labs(fill = ("")) + 
  scale_y_continuous(breaks = seq(0, 5, 0.5)) + 
  geom_text(x = 2.0, y = 4, label = "***", size = 12)

I'm afraid that you're overly complicating things with the nudging. 恐怕您的操作会使事情变得过于复杂。 The reason that one of your bars was colliding with the y axis is because of the expand = FALSE in coord_cartesian() . 您的一根杆与y轴发生碰撞的原因是因为coord_cartesian()中的expand = FALSE

My best guess here, is that you wanted to prevent the awkward gap between the bars and the x-axis. 我在这里的最佳猜测是,您想避免条形图和x轴之间的尴尬间隙。 If that is the case than I would recommend tweaking the expand argument inside scale_y_continuous() . 如果是这种情况,那么我建议调整scale_y_continuous()内部的expand参数。

I've rewritten your code below to what I think you're after: 我将下面的代码重写为我认为的代码:

ggplot(AV_sum, aes(x =condition, y = abs(AvgVelocity), fill = condition)) + 
  ggtitle("Average Velocity Between Conditions")  + 
  # Deleted nudges, use geom_col, which is equivalent to geom_bar(stat = "identity")
  geom_col(colour = "black") + 
  geom_errorbar(aes(ymin=abs(AvgVelocity), ymax = abs(AvgVelocity)+se),
                width = 0.1,  colour = "black") + 
  geom_text(x = 1.5, y = 4, label = "***", size = 12) +
  # Moved expand from coord_cartesian to scale_y_condintuous
  coord_cartesian(ylim = c(0, 5)) +
  scale_x_discrete(name = "Condition") + 
  scale_y_continuous(breaks = seq(0, 5, 0.5), expand = c(0,0), name = "Average Velocity") + 
  scale_fill_manual(values = c( "white", "black")) + 
  theme_classic() + 
  theme(
    plot.title = element_text(colour = "black", size = 18, face = "bold", hjust = 0.5), 
    legend.position = "none", 
    axis.text = element_text(colour = "black", size= 12, face = "bold"), 
    axis.title = element_text(size = 14, face = "bold")
  )

在此处输入图片说明

With regards to Tjebo's comment, I generally find it easiest to construct ggplot code when keeping together functions that do similar things, ie group geoms together, group coordinates & (position) scales, and group theme adjustments. 关于Tjebo的评论,我通常认为将功能相似的函数放在一起(例如将几何组在一起,组坐标和(位置)比例以及组主题调整)组合在一起时,构造ggplot代码最容易。 Since theme arguments can get bulky sometimes, I recommend giving each argument it's own line. 由于主题参数有时会变得笨拙,因此我建议给每个参数自己一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM