ggplot facet wrap在y轴上有小数而不是整数

时间:2019-06-03 05:30:46

标签: r ggplot2

在通过小平面换行和自由缩放获得的两个绘图之一中,对于Y轴值,使用小数点代替整数。

我验证了数据框中的数字,它们是正确的。我尝试了方面网格,并得到了同样的事情。我也尝试过scale_y_conitnuos,但是两个图形的数​​字范围都不同。我还尝试了两个图形,并将它们与ggarrange放在一起,但是有两个y_axis标签。我只想要一个共享的y_axis标签。使用下面的代码,除了y_axis的小数点之外,我得到了预期的结果。

   DATA <- tibble(
color = c( "blue","blue","blue","white","white","white","white",
    "white","white","white","red","red","red","red","red",
    "purple","purple","purple","purple","purple","purple",
    "black","black","black","black","black","black"), 
freq  = c(2, 2, 1, 2, 7, 1, 2, 1, 1, 1, 1,13, 6, 3, 1, 3,15,
     1,10, 6, 1, 2,13, 1, 5, 3, 1),
max = c(2, 2, 3, 3, 6, 6, 4, 2, 3, 8, 2, 9, 6,11, 4, 3, 9,
     2, 5, 4, 4, 4,10, 3, 2, 3, 2), 
code = c("A","E","B","A","E","D","B","F","C","G","A","E","B",
    "F","G","A","E","D","B","F","G","A","E","H","B","F","G"),
group = c("a)","b)","a)","a)","b)","a)","a)","b)","a)","b)","a)","b)"
    ,"a)","b)","b)","a)","b)","a)","a)","b)","b)","a)","b)","b)","a)","b)","b)")
)

ggplot(DATA, aes(x= code, y=freq,fill=color))+                                                                                           

facet_wrap(~group, nrow = 2, scales = "free") +
geom_bar(position=position_dodge(), stat="identity") +
geom_bar(position=position_dodge(), stat="identity", color = "black", 
fill = NA,  linetype = "dashed",
       size = 0.3,
       aes(group = color, y = max)) +

labs(y="Frequency",x= "Design")

我想在两个带有自由标度且仅带有一个y_axis标签的图中获得整数。小数位数不足。

1 个答案:

答案 0 :(得分:0)

如果您在scales = "free_x"中使用facet_wrap选项

您可以得到想要的情节。

 facet_wrap(~group, nrow = 2, scales = "free_x")

下面的完整代码:

ggplot(DATA, aes(x= code, y=freq,fill=color))+
  facet_wrap(~group, nrow = 2, scales = "free_x",) +
  geom_bar(position=position_dodge(), stat="identity") +
  geom_bar(position=position_dodge(), stat="identity", color = "black", 
           fill = NA,  linetype = "dashed",
           size = 0.3,
           aes(group = color, y = max)) +
  labs(y="Frequency",x= "Design")

Result