skip to Main Content

Please, find My script below.

I have produced this plot

enter image description here

However, for graphic purposes, I would like to rearrange the horizontal legend to be presented as 2×2. I have manually produced an example of what it should look like in Photoshop:

enter image description here

Unfortunately, I have not been able to locate any clear instructions.

My script

ggplot(as.data.frame(out), aes(x = n.fjernet)) + theme +
  geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .1) +
  geom_line(aes(y = yhat, col = model),size=1) +

  ggtitle("Lymph node yield") + 

  geom_segment(aes(x = 0, y = 1, xend = 100, yend = 1), lty="dashed", size=0.5) +
  geom_segment(aes(x = 25, y = 1, xend = 25, yend = 0.5), lty="dashed", size=0.5, col="black") +

  geom_point(mapping = aes(x = 25, y = 1), size=2, shape=16, col="black", alpha=0.5) +

  scale_fill_manual(values = c("#DAE5F2", "#F9E7E5","#E4F2F3","#FAF1D9"), name = "",
                    labels = c("Overall survival (without LNY)", "Event-free survival (without LNY)","Overall survival (with LNY)", "Event-free survival (with LNY)")) +
  scale_colour_manual(values = c("#2C77BF", "#E38072","#6DBCC3","#E1B930"), name = "",
                      labels = c("Overall survival (without LNY)", "Event-free survival (without LNY)","Overall survival (with LNY)", "Event-free survival (with LNY)")) +

  scale_x_continuous(name="", breaks=seq(0,100,by=25), limits=c(0,100), label=c("0","25nas reference","50", "75", "100")) +
  scale_y_continuous(name="Hazard ratio", breaks = seq(0.5,1.2,by=.1)) +coord_cartesian(ylim=c(0.5,1.25)) +

  theme(axis.text.x = element_text(color = "grey20", size =11), 
        axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
        axis.text.y = element_text(color = "grey20", size = 11), 
        axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
        legend.key = element_rect(fill = "white"),
        plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5),
        legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top")

3

Answers


  1. Chosen as BEST ANSWER

    The solution was

    p + guides(fill=guide_legend(nrow=2,byrow=TRUE))
    

    When storing my ggplot in p


  2. You can try guides

    Let’s say you store your plot in p, you could write:

    p + guides(col = guide_legend(nrow=2, ncol=2))
    
    Login or Signup to reply.
  3. Using the dataset data(iris),part of your code and guides(col = guide_legend(nrow=2, ncol=2)), I have achieved the following:

    library(ggplot2)
    ggplot(iris, aes(x = Sepal.Length))+
       geom_point(aes(y = Sepal.Width, col = Species)) +
      theme(axis.text.x = element_text(color = "grey20", size =11), 
            axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
            axis.text.y = element_text(color = "grey20", size = 11), 
            axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
            legend.key = element_rect(fill = "white"),
            plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
            plot.subtitle = element_text(hjust = 0.5),
            legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top")+
            guides(col = guide_legend(title.position = "top",nrow = 2))
    

    Be aware that if you do fill, then in guides() you should also use fill. I have used col since it is what creates my legend.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search