1

I am drawing a graph using ggplot2 in R for two objects called A and B. My code is

  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 

The result is

enter image description here

As can be seen, I have the negative results on x-axis, so what I should do to also mark the negative interval on x-axis ? I saw a post here but it is only for negative value, not for both negative and positive values on the same axis.

Apart from that, how to rescale the y-axis based on 10%-20%-...100% rather than 0-25%-75%-100% as in the current graph.

As suggested by @Park, I add the whole sample of test_error_bar_14_10_21 here

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

(sorry I got an error when using dput that it only reads 2 over 4 columns of mine so I type here the data directly). I followed this post to use dput but I failed so far unfortunately.

I add the code suggested by @Dave2e and it works so far, the result is as below

enter image description here

Update: I add the code of @Park to improve the display in the left column to percentage display

enter image description here

After the help from @Park, I got the desired results. Thanks a heaps

enter image description here

Nguyen Lis
  • 113
  • 4
  • 2
    Can you provide some sample of `test_error_bar_14_10_21`? You may use `dput(test_error_bar_14_10_21))`. – Park Oct 14 '21 at 02:04
  • 2
    Try adding this: `scale_y_continuous(breaks=seq(-20,20,4))` and `scale_x_continuous(n.breaks=10)` – Dave2e Oct 14 '21 at 02:46
  • Thanks for your data. Now I'm wondering about your second plot. Is x axis of that plot is done? Then, rescale y axis as 0-25%-50%-75%-100% is left? – Park Oct 14 '21 at 04:18
  • Hi @Park, From what I see, it seems that the x-axis is done from my understanding, I am sorry if there is anything here confused you. And I think the y-axis at the left also show 0.1 ->1 with the interval of 0.1. I am wondering how we can change it to **percentage display**. I mean, instead of 0.1, 0.2,...,1, what I should do to transform it to 10%, 20%, ...100% – Nguyen Lis Oct 14 '21 at 04:23
  • And one more question, maybe it is out of the scope of this question, but we are following this post, if it is not politically correct, please let me know that I can think of opening another topic. Is there any way that we can make the red line thichker but stranparent that we can see both red and green line above more clearly. I know we can use ```pd <- position_dodge(0.1)``` but it will separate the red and green lines separately but not on the same y-axis, making by graph harder to read – Nguyen Lis Oct 14 '21 at 04:26
  • Change axis to percentage is simple. in @Dave2e's code, just add `labels = scales::percent` inside `scale_x_continuous`. Like `scale_x_continuous(n.breaks=10, labels = scales::percent)`. I'll take a look at your another question. – Park Oct 14 '21 at 04:26
  • Thanks @Park, I updated the result when adding the ```scales::percent```, how to convert 100.0% to 100% in this graph then, can I ask? I updated the picture as in the original post – Nguyen Lis Oct 14 '21 at 04:29

1 Answers1

1

I named your data dummy. In x axis, @Dave2e 's comment is very helpful, so I made nothing change, just some addition. Change labels = scales::percent to lables = label_percent(accuracy = 5L) will get rid of rounds. And make another vector dummy2 that indicates the size of errorbar.

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))

enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Park
  • 14,771
  • 6
  • 10
  • 29
  • Hi @Park, thanks a lot for your help, I have some questions as below – Nguyen Lis Oct 14 '21 at 05:26
  • 1> I am wondering why I change the code ``` labels = scales::percent``` to ```lables = label_percent(accuracy = 5L)``` in my original code, the graph does not change – Nguyen Lis Oct 14 '21 at 05:27
  • 2> I search and see the symbol ```%>%``` meaning here (https://stackoverflow.com/questions/27125672/what-does-function-mean-in-r). But I do not understand why when I run the first three lines of code, it caused some errors that I will pot in the original post that we can easily follow. – Nguyen Lis Oct 14 '21 at 05:29
  • @NguyenLis I didn't notice that you do not use `library(dplyr)`. It's my mistake. Try load that library and try again. Also, try `labels = scales::label_percent(accuracy = 5L)` – Park Oct 14 '21 at 05:30
  • I am testing, just want to thankfully report here that ```labels = scales::label_percent(accuracy = 5L)``` nicely work – Nguyen Lis Oct 14 '21 at 05:37
  • @NguyenLis It would be better to add that code/error on your post. It's pretty hard to read/understand whole code/error.. – Park Oct 14 '21 at 05:44
  • Hi @Park, I nicely apply the code and the result went well as I updated from the original post. Thanks a heap. Just one more question, after running all, I face this error notice, do you know how to overcome it then ```Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.``` – Nguyen Lis Oct 14 '21 at 05:49
  • @NguyenLis That's not error. Just a warning. You can ignore that. – Park Oct 14 '21 at 05:51
  • one side question when I look at the picture again. The last picture I updated in the original post, the line at the smallest level (10%), the red line I did not see the right end. But when I drag in the Plots box of R, I can saw the whole picture but the picture at that time is ugly. Can you please help me to adjust the interval length in x-axis smaller in size ? Many thanks and warm regards. – Nguyen Lis Oct 14 '21 at 06:10
  • @NguyenLis To adjust x axis in plot, use `scale_y_continuous(breaks=seq(-20,20,4))`. That `breaks` and `seq(...)` object control the x axis. You may change that sequence by yourself. – Park Oct 14 '21 at 06:17