ggplot2 - R-Plot Grid Legend Editing -
i have r project need plot 9 schools on 1 page used plot_grid assist. code below based on dummy data:
#dataframes df1 <- data.frame(acro=c('school1','school1','school1','school1','school1','school1','school1','school1','school1','school1','school1','school1','school1','school1','school1','school1'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(53,93,19,37,60,90,39,19,75,65,24,94,44,16,40,84), score=c(275,530,571,330,306,533,618,774,401,605,356,344,493,778,792,306) ,state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) df2 <- data.frame(acro=c('school2','school2','school2','school2','school2','school2','school2','school2','school2','school2','school2','school2','school2','school2','school2','school2'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(53,93,19,37,60,90,39,19,75,65,24,94,44,16,40,84), score=c(275,530,571,330,306,533,618,774,401,605,356,344,493,778,792,306) ,state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) df3 <- data.frame(acro=c('school3','school3','school3','school3','school3','school3','school3','school3','school3','school3','school3','school3','school3','school3','school3','school3'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(53,93,19,37,60,90,39,19,75,65,24,94,44,16,40,84), score=c(275,530,571,330,306,533,618,774,401,605,356,344,493,778,792,306) ,state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) df4 <- data.frame(acro=c('school4','school4','school4','school4','school4','school4','school4','school4','school4','school4','school4','school4','school4','school4','school4','school4'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(53,93,19,37,60,90,39,19,75,65,24,94,44,16,40,84), score=c(275,530,571,330,306,533,618,774,401,605,356,344,493,778,792,306) ,state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) df5 <- data.frame(acro=c('school5','school5','school5','school5','school5','school5','school5','school5','school5','school5','school5','school5','school5','school5','school5','school5'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(53,93,19,37,60,90,39,19,75,65,24,94,44,16,40,84), score=c(275,530,571,330,306,533,618,774,401,605,356,344,493,778,792,306) ,state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) df6 <- data.frame(acro=c('school6','school6','school6','school6','school6','school6','school6','school6','school6','school6','school6','school6','school6','school6','school6','school6'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(53,93,19,37,60,90,39,19,75,65,24,94,44,16,40,84), score=c(275,530,571,330,306,533,618,774,401,605,356,344,493,778,792,306) ,state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) df7 <- data.frame(acro=c('school7','school7','school7','school7','school7','school7','school7','school7','school7','school7','school7','school7','school7','school7','school7','school7'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(53,93,19,37,60,90,39,19,75,65,24,94,44,16,40,84), score=c(275,530,571,330,306,533,618,774,401,605,356,344,493,778,792,306) ,state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) df8 <- data.frame(acro=c('school8','school8','school8','school8','school8','school8','school8','school8','school8','school8','school8','school8','school8','school8','school8','school8'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(95,57,90,92,56,20,33,58,39,31,18,42,77,63,73,29), score=c(408,270,265,515,702,417,495,379,384,253,603,773,675,779,765,760), state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) df9 <- data.frame(acro=c('school9','school9','school9','school9','school9','school9','school9','school9','school9','school9','school9','school9','school9','school9','school9','school9'),condition=c('long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 1','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 2','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 3','long condition has spelled out 4','long condition has spelled out 4', 'long condition has spelled out 4', 'long condition has spelled out 4'), count=c(79,62,29,27,19,39,83,91,27,88,88,72,45,51,25,73),score=c(754,735,451,467,261,436,563,310,379,786,528,429,776,548,378,408), state=c('state1','state1','state1','state1','state2','state2','state2','state2','state3','state3','state3','state3','state4','state4','state4','state4')) #bubble charts df1_bubble <- ggplot(df1, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 1") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900) df2_bubble <- ggplot(df2, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 2") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900) df3_bubble <- ggplot(df3, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 3") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900) df4_bubble <- ggplot(df4, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 4") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900) df5_bubble <- ggplot(df5, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 5") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900) df6_bubble <- ggplot(df6, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 6") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900) df7_bubble <- ggplot(df7, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 7") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900) df8_bubble <- ggplot(df8, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 8") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900) df9_bubble <- ggplot(df9, aes(x = state, y = score, size = count, fill = condition)) + geom_point(shape=21) + ggtitle("school 9") + theme(plot.title = element_text(hjust = 0.5))+ labs(x = "state", y = "score") + scale_size_area(max_size = 20) + theme(legend.position="none") + theme(axis.title.x = element_blank())+ theme(axis.title.y = element_blank()) + ylim(200, 900)
and below i'm trying construct grid:
library(cowplot) df_grid <- plot_grid(df1_bubble + theme(legend.position="none"), df2_bubble + theme(legend.position="none"), df3_bubble + theme(legend.position="none"),df4_bubble + theme(legend.position="none"), df5_bubble + theme(legend.position="none"),df6_bubble + theme(legend.position="none"), df7_bubble + theme(legend.position="none"),df8_bubble + theme(legend.position="none"), df9_bubble + theme(legend.position="none"),align = 'vh') legend <- get_legend(df1_bubble + theme(legend.position="bottom", legend.box = "vertical", legend.justification = "center")) df_grid <- plot_grid(df_grid, legend, ncol = 1, rel_heights = c(1, .2)) title <- ggdraw() + draw_label("the full title of grid here", fontface='bold') df_grid <- plot_grid(title, df_grid, ncol=1, rel_heights=c(0.1, 1))
is there way can edit legend conditions listed on 2 lines?
i tried , didn't final image @ all:
df_grid + guides(col = guide_legend(nrow = 4))
thank you
Comments
Post a Comment