简体   繁体   English

R 绘图时 x 轴出现问题

[英]R problems with x-axis when plotting

I am plotting my dataset and I want to have under each facet a separate x-axis.我正在绘制我的数据集,我希望在每个方面都有一个单独的 x 轴。 I have seen other proposed solutions but they are all more or less based on the scales="free" command which I also use but it is not resulting in exactly what I want.我已经看到了其他提议的解决方案,但它们或多或少都基于我也使用的scales="free"命令,但它并没有得到我想要的结果。
My code:我的代码:

mapping <- aes(
  x = x_label,
  y = mean,
)
plot <- (ggplot(data=df_final, mapping=mapping)
         + geom_point()
         + facet_grid(boundry ~ group, scales = "free")
         + theme(axis.text.x = element_text(angle = 45))
)

and the output from dput:和来自 dput 的 output:

> dput(df_final)
structure(list(combination = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 
4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 10, 10, 
11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 
16, 17, 17, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), priority = c(3L, 3L, 5L, 3L, 
3L, 5L, 3L, 3L, 5L, 3L, 3L, 5L, 3L, 3L, 5L, 3L, 3L, 5L, 3L, 3L, 
5L, 3L, 3L, 5L, 3L, 5L, 1L, 3L, 3L, 5L, 1L, 3L, 5L, 3L, 5L, 1L, 
3L, 3L, 5L, 3L, 3L, 5L, 3L, 3L, 5L, 3L, 5L, 3L, 5L, 3L, 3L, 5L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), boundry = c("f", "t", "t", "f", 
"t", "t", "f", "t", "t", "f", "t", "t", "f", "t", "t", "f", "t", 
"t", "f", "t", "t", "f", "t", "t", "t", "t", "f", "f", "t", "t", 
"f", "f", "t", "t", "t", "f", "f", "t", "t", "f", "t", "t", "f", 
"t", "t", "f", "t", "f", "t", "f", "t", "t", "f", "f", "f", "f", 
"f", "f", "f", "f", "t", "t", "t", "t", "t", "t", "t", "t", "t", 
"t", "t", "t", "t", "t", "t", "t"), mean = c(3.57246241300093, 
0.223278628500933, 0.0576045030267333, 3.47917123999747, 0.262627433455467, 
0.0568350288493333, 3.541798911422, 0.2300142381712, 0.0578985239072667, 
3.53039631497327, 0.223439785123733, 0.0594544778294667, 3.555082939998, 
0.290927503755467, 4.99720916622347, 3.5148755357448, 0.2171132381202, 
0.0587134527254667, 3.55752056473307, 0.242529728515533, 0.0574939280672, 
3.5486223596312, 0.235554695571133, 0.0553161936733333, 0.127090894458333, 
0.103398049264, 0.796592095286273, 2.55477406604567, 0.503062014775867, 
0.0433218230143333, 0.096633892883, 2.52616000748233, 4.6637793703124, 
0.230329894736867, 0.458059911769, 0.116944939478867, 2.50189798417633, 
0.375344985710867, 0.756807906329867, 3.5250821665858, 0.252925866716533, 
0.0555290232793333, 3.5202609789174, 0.262929104007133, 0.0556773603774667, 
0.894493875427733, 3.1103110398666, 0.363831092087467, 4.00991445910473, 
345.265829701703, 3.38619075901827, 0.0556595059382, 3.50358620292527, 
3.50358620292527, 3.50358620292527, 3.50358620292527, 3.50358620292527, 
3.50358620292527, 3.50358620292527, 3.50358620292527, 0.233816137867, 
0.233816137867, 0.233816137867, 0.233816137867, 0.233816137867, 
0.233816137867, 0.233816137867, 0.233816137867, 0.0536830562219333, 
0.0536830562219333, 0.0536830562219333, 0.0536830562219333, 0.0536830562219333, 
0.0536830562219333, 0.0536830562219333, 0.0536830562219333), 
    group = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
    3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 
    6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 1, 2, 
    3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 
    6, 7, 8), x_label = c(4, 4, 4, 100, 100, 100, 1, 1, 1, 100, 
    100, 100, 40, 40, 40, 150, 150, 150, 40, 40, 40, 150, 150, 
    150, 125, 125, 500, 500, 500, 500, 1000, 1000, 1000, 1522, 
    1522, 128, 128, 128, 128, 512, 512, 512, 128, 128, 128, 250000, 
    250000, 100000, 100000, 100000000, 100000000, 100000000, 
    40, 40, 100, 100, 250, 512, 1522, 1000000, 40, 40, 100, 100, 
    250, 512, 1522, 1000000, 40, 40, 100, 100, 250, 512, 1522, 
    1000000)), .Names = c("combination", "priority", "boundry", 
"mean", "group", "x_label"), row.names = c(NA, -76L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x555f0e7ee668>)

Here is the plot I get and as you can see there is a different x-axis per column but not for each row as well.这是我得到的 plot,正如您所看到的,每列有不同的 x 轴,但每行也没有。 -> I want it both per column and per row so that each facet has its own values on the x axis: -> 我希望每列和每行都有它,以便每个方面在 x 轴上都有自己的值:
图片

The lemon -package provides a way to repeat the x-axis labels and ticks with coord_capped_cart() . lemon包提供了一种使用coord_capped_cart()重复 x 轴标签和刻度的方法。

I am not sure the output is what you want, but the code below does the trick required: repeat the X-axis labels.我不确定 output 是否是您想要的,但下面的代码可以实现所需的技巧:重复 X 轴标签。

Alternative 1备选方案 1

In order to make it work, I had to change facet_grid() to facet_wrap()为了使它工作,我不得不将facet_grid()更改为facet_wrap()

library(ggplot2)
library(lemon)

ggplot(df_final, aes(x_label, mean)) + 
  geom_point() + 
  facet_wrap(boundry ~ group, scales = "free", nrow = 2) +  ### changed
  theme(axis.text.x = element_text(angle = 45)) +
  coord_capped_cart()                                       ### added

This yields the following graph:这会产生以下图表:

在此处输入图像描述

Alternative 2备选方案 2

More straigthforward with lemon 's facet_rep_grid() -function:使用lemonfacet_rep_grid()功能更直接:

ggplot(df_final, aes(x_label, mean)) + 
  geom_point() + 
  facet_rep_grid(boundry ~ group,                ### changed to facet_rep_grid
                 scales = "free", 
                 repeat.tick.labels = "all") +   ### added (or T/F, x/y, trbl)
  theme(axis.text.x = element_text(angle = 45))

which gives the following pic:这给出了以下图片:

在此处输入图像描述

Alternative 3备选方案 3

If you do not want the Y-axis labels to be repeated, then replace "all" with "bottom" in the repeat.tick.labels argument, which produces the following pic:如果您不想重复 Y 轴标签,则在repeat.tick.labels参数中将“all”替换为“bottom”,这会产生以下图片:

在此处输入图像描述

Please let me know whether this is what you wanted.请让我知道这是否是您想要的。

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

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