R-Plot网格图例编辑

时间:2017-08-15 13:23:51

标签: r ggplot2

我有一个R项目,我需要在一个页面上绘制九所学校,所以我使用plot_grid来协助。下面的代码基于虚拟数据:

#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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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 that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 1','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 2','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 3','long condition that has to be spelled out 4','long condition that has to be spelled out 4', 'long condition that has to be spelled out 4', 'long condition that has to be 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)

以下是我正在尝试构建网格的地方:

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 My Grid Here", fontface='bold')  

df_grid <- plot_grid(title, df_grid, ncol=1, rel_heights=c(0.1, 1))  

我得到了这个: enter image description here

有没有办法可以编辑图例,以便条件列在两行?

我尝试了这个并且根本没有对最终图像做任何事情:

df_grid + guides(col = guide_legend(nrow = 4))

谢谢

0 个答案:

没有答案