How to create geom_boxplot with large amount of continuous x-variables

后端 未结 2 1744
刺人心
刺人心 2020-12-29 06:48

I have a data frame which contains x-axis numeric bins and continuous y-axis data across multiple categories. Initially, I created a boxplot by making the x-axis bins \"fact

相关标签:
2条回答
  • 2020-12-29 07:01

    Here is a way using the original data you posted on Google - which actually was much more helpful, IMO.

    ggplot(df, aes(x=CH, y=value,group=CH))+
      geom_boxplot(notch=FALSE, outlier.shape=NA, fill="red", alpha=0.2)+
      scale_x_log10()
    

    So, as @BenBolker said before he deleted his answer(??), you should leave the x-variable (CH) as numeric, and set group=CH in the call to aes(...).

    With your real data there is another problem though. Your CH is more or less logarithmically spaced, so there are about as many points < 1 as there are between 1 - 10, etc. ggplot wants to make the boxes all the same size, so with a linear x-axis the box width is smaller than the line width, and you don't see the boxes at all. Changing the x-axis to a logarithmic scale fixes that, more or less.

    0 讨论(0)
  • 2020-12-29 07:14

    Don't make x a factor. You need to aesthetically map a group that is a factor determining which box the value is associated with, luckily, after melting, this is what you variable column is:

    ggplot(df.m, aes(x = x, y = value, group = variable)) +
        geom_boxplot()
    

    As x is still numeric, you can give it whatever values you want within a specific variable level and the boxplot will show up at that spot. Or you could transform the x axis, etc.

    0 讨论(0)
提交回复
热议问题