使用ggplot2水平移动boxplot bars的位置

时间:2013-08-19 21:29:48

标签: r ggplot2

我需要在图表上水平移动框图中的所有条形图(左或右)。有没有办法在不改变x轴的情况下调整箱形图的x轴位置?

我用来生成此箱图的代码列在下面,

plot <- ggplot(aes(y = SCORE, x = DATE, fill = CATEGORY), data = data_R1000) 
+ geom_boxplot(width=0.8) + ylim(20,100) + labs(title = "US_MARKETOR") 
+ theme(legend.position="bottom") 
+ theme(panel.background = element_rect(fill = "transparent",colour = NA)) 
+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 
+ scale_fill_hue(c=50, l=85)

结果如下enter image description here

我尝试过使用position = position_dodge / position_jitter,但都没有。输出是一个包含多个boxplot条的图,我删除了所有背景和网格。我想将这些条移动到其默认位置的左侧或右侧。

1 个答案:

答案 0 :(得分:0)

coord_flip()添加到您的代码中:

plot <- ggplot(aes(y = SCORE, x = DATE, fill = CATEGORY), data = data_R1000) 
+ geom_boxplot(width=0.8) + ylim(20,100) + labs(title = "US_MARKETOR") 
+ theme(legend.position="bottom") 
+ theme(panel.background = element_rect(fill = "transparent",colour = NA)) 
+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 
+ scale_fill_hue(c=50, l=85) 
+ coord_flip()