0

I have some data:

df <- data.frame(grade=c('C','F','D','B','A','C','A','A','C','F','B','B','C','A','B'),test=c(800,700,750,220,550,650,675,350,350,460,780,540,675,490,370),
  condition=c('Condition 2','Condition 2','Condition 1','Condition 1','Condition 1','Condition 2','Condition 1','Condition 2','Condition 2','Condition 2','Condition 1','Condition 2','Condition1','Condition 2','Condition 2'), 
  count=c(10,11,12,15,12,13,15,13,19,18,15,20,20,22,23))

and I'm trying to make the legends be at the bottom and in the center

example <- ggplot(df, aes(x = grade, y = test, size = count, fill = condition)) +
  geom_point(shape=21)  +
  theme(legend.position = "bottom") + 
  guides(fill = guide_legend(order=1), size = guide_legend(order=2))

enter image description here

I would love it if the legend looked like this: Condition [condition legend] Count [count legend]

I tried this and I placed the nrow in the wrong place

example <- ggplot(df, aes(x = grade, y = test, size = count, fill = condition)) +
  geom_point(shape=21)  + 
  theme(legend.position = "bottom", nrow=4) + 
  guides(fill = guide_legend(order=1), size = guide_legend(order=2))

I found instructions here and nothing changed (search for "p2 + theme(legend.position = "bottom")"

example <- ggplot(df, aes(x = grade, y = test, size = count, fill = condition)) + 
  geom_point(shape=21)  + 
  theme(legend.position = "bottom") + 
  guides(fill = guide_legend(order=1), size = guide_legend(order=2)) + theme(legend.title.align=0.5)+ 
  theme(legend.justification = "bottom")

Can anyone assist?

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
tangerine7199
  • 443
  • 2
  • 8
  • 24

1 Answers1

0

Working off this answer I came to the following:

df <- data.frame(grade=c('C','F','D','B','A','C','A','A','C','F','B','B','C','A','B'),
                 test=c(800,700,750,220,550,650,675,350,350,460,780,540,675,490,370),
                 condition=c('Condition 2','Condition 2','Condition 1','Condition 1','Condition 1','Condition 2','Condition 1','Condition 2','Condition 2','Condition 2','Condition 1','Condition 2','Condition1','Condition 2','Condition 2'), 
                 count=c(10,11,12,15,12,13,15,13,19,18,15,20,20,22,23))

df$grade <- factor(df$grade, levels = c("A", "B", "C", "D", "F"))

library(ggplot2)
library(gridExtra)
library(gtable)
library(grid)

# Plot with color only
p1 <- ggplot(df, aes(grade, test)) +
  geom_point(aes(color = condition)) +
  theme(legend.position = "bottom")

# Extract legend
leg1 <- gtable_filter(ggplot_gtable(ggplot_build(p1)), "guide-box") 

# Plot with count only
p2 <- ggplot(df, aes(grade, test)) +
  geom_point(aes(size = count)) +
  theme(legend.position = "bottom")

# Extract legend
leg2 <- gtable_filter(ggplot_gtable(ggplot_build(p2)), "guide-box") 

# Produce the plot with no legends
p3 <- ggplot(df, aes(grade, test)) +
  geom_point(aes(color = condition, size = count)) +
  theme(legend.position = "none")

# Create a three-paneled plot, leaving space for each legend
plotNew <- arrangeGrob(p3, leg1,leg2, 
         heights = unit.c(unit(1, "npc") - (leg1$height + leg2$height),
                      leg1$height,
                      leg2$height), nrow = 3)

# Draw new plot with legends centered and stacked on top of each other at the bottom
grid.newpage()
grid.draw(plotNew)

enter image description here

Daniel Anderson
  • 2,394
  • 13
  • 26