0

I made a lollipop graph but the space between them is uneven. It is fine to some extent but need a bit of aesthetics. I tried to fix several times but haven't got the graph that I want.

# Create data
data <- data.frame(
    x=LETTERS[1:24],
    a=sample(1:30,24),
    b=sample(1:40,24),
    c=sample(1:50,24),
    d=sample(1:20,24,replace=T),
    gp=c("Gp A","Gp B","Gp C","Gp D")
)

fig1 <-data %>%
  arrange(a) %>%
  mutate(x=factor(x, x)) %>%
  ggplot(aes(x=x,y=a))+
  labs(title = "Child")+
  geom_segment( aes(x=x, xend=x, y=0, yend=a), color="grey") +
  geom_point( aes(x=x, y=a, color=gp), size=5 ) +
  coord_flip()+
  theme_ipsum() +
  geom_text(aes(label=a),color = "black", size = 2.5)+
  theme(
    legend.position = "top",
    panel.grid.major = element_line(colour = "transparent"),
    panel.grid.minor = element_line(colour = "transparent"),
    panel.spacing = unit(0.5, "lines"),
    axis.text.y = element_text(color='black', size=8),
    strip.text.x = element_text(size = 10)
  ) +
  xlab("") +
  ylab("Number of articles") +
  facet_grid(gp~.,scale="free_y", space = "free_y")

I repeat the same codes for fig(b), fig3(c) and fig4(d). Then I combined all four graph using the following code:


          ggarrange(fig1,fig2, fig3,fig4, 
          labels = c("A", "B", "C","D"),
          ncol = 2, nrow = 2)

But the combined plots look a bit packed and not see clearly.

enter image description here

I would like to removed "Gp A" "Gp B" "Gp C" "Gp D" from each plot.

I'm trying to make the plot like below. I have tried several times but still haven't got it. It would be great and really appreciated if someone could kindly share codes how to do it like below graph. enter image description here

Myo
  • 21
  • 3
  • That spacing is usually most-easily remedied by making the graphics pane larger. If you're saving to a file, you can control the dimensions of the plot, perhaps `ggsave("myfile.pdf", width=8, height=14)`. – r2evans Aug 24 '22 at 12:55
  • 2
    If you want it to appear more like the last image, you don't want to use `facet_wrap`. I'm guessing, since your question isn't reproducible, that when you do that, the order is wrong on the y-axis. You can make that y-axis variable an ordered factor and make the factor levels the order in which you want them to appear. If you want help with that part, it's best to make your question reproducible. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Aug 24 '22 at 13:15
  • 2
    I think replacing `facet_wrap() with `facet_grid(Aetiology ~., scale = "free_y", space = "free_y")` might help (not tested). – teunbrand Aug 24 '22 at 13:17
  • 2
    Hi Myo. Welcome to StackOverflow. To getter better and more precise responses on your question please post some data. See here how to create a nice minimal example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – harre Aug 24 '22 at 13:28
  • 1
    Thank you all for helpful comments. facet_grid(Aetiology ~., scale="free_y", space="free_y") command works. I'll put the data to reproducible. – Myo Aug 24 '22 at 15:19

0 Answers0