Reputation: 113
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
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
Update: I add the code of @Park to improve the display in the left column to percentage display
After the help from @Park, I got the desired results. Thanks a heaps
Upvotes: 1
Views: 1877
Reputation: 15143
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))
Upvotes: 1