2014-12-31 3 views
9

Я хотел бы положить длинную легенду в две колонки, и я не имею никакого успеха. Вот код, который я использую с решением, найденным в другом месте, которое не работает для geom='area', хотя оно работает и для моих других графиков. Сюжет, который я получаю от кода ниже, выглядит так: single column legendКак положить легенду ggplot2 в две колонки для участка участка

Так как я могу построить Q1 с легендой в двух столбцах, пожалуйста?

NVER <- 10 
NGRID <- 20 
MAT <- matrix(NA, nrow=NVER, ncol=NGRID) 
gsd <- 0.1 # standard deviation of the Gaussians 

verlocs <- seq(from=0, to=1, length.out=NVER) 
thegrid <- seq(from=0, to=1, length.out=NGRID) 
# create a mixture of Gaussians with modes spaced evenly on 0 to 1 
# i.e. the first mode is at 0 and the last mode is at 1 
for (i in 1:NVER) { 
    # add the shape of gaussian i 
    MAT[i,] <- dnorm(thegrid, verlocs[[i]], sd=gsd) 
} 
M2 <- MAT/rowSums(MAT) 
colnames(M2) <- as.character(thegrid) 
# rownames(M2) <- as.character(verlocs) 
library(reshape2) 
D2 <- melt(M2) 
# head(D2) 
# str(D2) 
D2$Var1 <- ordered(D2$Var1) 
library(ggplot2) 
Q1 <- qplot(Var2, value, data=D2, order=Var1, fill=Var1, geom='area') 
Q1 
# ggsave('sillyrainbow.png') 
# now try the stackoverflow guide() solution 
Q1 + guides(col=guide_legend(ncol=2)) # try but fail to put the legend in two columns! 

Обратите внимание, что решение в creating columns within a legend list while using ggplot in R code включено выше, и он не работает, к сожалению!

ответ

15

Вы имеете в виду неправильное руководство.

Q1 + guides(fill=guide_legend(ncol=2)) 

resulting plot

Смежные вопросы