首页 > 解决方案 > 如何在ggplot2中获取x轴的正数和负数并重新调整R中的y轴?

问题描述

我正在使用 R 中的 ggplot2 为两个名为 A 和 B 的对象绘制图表。我的代码是

  ggplot(test_error_bar_14_10_21, aes(x=levels, y=len, colour=index, group=index)) + 
  geom_errorbar(aes(ymin=len-ci, ymax=len+ci), width=.03) +
  geom_line() +
  geom_point(size=3, shape=21, fill="white") + # 21 is filled circle
  xlab("Percentage(%population)") +
  ylab("Treatment on new cases") +
  scale_colour_hue(name="Type of indices",    # Legend label, use darker colors
                   breaks=c("doses/population", "death/population"),
                   labels=c("A", "B"),
                   l=40) +                    # Use darker colors, lightness=40
  ggtitle("The Effect of different vaccines rates on \nA and B") +
  coord_flip() +                              #rotate the graph (https://ggplot2.tidyverse.org/reference/coord_flip.html)
  expand_limits(y=0) +                        # Expand y range
  scale_y_continuous(breaks=0:20*4) +         # Set tick every 4
  theme_bw() +
  theme(legend.justification=c(1,0),
        legend.position=c(1,0))               # Position legend in bottom right 

结果是

在此处输入图像描述

可以看出,我在 x 轴上有负面结果,那么我应该怎么做才能在 x 轴上标记负区间?我在这里看到了一个帖子,但它仅适用于负值,而不适用于同一轴上的负值和正值。

除此之外,如何根据当前图表中的 10%-20%-...100% 而不是 0-25%-75%-100% 重新调整 y 轴。

正如@Park 所建议的,我在test_error_bar_14_10_21这里添加了整个示例

index            levels   len        ci

doses/population 0.1      7.232200   7.511183

doses/population 0.2      2.542600   8.51828

doses/population 0.3      -0.615100  10.090960

doses/population 0.4      -1.219400  9.363690

doses/population 0.5      -2.942717  11.359863

doses/population 0.6      -2.063000  9.12014

doses/population 0.7      0.721000   8.69263

doses/population 0.8      3.288216   10.747079

doses/population 0.9      6.778900   8.944848

doses/population 1.0      7.652900   8.24641

death/population 0.1      4.645150   11.179297

death/population 0.2     -5.860138   14.206702

death/population 0.3     -3.841500   15.451860

death/population 0.4      2.966200   16.215530

death/population 0.5      2.168000   18.536120

(对不起,我在使用 dput 时遇到一个错误,它只读取了我的 4 列中的 2 列,所以我在这里直接输入数据)。我按照这篇文章使用 dput但不幸的是到目前为止我失败了。

我添加了@Dave2e 建议的代码,到目前为止它可以工作,结果如下

在此处输入图像描述

更新:我添加了@Park的代码,以改进左栏中的显示为百分比显示

在此处输入图像描述

在@Park 的帮助下,我得到了想要的结果。非常感谢

在此处输入图像描述

标签: rggplot2scale

解决方案


我命名了你的数据dummy。在 x 轴上,@Dave2e 的评论很有帮助,所以我没有做任何更改,只是添加了一些内容。更改labels = scales::percentlables = label_percent(accuracy = 5L)将摆脱回合。并制作另一个dummy2指示误差线大小的向量。

dummy2 <- dummy %>%
  mutate(width = ifelse(index == "doses/population", 1, 0.03)) %>% pull(width)

dummy %>%
  ggplot( aes(x=levels, y=len, colour=index, group=index)) + 
  geom_errorbar(aes(ymin=len-ci, ymax=len+ci), width= 0.03, size = dummy2) +
  geom_line() +
  geom_point(size=3, shape=21, fill="white") + # 21 is filled circle
  xlab("Percentage(%population)") +
  ylab("Treatment on new cases") +
  scale_colour_hue(name="Type of indices",    # Legend label, use darker colors
                   breaks=c("doses/population", "death/population"),
                   labels=c("A", "B"),
                   l=40) +                    # Use darker colors, lightness=40
  ggtitle("The Effect of different vaccines rates on \nA and B") +
  coord_flip() +                              #rotate the graph (https://ggplot2.tidyverse.org/reference/coord_flip.html)
  expand_limits(y=0) +                        # Expand y range
  theme_bw() +
  theme(legend.justification=c(1,0), legend.position=c(1,0)) + 
  scale_y_continuous(breaks=seq(-20,20,4)) + 
  scale_x_continuous(n.breaks=10, labels = label_percent(accuracy = 5L))

在此处输入图像描述


推荐阅读