Marna Wagley
2017-Nov-10 09:21 UTC
[R] How to create separate legend for each plot in the function of facet_wrap in ggplot2?
Hi R users, I need to create more than 20 figures (one for each group) in one page. I have a common legend for 20 figures using the facet_wrap. However the range of the values among the groups are very wide. For example one group has the value of 0 to 3, but the values of some of the groups has ranged from 0 to 20 so that when I used a single common legend for all 20 figures, I could not display the contrast of the values in some of the figures. Therefore I wanted to create the figures with *a separate legend*.In this way, I can display the gradient of the values in each figure. Any suggestions on how I can create it. The example is given below, *I wanted to create a separate legend with keeping legend inside of each of the figure*. library(ggplot2) dat<-structure(list(X = c(289.6, 289.7, 289.8, 289.9, 290, 290.1, 927.8, 927.9, 928, 928.1, 928.2, 928.3), Y = c(789.1, 789.2, 789.3, 789.4, 789.5, 789.6, 171.1, 171.2, 171.3, 171.4, 171.5, 171.6), value = c(0.05, 0.06, 0.07, 0.09, 0.1, 0.11, 0.06, 0.05, 0.05, 0.06, 0.1, 1.5), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("X", "Y", "value", "group"), class = "data.frame", row.names = c(NA, -12L)) AB<-ggplot(data = dat, aes(x = X, y = Y, color = value)) + geom_point(size =2) + coord_equal() + theme_bw()+ scale_color_gradientn(colours = terrain.colors( 7)) AB+facet_wrap(~group, scales="free")+theme(strip.text = element_text(size = 8)) Thanks MW [[alternative HTML version deleted]]
Dominik Schneider
2017-Nov-10 09:24 UTC
[R] How to create separate legend for each plot in the function of facet_wrap in ggplot2?
That's not the point of facet_wrap so check out the cowplot package for combining multiple ggplot objects (with legends) into one figure. On Fri, Nov 10, 2017 at 10:21 AM, Marna Wagley <marna.wagley at gmail.com> wrote:> Hi R users, > I need to create more than 20 figures (one for each group) in one page. I > have a common legend for 20 figures using the facet_wrap. However the > range of the values among the groups are very wide. For example one group > has the value of 0 to 3, but the values of some of the groups has ranged > from 0 to 20 so that when I used a single common legend for all 20 figures, > I could not display the contrast of the values in some of the figures. > Therefore I wanted to create the figures with *a separate legend*.In this > way, I can display the gradient of the values in each figure. Any > suggestions on how I can create it. > > The example is given below, *I wanted to create a separate legend with > keeping legend inside of each of the figure*. > > library(ggplot2) > > dat<-structure(list(X = c(289.6, 289.7, 289.8, 289.9, 290, 290.1, > > 927.8, 927.9, 928, 928.1, 928.2, 928.3), Y = c(789.1, 789.2, > > 789.3, 789.4, 789.5, 789.6, 171.1, 171.2, 171.3, 171.4, 171.5, > > 171.6), value = c(0.05, 0.06, 0.07, 0.09, 0.1, 0.11, 0.06, 0.05, > > 0.05, 0.06, 0.1, 1.5), group = structure(c(1L, 1L, 1L, 1L, 1L, > > 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor")), > .Names = c("X", > > "Y", "value", "group"), class = "data.frame", row.names = c(NA, > > -12L)) > > > AB<-ggplot(data = dat, aes(x = X, y = Y, color = value)) + > geom_point(size > =2) + > > coord_equal() + theme_bw()+ scale_color_gradientn(colours > terrain.colors( > 7)) > > AB+facet_wrap(~group, scales="free")+theme(strip.text = element_text(size > = 8)) > > > > > Thanks > > > MW > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]