The scale bar shifts to the left and outside of the data frame when setting the X position to 0 cm and when the anchor point is set to the middle and right positions.
This is working as expected. When setting the X position to zero and selecting the middle anchor point for the placement of scale bar, the scale bar middle anchor point is placed at X = 0 position. This is the reason half of the scale bar is outside of the layout frame.